Changeset 766
 Timestamp:
 03/29/18 22:51:30 (4 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

cpp/common/2d.h
r286 r766 18 18 template <typename Q> XY(const Q& other):x(other.x),y(other.y) {} 19 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);} 20 21 XY operator+(const XY&p) const {return XY(x+p.x,y+p.y);} 21 22 XY operator(const XY&p) const {return XY(xp.x,yp.y);} … … 49 50 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));} 50 51 51 typedef XY<int> IntXY; 52 template <typename T> 53 class XYMargin 54 { 55 public: 56 XYMargin(T x=0):left(x),top(x),right(x),bottom(x) {} 57 XYMargin(T l,T t,T r,T b):left(l),top(t),right(r),bottom(b) {} 58 T left,top,right,bottom; 59 void operator=(T x) {left=top=right=bottom=x;} 60 XYMargin operator() const {return XYMargin(left,top,right,bottom);} 61 void operator=(const XYMargin<T> &other) {left=other.left; top=other.top; right=other.right; bottom=other.bottom;} 62 T horizontal() const {return left+right;} 63 T vertical() const {return top+bottom;} 64 }; 52 65 53 66 template <typename T> … … 58 71 XYRect() {} 59 72 XYRect(const XY<T>& p1,const XY<T>& s):p(p1),size(s) {} 73 template <typename Q> XYRect(const Q& other):p(other.p),size(other.size) {} 60 74 XYRect(T _x,T _y,T _w,T _h):p(_x,_y),size(_w,_h) {} 61 75 static XYRect<T> centeredAt(const XY<T>& p,XY<T> s) {return XYRect<T>(ps*0.5,s);} … … 64 78 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));} 65 79 bool operator==(const XYRect& r) const {return (p==r.p) && (size==r.size);} 80 template <typename Q> const XYRect& operator=(const Q& other) {p=other.p; size=other.size; return *this;} 66 81 67 82 bool intersects(const XYRect& r) const … … 91 106 } 92 107 93 void extend(const XY<T>& border_size) 108 XYRect extendBy(const XY<T>& border_size) const 94 109 { 95 size+=border_size*2; p=border_size; 110 return XYRect(pborder_size,size+border_size*2); 111 } 112 113 XYRect shrinkBy(const XY<T>& border_size) const 114 { 115 return XYRect(p+border_size,sizeborder_size*2); 116 } 117 118 XYRect extendBy(const XYMargin<T>& m) const 119 { 120 return XYRect(p.xm.left,p.ym.top,size.x+m.horizontal(),size.y+m.vertical()); 121 } 122 123 XYRect shrinkBy(const XYMargin<T>& m) const 124 { 125 return XYRect(p.x+m.left,p.y+m.top,size.xm.horizontal(),size.ym.vertical()); 126 } 127 128 XYMargin<T> marginTowards(const XYRect &r) const 129 { 130 return XYMargin<T>(r.p.x, r.p.y, 131 (p.x+size.x)(r.p.x+r.size.x), (p.y+size.y)(r.p.y+r.size.y)); 96 132 } 97 133 … … 108 144 } 109 145 146 XYRect translation(const XY<T>& t) const 147 { 148 return XYRect(p+t,size); 149 } 150 110 151 T distanceTo(const XY<T>& n) const 111 152 { … … 120 161 }; 121 162 163 typedef XY<int> IntXY; 164 typedef XYRect<int> IntRect; 122 165 123 166 #endif
Note: See TracChangeset
for help on using the changeset viewer.