# Changeset 766

Ignore:
Timestamp:
03/29/18 22:51:30 (5 years ago)
Message:

Added a number of useful methods in 2D

File:
1 edited

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

 r286 template XY(const Q& other):x(other.x),y(other.y) {} template const XY& operator=(const Q& other) {x=other.x; y=other.y; return *this;} template const XY operator()(const Q& other) {return XY(other.x,other.y);} XY operator+(const XY&p) const  {return XY(x+p.x,y+p.y);} XY operator-(const XY&p) const {return XY(x-p.x,y-p.y);} template XY xymax(const XY& a, const XY& b) {return XY(max(a.x,b.x),max(a.y,b.y));} typedef XY IntXY; template class XYMargin { public: XYMargin(T x=0):left(x),top(x),right(x),bottom(x) {} XYMargin(T l,T t,T r,T b):left(l),top(t),right(r),bottom(b) {} T left,top,right,bottom; void operator=(T x) {left=top=right=bottom=x;} XYMargin operator-() const {return XYMargin(-left,-top,-right,-bottom);} void operator=(const XYMargin &other) {left=other.left; top=other.top; right=other.right; bottom=other.bottom;} T horizontal() const {return left+right;} T vertical() const {return top+bottom;} }; template XYRect() {} XYRect(const XY& p1,const XY& s):p(p1),size(s) {} template XYRect(const Q& other):p(other.p),size(other.size) {} XYRect(T _x,T _y,T _w,T _h):p(_x,_y),size(_w,_h) {} static XYRect centeredAt(const XY& p,XY s) {return XYRect(p-s*0.5,s);} 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));} bool operator==(const XYRect& r) const {return (p==r.p) && (size==r.size);} template const XYRect& operator=(const Q& other) {p=other.p; size=other.size; return *this;} bool intersects(const XYRect& r) const } void extend(const XY& border_size) XYRect extendBy(const XY& border_size) const { size+=border_size*2; p-=border_size; return XYRect(p-border_size,size+border_size*2); } XYRect shrinkBy(const XY& border_size) const { return XYRect(p+border_size,size-border_size*2); } XYRect extendBy(const XYMargin& m) const { return XYRect(p.x-m.left,p.y-m.top,size.x+m.horizontal(),size.y+m.vertical()); } XYRect shrinkBy(const XYMargin& m) const { return XYRect(p.x+m.left,p.y+m.top,size.x-m.horizontal(),size.y-m.vertical()); } XYMargin marginTowards(const XYRect &r) const { return XYMargin(r.p.x, r.p.y, (p.x+size.x)-(r.p.x+r.size.x), (p.y+size.y)-(r.p.y+r.size.y)); } } XYRect translation(const XY& t) const { return XYRect(p+t,size); } T distanceTo(const XY& n) const { }; typedef XY IntXY; typedef XYRect IntRect; #endif
Note: See TracChangeset for help on using the changeset viewer.