Changeset 856 for cpp/common/2d.h
- Timestamp:
- 03/28/19 22:13:25 (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
cpp/common/2d.h
r848 r856 27 27 template <typename Q> XY operator*=(Q q) { x *= q; y *= q; return *this; } 28 28 template <typename Q> XY operator/=(Q q) { x /= q; y /= q; return *this; } 29 template <typename Q> XY operator/(Q q) { return XY(x / q, y / q); }29 template <typename Q> XY operator/(Q q) const { return XY(x / q, y / q); } 30 30 template <typename Q> XY operator*(Q q) const { return XY(q*x, q*y); } 31 31 void set(T _x, T _y) { x = _x; y = _y; } … … 88 88 template <typename Q> const XYRect& operator=(const Q& other) { p = other.p; size = other.size; return *this; } 89 89 90 T right() const {return p.x+size.x;} 91 T bottom() const {return p.y+size.y;} 92 T top() const {return p.y;} 93 T left() const {return p.x;} 94 XY<T> center() const {return p+size/2;} 95 const XY<T>& topLeft() const {return p;} 96 XY<T> bottomRight() const {return p+size;} 97 XY<T> topRight() const {return XY<T>(p.x+size.x,p.y);} 98 XY<T> bottomLeft() const {return XY<T>(p.x,p.y+size.y);} 99 100 T area() const { return size.x*size.y; } 101 90 102 bool intersects(const XYRect& r) const 91 103 { … … 106 118 } 107 119 120 bool contains(const XYRect& r) const 121 { 122 return contains(r.p) && contains(r.p+r.size); 123 } 124 108 125 void add(const XY<T>& n) 109 126 { … … 165 182 } 166 183 184 T distanceTo(const XYRect<T>& r) const 185 { 186 bool r_above = (r.bottom() <= top()); 187 bool r_below = (r.top() >= bottom()); 188 bool r_left = (r.right() <= left()); 189 bool r_right = (r.left() >= right()); 190 191 if (r_above) 192 { 193 if (r_left) return r.bottomRight().distanceTo(topLeft()); 194 else if (r_right) return r.bottomLeft().distanceTo(topRight()); 195 else return top()-r.bottom(); 196 } 197 else if (r_below) 198 { 199 if (r_left) return r.topRight().distanceTo(bottomLeft()); 200 else if (r_right) return r.topLeft().distanceTo(bottomRight()); 201 else return r.top()-bottom(); 202 } 203 else if (r_left) 204 { 205 return left()-r.right(); 206 } 207 else if (r_right) 208 { 209 return r.left()-right(); 210 } 211 else 212 return 0; //intersection 213 } 214 167 215 static const XYRect& zero() { static XYRect t(0, 0, 0, 0); return t; } 168 216 static const XYRect& one() { static XYRect t(0, 0, 1, 1); return t; }
Note: See TracChangeset
for help on using the changeset viewer.