# Changeset 856

Ignore:
Timestamp:
03/28/19 22:13:25 (5 years ago)
Message:

Support for more operations in 2D

File:
1 edited

Unmodified
Removed
• ## cpp/common/2d.h

 r848 template XY operator*=(Q q) { x *= q; y *= q; return *this; } template XY operator/=(Q q) { x /= q; y /= q; return *this; } template XY operator/(Q q) { return XY(x / q, y / q); } template XY operator/(Q q) const { return XY(x / q, y / q); } template XY operator*(Q q) const { return XY(q*x, q*y); } void set(T _x, T _y) { x = _x; y = _y; } template const XYRect& operator=(const Q& other) { p = other.p; size = other.size; return *this; } T right() const {return p.x+size.x;} T bottom() const {return p.y+size.y;} T top() const {return p.y;} T left() const {return p.x;} XY center() const {return p+size/2;} const XY& topLeft() const {return p;} XY bottomRight() const {return p+size;} XY topRight() const {return XY(p.x+size.x,p.y);} XY bottomLeft() const {return XY(p.x,p.y+size.y);} T area() const { return size.x*size.y; } bool intersects(const XYRect& r) const { } bool contains(const XYRect& r) const { return contains(r.p) && contains(r.p+r.size); } void add(const XY& n) { } T distanceTo(const XYRect& r) const { bool r_above = (r.bottom() <= top()); bool r_below = (r.top() >= bottom()); bool r_left = (r.right() <= left()); bool r_right = (r.left() >= right()); if (r_above) { if (r_left) return r.bottomRight().distanceTo(topLeft()); else if (r_right) return r.bottomLeft().distanceTo(topRight()); else return top()-r.bottom(); } else if (r_below) { if (r_left) return r.topRight().distanceTo(bottomLeft()); else if (r_right) return r.topLeft().distanceTo(bottomRight()); else return r.top()-bottom(); } else if (r_left) { return left()-r.right(); } else if (r_right) { return r.left()-right(); } else return 0; //intersection } static const XYRect& zero() { static XYRect t(0, 0, 0, 0); return t; } static const XYRect& one() { static XYRect t(0, 0, 1, 1); return t; }
Note: See TracChangeset for help on using the changeset viewer.