# source:cpp/common/2d.h@766

Last change on this file since 766 was 766, checked in by Maciej Komosinski, 4 years ago

Added a number of useful methods in 2D

• Property svn:eol-style set to `native`
File size: 5.3 KB
Line
1// This file is a part of Framsticks SDK.  http://www.framsticks.com/
2// Copyright (C) 1999-2015  Maciej Komosinski and Szymon Ulatowski.
4
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
12template <typename T> class XY
13{
14public:
15T x,y;
16XY() {}
17XY(T _x,T _y):x(_x),y(_y) {}
18template <typename Q> XY(const Q& other):x(other.x),y(other.y) {}
19template <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);}
21XY operator+(const XY&p) const  {return XY(x+p.x,y+p.y);}
22XY operator-(const XY&p) const {return XY(x-p.x,y-p.y);}
23XY operator+=(const XY&p) {x+=p.x; y+=p.y; return *this;}
24XY operator-=(const XY&p) {x-=p.x; y-=p.y; return *this;}
25XY operator-() const {return XY(-x,-y);}
26XY operator*=(T q) {x*=q; y*=q; return *this;}
27XY operator/=(T q) {x/=q; y/=q; return *this;}
28XY operator/(T q) {return XY(x/q,y/q);}
29XY operator*(T q) const  {return XY(q*x,q*y);}
30void set(T _x,T _y) {x=_x; y=_y;}
31void add(T _x,T _y) {x+=_x; y+=_y;}
32void sub(T _x,T _y) {x-=_x; y-=_y;}
33bool operator==(const XY& p) const {return (fabs(double(x-p.x))<1e-20)&&(fabs(double(y-p.y))<1e-20);}
34T distanceTo(const XY& p) const {return sqrt(double((p.x-x)*(p.x-x)+(p.y-y)*(p.y-y)));}
35T magnitude() const {return sqrt(x*x+y*y);}
36T length() const {return sqrt(x*x+y*y);}
37T lengthSq() const { return x*x + y*y; }
38T dotProduct(const XY& v) const {return x*v.x + y*v.y;}
39T crossProduct(const XY& v) const {return x*v.y - y*v.x;}
40void normalize() { operator/=(length()); } // length becomes 1
41static XY average(const XY& v1,const XY& v2) { return XY((v1.x+v2.x)*0.5,(v1.y+v2.y)*0.5); }
42double getDirection() const {return atan2(y,x);}
43static XY interpolate(const XY& v1, const XY& v2,double t) {return v1+(v2-v1)*t;}
44XY toInt() const {return XY(int(x),int(y));}
45static const XY& zero() {static XY t(0,0); return t;}
46static const XY& one() {static XY t(1,1); return t;}
47};
48
49template <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));}
50template <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));}
51
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;}
64};
65
66template <typename T>
67class XYRect
68{
69public:
70XY<T> p,size;
71XYRect() {}
72XYRect(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) {}
74XYRect(T _x,T _y,T _w,T _h):p(_x,_y),size(_w,_h) {}
75static XYRect<T> centeredAt(const XY<T>& p,XY<T> s) {return XYRect<T>(p-s*0.5,s);}
76
77bool isEmpty() const {return (size.x<0)||(size.y<0);}
78XYRect 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));}
79bool 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;}
81
82bool intersects(const XYRect& r) const
83{
84if (r.p.x >= (p.x+size.x)) return false;
85if (r.p.y >= (p.y+size.y)) return false;
86if ((r.p.x+r.size.x) <= p.x) return false;
87if ((r.p.y+r.size.y) <= p.y) return false;
88return true;
89}
90
91bool contains(const XY<T>& n) const
92{
93if (n.x<p.x) return false;
94if (n.x>(p.x+size.x)) return false;
95if (n.y<p.y) return false;
96if (n.y>(p.y+size.y)) return false;
97return true;
98}
99
101{
102if (n.x<p.x) {size.x+=p.x-n.x; p.x=n.x;}
103else if (n.x>(p.x+size.x)) size.x=n.x-p.x;
104if (n.y<p.y) {size.y+=p.y-n.y; p.y=n.y;}
105else if (n.y>(p.y+size.y)) size.y=n.y-p.y;
106}
107
108XYRect extendBy(const XY<T>& border_size) const
109{
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));
132}
133
134XYRect intersection(const XYRect& r) const
135{
136XYRect i;
137XY<T> p2=p+size;
138XY<T> rp2=r.p+r.size;
139i.p.x=max(p.x,r.p.x);
140i.p.y=max(p.y,r.p.y);
141i.size.x=min(p2.x,rp2.x)-i.p.x;
142i.size.y=min(p2.y,rp2.y)-i.p.y;
143return i;
144}
145
146XYRect translation(const XY<T>& t) const
147{
148return XYRect(p+t,size);
149}
150
151T distanceTo(const XY<T>& n) const
152{
153XY<T> tp=n;
154if (n.x<p.x) tp.x=p.x; else if (n.x>=(p.x+size.x)) tp.x=p.x+size.x;
155if (n.y<p.y) tp.y=p.y; else if (n.y>=(p.y+size.y)) tp.y=p.y+size.y;
156 return tp.distanceTo(n);
157}
158
159static const XYRect& zero() {static XYRect t(0,0,0,0); return t;}
160static const XYRect& one() {static XYRect t(0,0,1,1); return t;}
161};
162
163typedef XY<int> IntXY;
164typedef XYRect<int> IntRect;
165
166#endif
Note: See TracBrowser for help on using the repository browser.