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

Last change on this file since 684 was 286, checked in by Maciej Komosinski, 10 years ago

Updated headers

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