| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 2.1 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This library is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * Lesser General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Lesser General Public |
| 15 | * License along with this library; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | */ |
| 18 |
| 19 | #ifndef VRECT_H |
| 20 | #define VRECT_H |
| 21 | #include "vglobal.h" |
| 22 | #include "vpoint.h" |
| 23 |
| 24 | V_BEGIN_NAMESPACE |
| 25 | class VRectF; |
| 26 |
| 27 | class VRect { |
| 28 | public: |
| 29 | VRect() = default; |
| 30 | VRect(int x, int y, int w, int h):x1(x),y1(y),x2(x+w),y2(y+h){} |
| 31 | explicit VRect(const VRectF &r); |
| 32 | V_CONSTEXPR bool empty() const {return x1 >= x2 || y1 >= y2;} |
| 33 | V_CONSTEXPR int left() const {return x1;} |
| 34 | V_CONSTEXPR int top() const {return y1;} |
| 35 | V_CONSTEXPR int right() const {return x2;} |
| 36 | V_CONSTEXPR int bottom() const {return y2;} |
| 37 | V_CONSTEXPR int width() const {return x2-x1;} |
| 38 | V_CONSTEXPR int height() const {return y2-y1;} |
| 39 | V_CONSTEXPR int x() const {return x1;} |
| 40 | V_CONSTEXPR int y() const {return y1;} |
| 41 | VSize size() const {return {width(), height()};} |
| 42 | void setLeft(int l) { x1 = l; } |
| 43 | void setTop(int t) { y1 = t; } |
| 44 | void setRight(int r) { x2 = r; } |
| 45 | void setBottom(int b) { y2 = b; } |
| 46 | void setWidth(int w) { x2 = x1 + w; } |
| 47 | void setHeight(int h) { y2 = y1 + h; } |
| 48 | VRect translated(int dx, int dy) const; |
| 49 | void translate(int dx, int dy); |
| 50 | bool contains(const VRect &r, bool proper = false) const; |
| 51 | bool intersects(const VRect &r); |
| 52 | friend V_CONSTEXPR inline bool operator==(const VRect &, |
| 53 | const VRect &) noexcept; |
| 54 | friend V_CONSTEXPR inline bool operator!=(const VRect &, |
| 55 | const VRect &) noexcept; |
| 56 | friend VDebug & operator<<(VDebug &os, const VRect &o); |
| 57 |
| 58 | VRect intersected(const VRect &r) const; |
| 59 | VRect operator&(const VRect &r) const; |
| 60 |
| 61 | private: |
| 62 | int x1{0}; |
| 63 | int y1{0}; |
| 64 | int x2{0}; |
| 65 | int y2{0}; |
| 66 | }; |
| 67 |
| 68 | inline VRect VRect::intersected(const VRect &r) const |
| 69 | { |
| 70 | return *this & r; |
| 71 | } |
| 72 |
| 73 | inline bool VRect::intersects(const VRect &r) |
| 74 | { |
| 75 | return (right() > r.left() && left() < r.right() && bottom() > r.top() && |
| 76 | top() < r.bottom()); |
| 77 | } |
| 78 |
| 79 | inline VDebug &operator<<(VDebug &os, const VRect &o) |
| 80 | { |
| 81 | os << "{R " << o.x() << "," << o.y() << "," << o.width() << "," |
| 82 | << o.height() << "}"; |
| 83 | return os; |
| 84 | } |
| 85 | V_CONSTEXPR inline bool operator==(const VRect &r1, const VRect &r2) noexcept |
| 86 | { |
| 87 | return r1.x1 == r2.x1 && r1.x2 == r2.x2 && r1.y1 == r2.y1 && r1.y2 == r2.y2; |
| 88 | } |
| 89 |
| 90 | V_CONSTEXPR inline bool operator!=(const VRect &r1, const VRect &r2) noexcept |
| 91 | { |
| 92 | return r1.x1 != r2.x1 || r1.x2 != r2.x2 || r1.y1 != r2.y1 || r1.y2 != r2.y2; |
| 93 | } |
| 94 |
| 95 | inline VRect VRect::translated(int dx, int dy) const |
| 96 | { |
| 97 | return {x1 + dx, y1 + dy, x2 - x1, y2 - y1}; |
| 98 | } |
| 99 |
| 100 | inline void VRect::translate(int dx, int dy) |
| 101 | { |
| 102 | x1 += dx; |
| 103 | y1 += dy; |
| 104 | x2 += dx; |
| 105 | y2 += dy; |
| 106 | } |
| 107 |
| 108 | inline bool VRect::contains(const VRect &r, bool proper) const |
| 109 | { |
| 110 | if (!proper) { |
| 111 | if ((x1 <= r.x1) && (x2 >= r.x2) && (y1 <= r.y1) && (y2 >= r.y2)) |
| 112 | return true; |
| 113 | return false; |
| 114 | } else { |
| 115 | if ((x1 < r.x1) && (x2 > r.x2) && (y1 < r.y1) && (y2 > r.y2)) |
| 116 | return true; |
| 117 | return false; |
| 118 | } |
| 119 | } |
| 120 |
| 121 | class VRectF { |
| 122 | public: |
| 123 | VRectF() = default; |
| 124 | VRectF(float x, float y, float w, float h):x1(x),y1(y),x2(x+w),y2(y+h){} |
| 125 | explicit VRectF(const VRect &r):x1(r.left()),y1(r.top()), |
| 126 | x2(r.right()),y2(r.bottom()){} |
| 127 |
| 128 | V_CONSTEXPR bool empty() const {return x1 >= x2 || y1 >= y2;} |
| 129 | V_CONSTEXPR float left() const {return x1;} |
| 130 | V_CONSTEXPR float top() const {return y1;} |
| 131 | V_CONSTEXPR float right() const {return x2;} |
| 132 | V_CONSTEXPR float bottom() const {return y2;} |
| 133 | V_CONSTEXPR float width() const {return x2-x1;} |
| 134 | V_CONSTEXPR float height() const {return y2-y1;} |
| 135 | V_CONSTEXPR float x() const {return x1;} |
| 136 | V_CONSTEXPR float y() const {return y1;} |
| 137 | V_CONSTEXPR inline VPointF center() const |
| 138 | { |
| 139 | return {x1 + (x2 - x1) / 2.f, y1 + (y2 - y1) / 2.f}; |
| 140 | } |
| 141 | void setLeft(float l) { x1 = l; } |
| 142 | void setTop(float t) { y1 = t; } |
| 143 | void setRight(float r) { x2 = r; } |
| 144 | void setBottom(float b) { y2 = b; } |
| 145 | void setWidth(float w) { x2 = x1 + w; } |
| 146 | void setHeight(float h) { y2 = y1 + h; } |
| 147 | void translate(float dx, float dy) |
| 148 | { |
| 149 | x1 += dx; |
| 150 | y1 += dy; |
| 151 | x2 += dx; |
| 152 | y2 += dy; |
| 153 | } |
| 154 |
| 155 | private: |
| 156 | float x1{0}; |
| 157 | float y1{0}; |
| 158 | float x2{0}; |
| 159 | float y2{0}; |
| 160 | }; |
| 161 |
| 162 | inline VRect::VRect(const VRectF &r):x1(r.left()),y1(r.top()), |
| 163 | x2(r.right()),y2(r.bottom()){} |
| 164 | V_END_NAMESPACE |
| 165 |
| 166 | #endif // VRECT_H |
| 167 |