source: cpp/common/2d.h @ 120

Last change on this file since 120 was 109, checked in by sz, 11 years ago

source reorganization (see README)
new feature added: part/joint shapes (see frams/_demos/part_shapes.cpp)

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