[286] | 1 | // This file is a part of Framsticks SDK. http://www.framsticks.com/ |
---|
[909] | 2 | // Copyright (C) 1999-2020 Maciej Komosinski and Szymon Ulatowski. |
---|
[286] | 3 | // See LICENSE.txt for details. |
---|
[122] | 4 | |
---|
[109] | 5 | #ifndef _2D_H_ |
---|
| 6 | #define _2D_H_ |
---|
| 7 | |
---|
| 8 | #include "nonstd_stl.h" |
---|
| 9 | #include <math.h> |
---|
| 10 | |
---|
[848] | 11 | //unification of old GUIXY and Pt2D |
---|
[109] | 12 | template <typename T> class XY |
---|
| 13 | { |
---|
| 14 | public: |
---|
[848] | 15 | T x, y; |
---|
| 16 | XY() {} |
---|
| 17 | XY(T _x, T _y) :x(_x), y(_y) {} |
---|
[909] | 18 | template <typename Q> XY(const Q &other) : x(other.x), y(other.y) {} |
---|
| 19 | template <typename Q> const XY &operator=(const Q &other) { x = other.x; y = other.y; return *this; } |
---|
| 20 | template <typename Q> const XY operator()(const Q &other) { return XY(other.x, other.y); } |
---|
| 21 | XY operator+(const XY &p) const { return XY(x + p.x, y + p.y); } |
---|
| 22 | XY operator-(const XY &p) const { return XY(x - p.x, y - p.y); } |
---|
| 23 | XY operator+=(const XY &p) { x += p.x; y += p.y; return *this; } |
---|
| 24 | XY operator-=(const XY &p) { x -= p.x; y -= p.y; return *this; } |
---|
[848] | 25 | XY operator-() const { return XY(-x, -y); } |
---|
| 26 | // allows float operations on ints |
---|
| 27 | template <typename Q> XY operator*=(Q q) { x *= q; y *= q; return *this; } |
---|
| 28 | template <typename Q> XY operator/=(Q q) { x /= q; y /= q; return *this; } |
---|
[856] | 29 | template <typename Q> XY operator/(Q q) const { return XY(x / q, y / q); } |
---|
[909] | 30 | template <typename Q> XY operator*(Q q) const { return XY(q * x, q * y); } |
---|
| 31 | XY operator*=(const XY &q) { x *= q.x; y *= q.y; return *this; } |
---|
| 32 | XY operator/=(const XY &q) { x /= q.x; y /= q.y; return *this; } |
---|
| 33 | XY operator*(const XY &q) const { return XY(x * q.x, y * q.y); } |
---|
| 34 | XY operator/(const XY &q) const { return XY(x / q.x, y / q.y); } |
---|
[848] | 35 | void set(T _x, T _y) { x = _x; y = _y; } |
---|
| 36 | void add(T _x, T _y) { x += _x; y += _y; } |
---|
| 37 | void sub(T _x, T _y) { x -= _x; y -= _y; } |
---|
[909] | 38 | bool operator==(const XY &p) const { return (fabs(double(x - p.x)) < 1e-20) && (fabs(double(y - p.y)) < 1e-20); } |
---|
| 39 | bool operator!=(const XY &p) const { return !operator==(p); } |
---|
[1028] | 40 | T distanceTo(const XY &p) const { return sqrt(distanceToSq(p)); } |
---|
| 41 | T distanceToSq(const XY &p) const { return double((p.x - x) * (p.x - x) + (p.y - y) * (p.y - y)); } |
---|
[909] | 42 | T magnitude() const { return sqrt(x * x + y * y); } |
---|
| 43 | T length() const { return sqrt(x * x + y * y); } |
---|
[848] | 44 | T lengthSq() const { return x * x + y * y; } |
---|
[909] | 45 | T dotProduct(const XY &v) const { return x * v.x + y * v.y; } |
---|
| 46 | T crossProduct(const XY &v) const { return x * v.y - y * v.x; } |
---|
[848] | 47 | void normalize() { operator/=(length()); } // length becomes 1 |
---|
[909] | 48 | static XY average(const XY &v1, const XY &v2) { return XY((v1.x + v2.x) * 0.5, (v1.y + v2.y) * 0.5); } |
---|
[848] | 49 | double getDirection() const { return atan2(y, x); } |
---|
[909] | 50 | static XY interpolate(const XY &v1, const XY &v2, double t) { return universal_lerp(v1, v2, t); } |
---|
[848] | 51 | XY toInt() const { return XY(int(x), int(y)); } |
---|
| 52 | XY transpose() const { return XY(y, x); } |
---|
[909] | 53 | static const XY &zero() { static XY t(0, 0); return t; } |
---|
| 54 | static const XY &one() { static XY t(1, 1); return t; } |
---|
[109] | 55 | }; |
---|
| 56 | |
---|
[848] | 57 | //specialized: int equality not using fabs() |
---|
[909] | 58 | template<> inline bool XY<int>::operator==(const XY<int> &p) const { return (x == p.x) && (y == p.y); } |
---|
[109] | 59 | |
---|
[909] | 60 | template <typename T> XY<T> xymin(const XY<T> &a, const XY<T> &b) { return XY<T>(min(a.x, b.x), min(a.y, b.y)); } |
---|
| 61 | template <typename T> XY<T> xymax(const XY<T> &a, const XY<T> &b) { return XY<T>(max(a.x, b.x), max(a.y, b.y)); } |
---|
[848] | 62 | |
---|
[766] | 63 | template <typename T> |
---|
| 64 | class XYMargin |
---|
| 65 | { |
---|
[848] | 66 | public: |
---|
| 67 | XYMargin(T x = 0) :left(x), top(x), right(x), bottom(x) {} |
---|
| 68 | XYMargin(T l, T t, T r, T b) :left(l), top(t), right(r), bottom(b) {} |
---|
| 69 | T left, top, right, bottom; |
---|
| 70 | void operator=(T x) { left = top = right = bottom = x; } |
---|
| 71 | XYMargin operator-() const { return XYMargin(-left, -top, -right, -bottom); } |
---|
| 72 | void operator=(const XYMargin<T> &other) { left = other.left; top = other.top; right = other.right; bottom = other.bottom; } |
---|
| 73 | T horizontal() const { return left + right; } |
---|
| 74 | T vertical() const { return top + bottom; } |
---|
| 75 | bool operator==(const XYMargin &other) const { return left == other.left && top == other.top && right == other.right && bottom == other.bottom; } |
---|
| 76 | XYMargin normalized() const { return XYMargin(max(left, T(0)), max(top, T(0)), max(right, T(0)), max(bottom, T(0))); } |
---|
[766] | 77 | }; |
---|
[109] | 78 | |
---|
| 79 | template <typename T> |
---|
| 80 | class XYRect |
---|
| 81 | { |
---|
| 82 | public: |
---|
[848] | 83 | XY<T> p, size; |
---|
| 84 | XYRect() {} |
---|
[909] | 85 | XYRect(const XY<T> &p1, const XY<T> &s) :p(p1), size(s) {} |
---|
| 86 | template <typename Q> XYRect(const Q &other) : p(other.p), size(other.size) {} |
---|
[848] | 87 | XYRect(T _x, T _y, T _w, T _h) :p(_x, _y), size(_w, _h) {} |
---|
[909] | 88 | static XYRect<T> centeredAt(const XY<T> &p, XY<T> s) { return XYRect<T>(p - s * 0.5, s); } |
---|
[109] | 89 | |
---|
[848] | 90 | bool isEmpty() const { return (size.x < 0) || (size.y < 0); } |
---|
| 91 | XYRect toInt() const { return XYRect(int(p.x), int(p.y), int(p.x + size.x) - int(p.x), int(p.y + size.y) - int(p.y)); } |
---|
[909] | 92 | bool operator==(const XYRect &r) const { return (p == r.p) && (size == r.size); } |
---|
| 93 | template <typename Q> const XYRect &operator=(const Q &other) { p = other.p; size = other.size; return *this; } |
---|
[109] | 94 | |
---|
[866] | 95 | T right() const { return p.x + size.x; } |
---|
| 96 | T bottom() const { return p.y + size.y; } |
---|
| 97 | T top() const { return p.y; } |
---|
| 98 | T left() const { return p.x; } |
---|
| 99 | XY<T> center() const { return p + size / 2; } |
---|
[909] | 100 | const XY<T> &topLeft() const { return p; } |
---|
[866] | 101 | XY<T> bottomRight() const { return p + size; } |
---|
| 102 | XY<T> topRight() const { return XY<T>(p.x + size.x, p.y); } |
---|
| 103 | XY<T> bottomLeft() const { return XY<T>(p.x, p.y + size.y); } |
---|
| 104 | |
---|
[909] | 105 | T area() const { return size.x * size.y; } |
---|
[856] | 106 | |
---|
[909] | 107 | bool intersects(const XYRect &r) const |
---|
[848] | 108 | { |
---|
| 109 | if (r.p.x >= (p.x + size.x)) return false; |
---|
| 110 | if (r.p.y >= (p.y + size.y)) return false; |
---|
| 111 | if ((r.p.x + r.size.x) <= p.x) return false; |
---|
| 112 | if ((r.p.y + r.size.y) <= p.y) return false; |
---|
| 113 | return true; |
---|
| 114 | } |
---|
[109] | 115 | |
---|
[909] | 116 | bool contains(const XY<T> &n) const |
---|
[848] | 117 | { |
---|
| 118 | if (n.x < p.x) return false; |
---|
| 119 | if (n.x > (p.x + size.x)) return false; |
---|
| 120 | if (n.y < p.y) return false; |
---|
| 121 | if (n.y > (p.y + size.y)) return false; |
---|
| 122 | return true; |
---|
| 123 | } |
---|
[109] | 124 | |
---|
[909] | 125 | bool contains(const XYRect &r) const |
---|
[856] | 126 | { |
---|
[866] | 127 | return contains(r.p) && contains(r.p + r.size); |
---|
[856] | 128 | } |
---|
[866] | 129 | |
---|
[909] | 130 | void add(const XY<T> &n) |
---|
[848] | 131 | { |
---|
| 132 | if (n.x < p.x) { size.x += p.x - n.x; p.x = n.x; } |
---|
| 133 | else if (n.x > (p.x + size.x)) size.x = n.x - p.x; |
---|
| 134 | if (n.y < p.y) { size.y += p.y - n.y; p.y = n.y; } |
---|
| 135 | else if (n.y > (p.y + size.y)) size.y = n.y - p.y; |
---|
| 136 | } |
---|
[109] | 137 | |
---|
[909] | 138 | XYRect extendBy(const XY<T> &border_size) const |
---|
[848] | 139 | { |
---|
| 140 | return XYRect(p - border_size, size + border_size * 2); |
---|
| 141 | } |
---|
[109] | 142 | |
---|
[909] | 143 | XYRect shrinkBy(const XY<T> &border_size) const |
---|
[848] | 144 | { |
---|
| 145 | return XYRect(p + border_size, size - border_size * 2); |
---|
| 146 | } |
---|
[766] | 147 | |
---|
[909] | 148 | XYRect extendBy(const XYMargin<T> &m) const |
---|
[848] | 149 | { |
---|
| 150 | return XYRect(p.x - m.left, p.y - m.top, size.x + m.horizontal(), size.y + m.vertical()); |
---|
| 151 | } |
---|
[766] | 152 | |
---|
[909] | 153 | XYRect shrinkBy(const XYMargin<T> &m) const |
---|
[848] | 154 | { |
---|
| 155 | return XYRect(p.x + m.left, p.y + m.top, size.x - m.horizontal(), size.y - m.vertical()); |
---|
| 156 | } |
---|
[766] | 157 | |
---|
[848] | 158 | XYMargin<T> marginTowards(const XYRect &r) const |
---|
| 159 | { |
---|
| 160 | return XYMargin<T>(r.p.x - p.x, r.p.y - p.y, |
---|
| 161 | (p.x + size.x) - (r.p.x + r.size.x), (p.y + size.y) - (r.p.y + r.size.y)); |
---|
| 162 | } |
---|
[766] | 163 | |
---|
[905] | 164 | XYRect fitAspect(float aspect) ///< place a new rectangle having 'aspect' inside the rectangle |
---|
| 165 | { |
---|
| 166 | XYRect r; |
---|
[909] | 167 | r.size = size; |
---|
| 168 | if (size.x < size.y * aspect) |
---|
| 169 | r.size.y = r.size.x / aspect; |
---|
[905] | 170 | else |
---|
[909] | 171 | r.size.x = r.size.y * aspect; |
---|
| 172 | r.p = p + (size - r.size) * 0.5; |
---|
| 173 | return r; |
---|
[905] | 174 | } |
---|
[909] | 175 | |
---|
| 176 | XYRect intersection(const XYRect &r) const |
---|
[848] | 177 | { |
---|
| 178 | XYRect i; |
---|
| 179 | XY<T> p2 = p + size; |
---|
| 180 | XY<T> rp2 = r.p + r.size; |
---|
| 181 | i.p.x = max(p.x, r.p.x); |
---|
| 182 | i.p.y = max(p.y, r.p.y); |
---|
| 183 | i.size.x = min(p2.x, rp2.x) - i.p.x; |
---|
| 184 | i.size.y = min(p2.y, rp2.y) - i.p.y; |
---|
| 185 | return i; |
---|
| 186 | } |
---|
[109] | 187 | |
---|
[909] | 188 | XYRect extensionContaining(const XY<T> &p) const |
---|
[885] | 189 | { |
---|
[909] | 190 | XY<T> p1 = xymin(topLeft(), p); |
---|
| 191 | XY<T> p2 = xymax(bottomRight(), p); |
---|
| 192 | return XYRect(p1, p2 - p1); |
---|
| 193 | } |
---|
| 194 | |
---|
| 195 | XYRect extensionContaining(const XYRect &r) const |
---|
| 196 | { |
---|
[885] | 197 | XY<T> p1 = xymin(topLeft(), r.topLeft()); |
---|
| 198 | XY<T> p2 = xymax(bottomRight(), r.bottomRight()); |
---|
| 199 | return XYRect(p1, p2 - p1); |
---|
| 200 | } |
---|
| 201 | |
---|
[909] | 202 | XYRect translation(const XY<T> &t) const |
---|
[848] | 203 | { |
---|
| 204 | return XYRect(p + t, size); |
---|
| 205 | } |
---|
[766] | 206 | |
---|
[909] | 207 | T distanceTo(const XY<T> &n) const |
---|
[848] | 208 | { |
---|
| 209 | XY<T> tp = n; |
---|
| 210 | if (n.x < p.x) tp.x = p.x; else if (n.x >= (p.x + size.x)) tp.x = p.x + size.x; |
---|
| 211 | if (n.y < p.y) tp.y = p.y; else if (n.y >= (p.y + size.y)) tp.y = p.y + size.y; |
---|
| 212 | return tp.distanceTo(n); |
---|
| 213 | } |
---|
[109] | 214 | |
---|
[909] | 215 | T distanceTo(const XYRect<T> &r) const |
---|
[856] | 216 | { |
---|
[866] | 217 | bool r_above = (r.bottom() <= top()); |
---|
| 218 | bool r_below = (r.top() >= bottom()); |
---|
| 219 | bool r_left = (r.right() <= left()); |
---|
| 220 | bool r_right = (r.left() >= right()); |
---|
[856] | 221 | |
---|
[866] | 222 | if (r_above) |
---|
[856] | 223 | { |
---|
[866] | 224 | if (r_left) return r.bottomRight().distanceTo(topLeft()); |
---|
| 225 | else if (r_right) return r.bottomLeft().distanceTo(topRight()); |
---|
| 226 | else return top() - r.bottom(); |
---|
[856] | 227 | } |
---|
[866] | 228 | else if (r_below) |
---|
[856] | 229 | { |
---|
[866] | 230 | if (r_left) return r.topRight().distanceTo(bottomLeft()); |
---|
| 231 | else if (r_right) return r.topLeft().distanceTo(bottomRight()); |
---|
| 232 | else return r.top() - bottom(); |
---|
[856] | 233 | } |
---|
[866] | 234 | else if (r_left) |
---|
[856] | 235 | { |
---|
[866] | 236 | return left() - r.right(); |
---|
[856] | 237 | } |
---|
[866] | 238 | else if (r_right) |
---|
[856] | 239 | { |
---|
[866] | 240 | return r.left() - right(); |
---|
[856] | 241 | } |
---|
[866] | 242 | else |
---|
| 243 | return 0; //intersection |
---|
[856] | 244 | } |
---|
| 245 | |
---|
[909] | 246 | static const XYRect &zero() { static XYRect t(0, 0, 0, 0); return t; } |
---|
| 247 | static const XYRect &one() { static XYRect t(0, 0, 1, 1); return t; } |
---|
[109] | 248 | }; |
---|
| 249 | |
---|
[766] | 250 | typedef XY<int> IntXY; |
---|
| 251 | typedef XYRect<int> IntRect; |
---|
[109] | 252 | |
---|
[866] | 253 | typedef XY<float> FloatXY; |
---|
| 254 | typedef XYRect<float> FloatRect; |
---|
| 255 | |
---|
[109] | 256 | #endif |
---|