[286] | 1 | // This file is a part of Framsticks SDK. http://www.framsticks.com/ |
---|
| 2 | // Copyright (C) 1999-2015 Maciej Komosinski and Szymon Ulatowski. |
---|
| 3 | // See LICENSE.txt for details. |
---|
[122] | 4 | |
---|
[109] | 5 | #ifndef _2D_H_ |
---|
| 6 | #define _2D_H_ |
---|
| 7 | |
---|
| 8 | #include "nonstd_stl.h" |
---|
| 9 | #include <math.h> |
---|
| 10 | |
---|
| 11 | //unifikacja starych GUIXY i Pt2D |
---|
| 12 | template <typename T> class XY |
---|
| 13 | { |
---|
| 14 | public: |
---|
| 15 | T x,y; |
---|
| 16 | XY() {} |
---|
| 17 | XY(T _x,T _y):x(_x),y(_y) {} |
---|
| 18 | template <typename Q> XY(const Q& other):x(other.x),y(other.y) {} |
---|
| 19 | template <typename Q> const XY& operator=(const Q& other) {x=other.x; y=other.y; return *this;} |
---|
| 20 | XY operator+(const XY&p) const {return XY(x+p.x,y+p.y);} |
---|
| 21 | XY operator-(const XY&p) const {return XY(x-p.x,y-p.y);} |
---|
| 22 | XY operator+=(const XY&p) {x+=p.x; y+=p.y; return *this;} |
---|
| 23 | XY operator-=(const XY&p) {x-=p.x; y-=p.y; return *this;} |
---|
| 24 | XY operator-() const {return XY(-x,-y);} |
---|
| 25 | XY operator*=(T q) {x*=q; y*=q; return *this;} |
---|
| 26 | XY operator/=(T q) {x/=q; y/=q; return *this;} |
---|
| 27 | XY operator/(T q) {return XY(x/q,y/q);} |
---|
| 28 | XY operator*(T q) const {return XY(q*x,q*y);} |
---|
| 29 | void set(T _x,T _y) {x=_x; y=_y;} |
---|
| 30 | void add(T _x,T _y) {x+=_x; y+=_y;} |
---|
| 31 | void sub(T _x,T _y) {x-=_x; y-=_y;} |
---|
| 32 | bool operator==(const XY& p) const {return (fabs(double(x-p.x))<1e-20)&&(fabs(double(y-p.y))<1e-20);} |
---|
| 33 | T distanceTo(const XY& p) const {return sqrt(double((p.x-x)*(p.x-x)+(p.y-y)*(p.y-y)));} |
---|
| 34 | T magnitude() const {return sqrt(x*x+y*y);} |
---|
| 35 | T length() const {return sqrt(x*x+y*y);} |
---|
| 36 | T lengthSq() const { return x*x + y*y; } |
---|
| 37 | T dotProduct(const XY& v) const {return x*v.x + y*v.y;} |
---|
| 38 | T crossProduct(const XY& v) const {return x*v.y - y*v.x;} |
---|
| 39 | void normalize() { operator/=(length()); } // length becomes 1 |
---|
| 40 | static XY average(const XY& v1,const XY& v2) { return XY((v1.x+v2.x)*0.5,(v1.y+v2.y)*0.5); } |
---|
| 41 | double getDirection() const {return atan2(y,x);} |
---|
| 42 | static XY interpolate(const XY& v1, const XY& v2,double t) {return v1+(v2-v1)*t;} |
---|
| 43 | XY toInt() const {return XY(int(x),int(y));} |
---|
| 44 | static const XY& zero() {static XY t(0,0); return t;} |
---|
| 45 | static const XY& one() {static XY t(1,1); return t;} |
---|
| 46 | }; |
---|
| 47 | |
---|
| 48 | template <typename T> XY<T> xymin(const XY<T>& a, const XY<T>& b) {return XY<T>(min(a.x,b.x),min(a.y,b.y));} |
---|
| 49 | 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 | typedef XY<int> IntXY; |
---|
| 52 | |
---|
| 53 | template <typename T> |
---|
| 54 | class XYRect |
---|
| 55 | { |
---|
| 56 | public: |
---|
| 57 | XY<T> p,size; |
---|
| 58 | XYRect() {} |
---|
| 59 | XYRect(const XY<T>& p1,const XY<T>& s):p(p1),size(s) {} |
---|
| 60 | XYRect(T _x,T _y,T _w,T _h):p(_x,_y),size(_w,_h) {} |
---|
| 61 | static XYRect<T> centeredAt(const XY<T>& p,XY<T> s) {return XYRect<T>(p-s*0.5,s);} |
---|
| 62 | |
---|
| 63 | bool isEmpty() const {return (size.x<0)||(size.y<0);} |
---|
| 64 | 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 | bool operator==(const XYRect& r) const {return (p==r.p) && (size==r.size);} |
---|
| 66 | |
---|
| 67 | bool intersects(const XYRect& r) const |
---|
| 68 | { |
---|
| 69 | if (r.p.x >= (p.x+size.x)) return false; |
---|
| 70 | if (r.p.y >= (p.y+size.y)) return false; |
---|
| 71 | if ((r.p.x+r.size.x) <= p.x) return false; |
---|
| 72 | if ((r.p.y+r.size.y) <= p.y) return false; |
---|
| 73 | return true; |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | bool contains(const XY<T>& n) const |
---|
| 77 | { |
---|
| 78 | if (n.x<p.x) return false; |
---|
| 79 | if (n.x>(p.x+size.x)) return false; |
---|
| 80 | if (n.y<p.y) return false; |
---|
| 81 | if (n.y>(p.y+size.y)) return false; |
---|
| 82 | return true; |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | void add(const XY<T>& n) |
---|
| 86 | { |
---|
| 87 | if (n.x<p.x) {size.x+=p.x-n.x; p.x=n.x;} |
---|
| 88 | else if (n.x>(p.x+size.x)) size.x=n.x-p.x; |
---|
| 89 | if (n.y<p.y) {size.y+=p.y-n.y; p.y=n.y;} |
---|
| 90 | else if (n.y>(p.y+size.y)) size.y=n.y-p.y; |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | void extend(const XY<T>& border_size) |
---|
| 94 | { |
---|
| 95 | size+=border_size*2; p-=border_size; |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | XYRect intersection(const XYRect& r) const |
---|
| 99 | { |
---|
| 100 | XYRect i; |
---|
| 101 | XY<T> p2=p+size; |
---|
| 102 | XY<T> rp2=r.p+r.size; |
---|
| 103 | i.p.x=max(p.x,r.p.x); |
---|
| 104 | i.p.y=max(p.y,r.p.y); |
---|
| 105 | i.size.x=min(p2.x,rp2.x)-i.p.x; |
---|
| 106 | i.size.y=min(p2.y,rp2.y)-i.p.y; |
---|
| 107 | return i; |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | T distanceTo(const XY<T>& n) const |
---|
| 111 | { |
---|
| 112 | XY<T> tp=n; |
---|
| 113 | if (n.x<p.x) tp.x=p.x; else if (n.x>=(p.x+size.x)) tp.x=p.x+size.x; |
---|
| 114 | if (n.y<p.y) tp.y=p.y; else if (n.y>=(p.y+size.y)) tp.y=p.y+size.y; |
---|
| 115 | return tp.distanceTo(n); |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | static const XYRect& zero() {static XYRect t(0,0,0,0); return t;} |
---|
| 119 | static const XYRect& one() {static XYRect t(0,0,1,1); return t;} |
---|
| 120 | }; |
---|
| 121 | |
---|
| 122 | |
---|
| 123 | #endif |
---|