Changeset 909 for cpp/common/2d.h
- Timestamp:
- 02/04/20 19:36:58 (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
cpp/common/2d.h
r905 r909 1 1 // This file is a part of Framsticks SDK. http://www.framsticks.com/ 2 // Copyright (C) 1999-20 19Maciej Komosinski and Szymon Ulatowski.2 // Copyright (C) 1999-2020 Maciej Komosinski and Szymon Ulatowski. 3 3 // See LICENSE.txt for details. 4 4 … … 16 16 XY() {} 17 17 XY(T _x, T _y) :x(_x), y(_y) {} 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; }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; } 25 25 XY operator-() const { return XY(-x, -y); } 26 26 // allows float operations on ints … … 28 28 template <typename Q> XY operator/=(Q q) { x /= q; y /= q; return *this; } 29 29 template <typename Q> XY operator/(Q q) const { return XY(x / q, y / q); } 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); }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); } 35 35 void set(T _x, T _y) { x = _x; y = _y; } 36 36 void add(T _x, T _y) { x += _x; y += _y; } 37 37 void sub(T _x, T _y) { x -= _x; y -= _y; } 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); }40 T distanceTo(const XY & p) const { return sqrt(double((p.x - x)*(p.x - x) + (p.y - y)*(p.y - y))); }41 T magnitude() const { return sqrt(x *x + y * y); }42 T length() const { return sqrt(x *x + y * y); }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); } 40 T distanceTo(const XY &p) const { return sqrt(double((p.x - x) * (p.x - x) + (p.y - y) * (p.y - y))); } 41 T magnitude() const { return sqrt(x * x + y * y); } 42 T length() const { return sqrt(x * x + y * y); } 43 43 T lengthSq() const { return x * x + y * y; } 44 T dotProduct(const XY &v) const { return x * v.x + y * v.y; }45 T crossProduct(const XY &v) const { return x * v.y - y * v.x; }44 T dotProduct(const XY &v) const { return x * v.x + y * v.y; } 45 T crossProduct(const XY &v) const { return x * v.y - y * v.x; } 46 46 void normalize() { operator/=(length()); } // length becomes 1 47 static XY average(const XY & v1, const XY& v2) { return XY((v1.x + v2.x)*0.5, (v1.y + v2.y)*0.5); }47 static XY average(const XY &v1, const XY &v2) { return XY((v1.x + v2.x) * 0.5, (v1.y + v2.y) * 0.5); } 48 48 double getDirection() const { return atan2(y, x); } 49 static XY interpolate(const XY & v1, const XY& v2, double t) { return universal_lerp(v1,v2,t); }49 static XY interpolate(const XY &v1, const XY &v2, double t) { return universal_lerp(v1, v2, t); } 50 50 XY toInt() const { return XY(int(x), int(y)); } 51 51 XY transpose() const { return XY(y, x); } 52 static const XY &zero() { static XY t(0, 0); return t; }53 static const XY &one() { static XY t(1, 1); return t; }52 static const XY &zero() { static XY t(0, 0); return t; } 53 static const XY &one() { static XY t(1, 1); return t; } 54 54 }; 55 55 56 56 //specialized: int equality not using fabs() 57 template<> inline bool XY<int>::operator==(const XY<int> &p) const { return (x == p.x) && (y == p.y); }58 59 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)); }60 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)); }57 template<> inline bool XY<int>::operator==(const XY<int> &p) const { return (x == p.x) && (y == p.y); } 58 59 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)); } 60 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)); } 61 61 62 62 template <typename T> … … 82 82 XY<T> p, size; 83 83 XYRect() {} 84 XYRect(const XY<T> & p1, const XY<T>&s) :p(p1), size(s) {}85 template <typename Q> XYRect(const Q &other) : p(other.p), size(other.size) {}84 XYRect(const XY<T> &p1, const XY<T> &s) :p(p1), size(s) {} 85 template <typename Q> XYRect(const Q &other) : p(other.p), size(other.size) {} 86 86 XYRect(T _x, T _y, T _w, T _h) :p(_x, _y), size(_w, _h) {} 87 static XYRect<T> centeredAt(const XY<T> &p, XY<T> s) { return XYRect<T>(p - s * 0.5, s); }87 static XYRect<T> centeredAt(const XY<T> &p, XY<T> s) { return XYRect<T>(p - s * 0.5, s); } 88 88 89 89 bool isEmpty() const { return (size.x < 0) || (size.y < 0); } 90 90 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)); } 91 bool operator==(const XYRect &r) const { return (p == r.p) && (size == r.size); }92 template <typename Q> const XYRect & operator=(const Q&other) { p = other.p; size = other.size; return *this; }91 bool operator==(const XYRect &r) const { return (p == r.p) && (size == r.size); } 92 template <typename Q> const XYRect &operator=(const Q &other) { p = other.p; size = other.size; return *this; } 93 93 94 94 T right() const { return p.x + size.x; } … … 97 97 T left() const { return p.x; } 98 98 XY<T> center() const { return p + size / 2; } 99 const XY<T> &topLeft() const { return p; }99 const XY<T> &topLeft() const { return p; } 100 100 XY<T> bottomRight() const { return p + size; } 101 101 XY<T> topRight() const { return XY<T>(p.x + size.x, p.y); } 102 102 XY<T> bottomLeft() const { return XY<T>(p.x, p.y + size.y); } 103 103 104 T area() const { return size.x *size.y; }105 106 bool intersects(const XYRect &r) const104 T area() const { return size.x * size.y; } 105 106 bool intersects(const XYRect &r) const 107 107 { 108 108 if (r.p.x >= (p.x + size.x)) return false; … … 113 113 } 114 114 115 bool contains(const XY<T> &n) const115 bool contains(const XY<T> &n) const 116 116 { 117 117 if (n.x < p.x) return false; … … 122 122 } 123 123 124 bool contains(const XYRect &r) const124 bool contains(const XYRect &r) const 125 125 { 126 126 return contains(r.p) && contains(r.p + r.size); 127 127 } 128 128 129 void add(const XY<T> &n)129 void add(const XY<T> &n) 130 130 { 131 131 if (n.x < p.x) { size.x += p.x - n.x; p.x = n.x; } … … 135 135 } 136 136 137 XYRect extendBy(const XY<T> &border_size) const137 XYRect extendBy(const XY<T> &border_size) const 138 138 { 139 139 return XYRect(p - border_size, size + border_size * 2); 140 140 } 141 141 142 XYRect shrinkBy(const XY<T> &border_size) const142 XYRect shrinkBy(const XY<T> &border_size) const 143 143 { 144 144 return XYRect(p + border_size, size - border_size * 2); 145 145 } 146 146 147 XYRect extendBy(const XYMargin<T> &m) const147 XYRect extendBy(const XYMargin<T> &m) const 148 148 { 149 149 return XYRect(p.x - m.left, p.y - m.top, size.x + m.horizontal(), size.y + m.vertical()); 150 150 } 151 151 152 XYRect shrinkBy(const XYMargin<T> &m) const152 XYRect shrinkBy(const XYMargin<T> &m) const 153 153 { 154 154 return XYRect(p.x + m.left, p.y + m.top, size.x - m.horizontal(), size.y - m.vertical()); … … 164 164 { 165 165 XYRect r; 166 r.size =size;167 if (size.x < size.y *aspect)168 r.size.y = r.size.x /aspect;166 r.size = size; 167 if (size.x < size.y * aspect) 168 r.size.y = r.size.x / aspect; 169 169 else 170 r.size.x = r.size.y *aspect;171 r.p =p+(size-r.size)*0.5;172 return r; 173 } 174 175 XYRect intersection(const XYRect &r) const170 r.size.x = r.size.y * aspect; 171 r.p = p + (size - r.size) * 0.5; 172 return r; 173 } 174 175 XYRect intersection(const XYRect &r) const 176 176 { 177 177 XYRect i; … … 185 185 } 186 186 187 XYRect extensionContaining(const XYRect& r) const 187 XYRect extensionContaining(const XY<T> &p) const 188 { 189 XY<T> p1 = xymin(topLeft(), p); 190 XY<T> p2 = xymax(bottomRight(), p); 191 return XYRect(p1, p2 - p1); 192 } 193 194 XYRect extensionContaining(const XYRect &r) const 188 195 { 189 196 XY<T> p1 = xymin(topLeft(), r.topLeft()); … … 192 199 } 193 200 194 XYRect translation(const XY<T> &t) const201 XYRect translation(const XY<T> &t) const 195 202 { 196 203 return XYRect(p + t, size); 197 204 } 198 205 199 T distanceTo(const XY<T> &n) const206 T distanceTo(const XY<T> &n) const 200 207 { 201 208 XY<T> tp = n; … … 205 212 } 206 213 207 T distanceTo(const XYRect<T> &r) const214 T distanceTo(const XYRect<T> &r) const 208 215 { 209 216 bool r_above = (r.bottom() <= top()); … … 236 243 } 237 244 238 static const XYRect &zero() { static XYRect t(0, 0, 0, 0); return t; }239 static const XYRect &one() { static XYRect t(0, 0, 1, 1); return t; }245 static const XYRect &zero() { static XYRect t(0, 0, 0, 0); return t; } 246 static const XYRect &one() { static XYRect t(0, 0, 1, 1); return t; } 240 247 }; 241 248
Note: See TracChangeset
for help on using the changeset viewer.