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

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

Added XYRect::fitAspect(float aspect)

  • Property svn:eol-style set to native
File size: 8.5 KB
Line 
1// This file is a part of Framsticks SDK.  http://www.framsticks.com/
2// Copyright (C) 1999-2019  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//unification of old GUIXY and Pt2D
12template <typename T> class XY
13{
14public:
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        template <typename Q> const XY operator()(const Q& other) { return XY(other.x, other.y); }
21        XY operator+(const XY&p) const { return XY(x + p.x, y + p.y); }
22        XY operator-(const XY&p) const { return XY(x - p.x, y - p.y); }
23        XY operator+=(const XY&p) { x += p.x; y += p.y; return *this; }
24        XY operator-=(const XY&p) { x -= p.x; y -= p.y; return *this; }
25        XY operator-() const { return XY(-x, -y); }
26        // allows float operations on ints
27        template <typename Q> XY operator*=(Q q) { x *= q; y *= q; return *this; }
28        template <typename Q> XY operator/=(Q q) { x /= q; y /= q; return *this; }
29        template <typename Q> XY operator/(Q q) const { return XY(x / q, y / q); }
30        template <typename Q> XY operator*(Q q) const { return XY(q*x, q*y); }
31        XY operator*=(const XY& q) { x *= q.x; y *= q.y; return *this; }
32        XY operator/=(const XY& q) { x /= q.x; y /= q.y; return *this; }
33        XY operator*(const XY& q) const { return XY(x*q.x, y*q.y); }
34        XY operator/(const XY& q) const { return XY(x/q.x, y/q.y); }
35        void set(T _x, T _y) { x = _x; y = _y; }
36        void add(T _x, T _y) { x += _x; y += _y; }
37        void sub(T _x, T _y) { x -= _x; y -= _y; }
38        bool operator==(const XY& p) const { return (fabs(double(x - p.x)) < 1e-20) && (fabs(double(y - p.y)) < 1e-20); }
39        bool operator!=(const XY& p) const { return !operator==(p); }
40        T distanceTo(const XY& p) const { return sqrt(double((p.x - x)*(p.x - x) + (p.y - y)*(p.y - y))); }
41        T magnitude() const { return sqrt(x*x + y * y); }
42        T length() const { return sqrt(x*x + y * y); }
43        T lengthSq() const { return x * x + y * y; }
44        T dotProduct(const XY& v) const { return x * v.x + y * v.y; }
45        T crossProduct(const XY& v) const { return x * v.y - y * v.x; }
46        void normalize() { operator/=(length()); } // length becomes 1
47        static XY average(const XY& v1, const XY& v2) { return XY((v1.x + v2.x)*0.5, (v1.y + v2.y)*0.5); }
48        double getDirection() const { return atan2(y, x); }
49        static XY interpolate(const XY& v1, const XY& v2, double t) { return universal_lerp(v1,v2,t); }
50        XY toInt() const { return XY(int(x), int(y)); }
51        XY transpose() const { return XY(y, x); }
52        static const XY& zero() { static XY t(0, 0); return t; }
53        static const XY& one() { static XY t(1, 1); return t; }
54};
55
56//specialized: int equality not using fabs()
57template<> inline bool XY<int>::operator==(const XY<int>& p) const { return (x == p.x) && (y == p.y); }
58
59template <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)); }
60template <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)); }
61
62template <typename T>
63class XYMargin
64{
65public:
66        XYMargin(T x = 0) :left(x), top(x), right(x), bottom(x) {}
67        XYMargin(T l, T t, T r, T b) :left(l), top(t), right(r), bottom(b) {}
68        T left, top, right, bottom;
69        void operator=(T x) { left = top = right = bottom = x; }
70        XYMargin operator-() const { return XYMargin(-left, -top, -right, -bottom); }
71        void operator=(const XYMargin<T> &other) { left = other.left; top = other.top; right = other.right; bottom = other.bottom; }
72        T horizontal() const { return left + right; }
73        T vertical() const { return top + bottom; }
74        bool operator==(const XYMargin &other) const { return left == other.left && top == other.top && right == other.right && bottom == other.bottom; }
75        XYMargin normalized() const { return XYMargin(max(left, T(0)), max(top, T(0)), max(right, T(0)), max(bottom, T(0))); }
76};
77
78template <typename T>
79class XYRect
80{
81public:
82        XY<T> p, size;
83        XYRect() {}
84        XYRect(const XY<T>& p1, const XY<T>& s) :p(p1), size(s) {}
85        template <typename Q> XYRect(const Q& other) : p(other.p), size(other.size) {}
86        XYRect(T _x, T _y, T _w, T _h) :p(_x, _y), size(_w, _h) {}
87        static XYRect<T> centeredAt(const XY<T>& p, XY<T> s) { return XYRect<T>(p - s * 0.5, s); }
88
89        bool isEmpty() const { return (size.x < 0) || (size.y < 0); }
90        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)); }
91        bool operator==(const XYRect& r) const { return (p == r.p) && (size == r.size); }
92        template <typename Q> const XYRect& operator=(const Q& other) { p = other.p; size = other.size; return *this; }
93
94        T right() const { return p.x + size.x; }
95        T bottom() const { return p.y + size.y; }
96        T top() const { return p.y; }
97        T left() const { return p.x; }
98        XY<T> center() const { return p + size / 2; }
99        const XY<T>& topLeft() const { return p; }
100        XY<T> bottomRight() const { return p + size; }
101        XY<T> topRight() const { return XY<T>(p.x + size.x, p.y); }
102        XY<T> bottomLeft() const { return XY<T>(p.x, p.y + size.y); }
103
104        T area() const { return size.x*size.y; }
105
106        bool intersects(const XYRect& r) const
107        {
108                if (r.p.x >= (p.x + size.x)) return false;
109                if (r.p.y >= (p.y + size.y)) return false;
110                if ((r.p.x + r.size.x) <= p.x) return false;
111                if ((r.p.y + r.size.y) <= p.y) return false;
112                return true;
113        }
114
115        bool contains(const XY<T>& n) const
116        {
117                if (n.x < p.x) return false;
118                if (n.x > (p.x + size.x)) return false;
119                if (n.y < p.y) return false;
120                if (n.y > (p.y + size.y)) return false;
121                return true;
122        }
123
124        bool contains(const XYRect& r) const
125        {
126                return contains(r.p) && contains(r.p + r.size);
127        }
128
129        void add(const XY<T>& n)
130        {
131                if (n.x < p.x) { size.x += p.x - n.x; p.x = n.x; }
132                else if (n.x > (p.x + size.x)) size.x = n.x - p.x;
133                if (n.y < p.y) { size.y += p.y - n.y; p.y = n.y; }
134                else if (n.y > (p.y + size.y)) size.y = n.y - p.y;
135        }
136
137        XYRect extendBy(const XY<T>& border_size) const
138        {
139                return XYRect(p - border_size, size + border_size * 2);
140        }
141
142        XYRect shrinkBy(const XY<T>& border_size) const
143        {
144                return XYRect(p + border_size, size - border_size * 2);
145        }
146
147        XYRect extendBy(const XYMargin<T>& m) const
148        {
149                return XYRect(p.x - m.left, p.y - m.top, size.x + m.horizontal(), size.y + m.vertical());
150        }
151
152        XYRect shrinkBy(const XYMargin<T>& m) const
153        {
154                return XYRect(p.x + m.left, p.y + m.top, size.x - m.horizontal(), size.y - m.vertical());
155        }
156
157        XYMargin<T> marginTowards(const XYRect &r) const
158        {
159                return XYMargin<T>(r.p.x - p.x, r.p.y - p.y,
160                        (p.x + size.x) - (r.p.x + r.size.x), (p.y + size.y) - (r.p.y + r.size.y));
161        }
162
163        XYRect fitAspect(float aspect) ///< place a new rectangle having 'aspect' inside the rectangle
164        {
165                XYRect r;
166                r.size=size;
167                if (size.x < size.y*aspect)
168                        r.size.y = r.size.x/aspect;
169                else
170                        r.size.x = r.size.y*aspect;
171                r.p=p+(size-r.size)*0.5;
172                return r;               
173        }
174       
175        XYRect intersection(const XYRect& r) const
176        {
177                XYRect i;
178                XY<T> p2 = p + size;
179                XY<T> rp2 = r.p + r.size;
180                i.p.x = max(p.x, r.p.x);
181                i.p.y = max(p.y, r.p.y);
182                i.size.x = min(p2.x, rp2.x) - i.p.x;
183                i.size.y = min(p2.y, rp2.y) - i.p.y;
184                return i;
185        }
186
187        XYRect extensionContaining(const XYRect& r) const
188        {
189                XY<T> p1 = xymin(topLeft(), r.topLeft());
190                XY<T> p2 = xymax(bottomRight(), r.bottomRight());
191                return XYRect(p1, p2 - p1);
192        }
193
194        XYRect translation(const XY<T>& t) const
195        {
196                return XYRect(p + t, size);
197        }
198
199        T distanceTo(const XY<T>& n) const
200        {
201                XY<T> tp = n;
202                if (n.x < p.x) tp.x = p.x; else if (n.x >= (p.x + size.x)) tp.x = p.x + size.x;
203                if (n.y < p.y) tp.y = p.y; else if (n.y >= (p.y + size.y)) tp.y = p.y + size.y;
204                return tp.distanceTo(n);
205        }
206
207        T distanceTo(const XYRect<T>& r) const
208        {
209                bool r_above = (r.bottom() <= top());
210                bool r_below = (r.top() >= bottom());
211                bool r_left = (r.right() <= left());
212                bool r_right = (r.left() >= right());
213
214                if (r_above)
215                {
216                        if (r_left) return r.bottomRight().distanceTo(topLeft());
217                        else if (r_right) return r.bottomLeft().distanceTo(topRight());
218                        else return top() - r.bottom();
219                }
220                else if (r_below)
221                {
222                        if (r_left) return r.topRight().distanceTo(bottomLeft());
223                        else if (r_right) return r.topLeft().distanceTo(bottomRight());
224                        else return r.top() - bottom();
225                }
226                else if (r_left)
227                {
228                        return left() - r.right();
229                }
230                else if (r_right)
231                {
232                        return r.left() - right();
233                }
234                else
235                        return 0; //intersection
236        }
237
238        static const XYRect& zero() { static XYRect t(0, 0, 0, 0); return t; }
239        static const XYRect& one() { static XYRect t(0, 0, 1, 1); return t; }
240};
241
242typedef XY<int> IntXY;
243typedef XYRect<int> IntRect;
244
245typedef XY<float> FloatXY;
246typedef XYRect<float> FloatRect;
247
248#endif
Note: See TracBrowser for help on using the repository browser.