| 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 VPOINT_H |
| 20 | #define VPOINT_H |
| 21 |
| 22 | #include "vglobal.h" |
| 23 |
| 24 | V_BEGIN_NAMESPACE |
| 25 |
| 26 | class VPointF { |
| 27 | public: |
| 28 | VPointF() = default; |
| 29 | constexpr inline VPointF(float x, float y) noexcept : mx(x), my(y) {} |
| 30 | constexpr inline float x() const noexcept { return mx; } |
| 31 | constexpr inline float y() const noexcept { return my; } |
| 32 | inline float & rx() noexcept { return mx; } |
| 33 | inline float & ry() noexcept { return my; } |
| 34 | inline void setX(float x) { mx = x; } |
| 35 | inline void setY(float y) { my = y; } |
| 36 | inline VPointF operator-() noexcept { return {-mx, -my}; } |
| 37 | inline VPointF & operator+=(const VPointF &p) noexcept; |
| 38 | inline VPointF & operator-=(const VPointF &p) noexcept; |
| 39 | friend const VPointF operator+(const VPointF &p1, const VPointF &p2) |
| 40 | { |
| 41 | return VPointF(p1.mx + p2.mx, p1.my + p2.my); |
| 42 | } |
| 43 | inline friend bool fuzzyCompare(const VPointF &p1, const VPointF &p2); |
| 44 | inline friend VDebug & operator<<(VDebug &os, const VPointF &o); |
| 45 |
| 46 | friend inline VPointF operator-(const VPointF &p1, const VPointF &p2); |
| 47 | friend inline const VPointF operator*(const VPointF &, float val); |
| 48 | friend inline const VPointF operator*(float val, const VPointF &); |
| 49 | friend inline const VPointF operator/(const VPointF &, float val); |
| 50 | friend inline const VPointF operator/(float val, const VPointF &); |
| 51 |
| 52 | private: |
| 53 | float mx{0}; |
| 54 | float my{0}; |
| 55 | }; |
| 56 |
| 57 | inline bool fuzzyCompare(const VPointF &p1, const VPointF &p2) |
| 58 | { |
| 59 | return (vCompare(p1.mx, p2.mx) && vCompare(p1.my, p2.my)); |
| 60 | } |
| 61 |
| 62 | inline VPointF operator-(const VPointF &p1, const VPointF &p2) |
| 63 | { |
| 64 | return {p1.mx - p2.mx, p1.my - p2.my}; |
| 65 | } |
| 66 |
| 67 | inline const VPointF operator*(const VPointF &p, float c) |
| 68 | { |
| 69 | return VPointF(p.mx * c, p.my * c); |
| 70 | } |
| 71 |
| 72 | inline const VPointF operator*(float c, const VPointF &p) |
| 73 | { |
| 74 | return VPointF(p.mx * c, p.my * c); |
| 75 | } |
| 76 |
| 77 | inline const VPointF operator/(const VPointF &p, float c) |
| 78 | { |
| 79 | return VPointF(p.mx / c, p.my / c); |
| 80 | } |
| 81 |
| 82 | inline const VPointF operator/(float c, const VPointF &p) |
| 83 | { |
| 84 | return VPointF(p.mx / c, p.my / c); |
| 85 | } |
| 86 |
| 87 | inline VDebug &operator<<(VDebug &os, const VPointF &o) |
| 88 | { |
| 89 | os << "{P " << o.x() << "," << o.y() << "}"; |
| 90 | return os; |
| 91 | } |
| 92 |
| 93 | inline VPointF &VPointF::operator+=(const VPointF &p) noexcept |
| 94 | { |
| 95 | mx += p.mx; |
| 96 | my += p.my; |
| 97 | return *this; |
| 98 | } |
| 99 |
| 100 | inline VPointF &VPointF::operator-=(const VPointF &p) noexcept |
| 101 | { |
| 102 | mx -= p.mx; |
| 103 | my -= p.my; |
| 104 | return *this; |
| 105 | } |
| 106 |
| 107 | class VPoint { |
| 108 | public: |
| 109 | VPoint() = default; |
| 110 | constexpr inline VPoint(int x, int y) noexcept : mx(x), my(y) {} |
| 111 | constexpr inline int x() const noexcept { return mx; } |
| 112 | constexpr inline int y() const noexcept { return my; } |
| 113 | inline void setX(int x) { mx = x; } |
| 114 | inline void setY(int y) { my = y; } |
| 115 | inline VPoint & operator+=(const VPoint &p) noexcept; |
| 116 | inline VPoint & operator-=(const VPoint &p) noexcept; |
| 117 | constexpr inline bool operator==(const VPoint &o) const; |
| 118 | constexpr inline bool operator!=(const VPoint &o) const |
| 119 | { |
| 120 | return !(operator==(o)); |
| 121 | } |
| 122 | friend inline VPoint operator-(const VPoint &p1, const VPoint &p2); |
| 123 | inline friend VDebug &operator<<(VDebug &os, const VPoint &o); |
| 124 |
| 125 | private: |
| 126 | int mx{0}; |
| 127 | int my{0}; |
| 128 | }; |
| 129 | inline VDebug &operator<<(VDebug &os, const VPoint &o) |
| 130 | { |
| 131 | os << "{P " << o.x() << "," << o.y() << "}"; |
| 132 | return os; |
| 133 | } |
| 134 |
| 135 | inline VPoint operator-(const VPoint &p1, const VPoint &p2) |
| 136 | { |
| 137 | return {p1.mx - p2.mx, p1.my - p2.my}; |
| 138 | } |
| 139 |
| 140 | constexpr inline bool VPoint::operator==(const VPoint &o) const |
| 141 | { |
| 142 | return (mx == o.x() && my == o.y()); |
| 143 | } |
| 144 |
| 145 | inline VPoint &VPoint::operator+=(const VPoint &p) noexcept |
| 146 | { |
| 147 | mx += p.mx; |
| 148 | my += p.my; |
| 149 | return *this; |
| 150 | } |
| 151 |
| 152 | inline VPoint &VPoint::operator-=(const VPoint &p) noexcept |
| 153 | { |
| 154 | mx -= p.mx; |
| 155 | my -= p.my; |
| 156 | return *this; |
| 157 | } |
| 158 |
| 159 | class VSize { |
| 160 | public: |
| 161 | VSize() = default; |
| 162 | constexpr inline VSize(int w, int h) noexcept : mw(w), mh(h) {} |
| 163 | bool empty() const {return (mw <= 0 || mh <= 0);} |
| 164 | constexpr inline int width() const noexcept { return mw; } |
| 165 | constexpr inline int height() const noexcept { return mh; } |
| 166 | inline void setWidth(int w) { mw = w; } |
| 167 | inline void setHeight(int h) { mh = h; } |
| 168 | inline VSize & operator+=(const VSize &p) noexcept; |
| 169 | inline VSize & operator-=(const VSize &p) noexcept; |
| 170 | constexpr inline bool operator==(const VSize &o) const; |
| 171 | constexpr inline bool operator!=(const VSize &o) const |
| 172 | { |
| 173 | return !(operator==(o)); |
| 174 | } |
| 175 | inline friend VDebug &operator<<(VDebug &os, const VSize &o); |
| 176 |
| 177 | private: |
| 178 | int mw{0}; |
| 179 | int mh{0}; |
| 180 | }; |
| 181 | inline VDebug &operator<<(VDebug &os, const VSize &o) |
| 182 | { |
| 183 | os << "{P " << o.width() << "," << o.height() << "}"; |
| 184 | return os; |
| 185 | } |
| 186 | constexpr inline bool VSize::operator==(const VSize &o) const |
| 187 | { |
| 188 | return (mw == o.width() && mh == o.height()); |
| 189 | } |
| 190 |
| 191 | inline VSize &VSize::operator+=(const VSize &p) noexcept |
| 192 | { |
| 193 | mw += p.mw; |
| 194 | mh += p.mh; |
| 195 | return *this; |
| 196 | } |
| 197 |
| 198 | inline VSize &VSize::operator-=(const VSize &p) noexcept |
| 199 | { |
| 200 | mw -= p.mw; |
| 201 | mh -= p.mh; |
| 202 | return *this; |
| 203 | } |
| 204 |
| 205 | V_END_NAMESPACE |
| 206 |
| 207 | #endif // VPOINT_H |
| 208 |