Changeset 766 for cpp


Ignore:
Timestamp:
03/29/18 22:51:30 (7 years ago)
Author:
Maciej Komosinski
Message:

Added a number of useful methods in 2D

File:
1 edited

Legend:

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

    r286 r766  
    1818template <typename Q> XY(const Q& other):x(other.x),y(other.y) {}
    1919template <typename Q> const XY& operator=(const Q& other) {x=other.x; y=other.y; return *this;}
     20template <typename Q> const XY operator()(const Q& other) {return XY(other.x,other.y);}
    2021XY operator+(const XY&p) const  {return XY(x+p.x,y+p.y);}
    2122XY operator-(const XY&p) const {return XY(x-p.x,y-p.y);}
     
    4950template <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));}
    5051
    51 typedef XY<int> IntXY;
     52template <typename T>
     53class 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};
    5265
    5366template <typename T>
     
    5871XYRect() {}
    5972XYRect(const XY<T>& p1,const XY<T>& s):p(p1),size(s) {}
     73template <typename Q> XYRect(const Q& other):p(other.p),size(other.size) {}
    6074XYRect(T _x,T _y,T _w,T _h):p(_x,_y),size(_w,_h) {}
    6175static XYRect<T> centeredAt(const XY<T>& p,XY<T> s) {return XYRect<T>(p-s*0.5,s);}
     
    6478XYRect 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));}
    6579bool operator==(const XYRect& r) const {return (p==r.p) && (size==r.size);}
     80template <typename Q> const XYRect& operator=(const Q& other) {p=other.p; size=other.size; return *this;}
    6681
    6782bool intersects(const XYRect& r) const
     
    91106}
    92107
    93 void extend(const XY<T>& border_size)
     108XYRect extendBy(const XY<T>& border_size) const
    94109{
    95 size+=border_size*2; p-=border_size;
     110return XYRect(p-border_size,size+border_size*2);
     111}
     112
     113XYRect shrinkBy(const XY<T>& border_size) const
     114{
     115return XYRect(p+border_size,size-border_size*2);
     116}
     117
     118XYRect extendBy(const XYMargin<T>& m) const
     119{
     120return XYRect(p.x-m.left,p.y-m.top,size.x+m.horizontal(),size.y+m.vertical());
     121}
     122
     123XYRect shrinkBy(const XYMargin<T>& m) const
     124{
     125return XYRect(p.x+m.left,p.y+m.top,size.x-m.horizontal(),size.y-m.vertical());
     126}
     127
     128XYMargin<T> marginTowards(const XYRect &r) const
     129{
     130return 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));
    96132}
    97133
     
    108144}
    109145
     146XYRect translation(const XY<T>& t) const
     147{
     148return XYRect(p+t,size);
     149}
     150
    110151T distanceTo(const XY<T>& n) const
    111152{
     
    120161};
    121162
     163typedef XY<int> IntXY;
     164typedef XYRect<int> IntRect;
    122165
    123166#endif
Note: See TracChangeset for help on using the changeset viewer.