]> git.lizzy.rs Git - irrlicht.git/blob - include/vector2d.h
Add a unified cross platform OpenGL core profile binding (#52)
[irrlicht.git] / include / vector2d.h
1 // Copyright (C) 2002-2012 Nikolaus Gebhardt\r
2 // This file is part of the "Irrlicht Engine".\r
3 // For conditions of distribution and use, see copyright notice in irrlicht.h\r
4 \r
5 #ifndef __IRR_POINT_2D_H_INCLUDED__\r
6 #define __IRR_POINT_2D_H_INCLUDED__\r
7 \r
8 #include "irrMath.h"\r
9 #include "dimension2d.h"\r
10 \r
11 namespace irr\r
12 {\r
13 namespace core\r
14 {\r
15 \r
16 \r
17 //! 2d vector template class with lots of operators and methods.\r
18 /** As of Irrlicht 1.6, this class supersedes position2d, which should\r
19         be considered deprecated. */\r
20 template <class T>\r
21 class vector2d\r
22 {\r
23 public:\r
24         //! Default constructor (null vector)\r
25         vector2d() : X(0), Y(0) {}\r
26         //! Constructor with two different values\r
27         vector2d(T nx, T ny) : X(nx), Y(ny) {}\r
28         //! Constructor with the same value for both members\r
29         explicit vector2d(T n) : X(n), Y(n) {}\r
30 \r
31         vector2d(const dimension2d<T>& other) : X(other.Width), Y(other.Height) {}\r
32 \r
33         // operators\r
34 \r
35         vector2d<T> operator-() const { return vector2d<T>(-X, -Y); }\r
36 \r
37         vector2d<T>& operator=(const dimension2d<T>& other) { X = other.Width; Y = other.Height; return *this; }\r
38 \r
39         vector2d<T> operator+(const vector2d<T>& other) const { return vector2d<T>(X + other.X, Y + other.Y); }\r
40         vector2d<T> operator+(const dimension2d<T>& other) const { return vector2d<T>(X + other.Width, Y + other.Height); }\r
41         vector2d<T>& operator+=(const vector2d<T>& other) { X+=other.X; Y+=other.Y; return *this; }\r
42         vector2d<T> operator+(const T v) const { return vector2d<T>(X + v, Y + v); }\r
43         vector2d<T>& operator+=(const T v) { X+=v; Y+=v; return *this; }\r
44         vector2d<T>& operator+=(const dimension2d<T>& other) { X += other.Width; Y += other.Height; return *this;  }\r
45 \r
46         vector2d<T> operator-(const vector2d<T>& other) const { return vector2d<T>(X - other.X, Y - other.Y); }\r
47         vector2d<T> operator-(const dimension2d<T>& other) const { return vector2d<T>(X - other.Width, Y - other.Height); }\r
48         vector2d<T>& operator-=(const vector2d<T>& other) { X-=other.X; Y-=other.Y; return *this; }\r
49         vector2d<T> operator-(const T v) const { return vector2d<T>(X - v, Y - v); }\r
50         vector2d<T>& operator-=(const T v) { X-=v; Y-=v; return *this; }\r
51         vector2d<T>& operator-=(const dimension2d<T>& other) { X -= other.Width; Y -= other.Height; return *this;  }\r
52 \r
53         vector2d<T> operator*(const vector2d<T>& other) const { return vector2d<T>(X * other.X, Y * other.Y); }\r
54         vector2d<T>& operator*=(const vector2d<T>& other) { X*=other.X; Y*=other.Y; return *this; }\r
55         vector2d<T> operator*(const T v) const { return vector2d<T>(X * v, Y * v); }\r
56         vector2d<T>& operator*=(const T v) { X*=v; Y*=v; return *this; }\r
57 \r
58         vector2d<T> operator/(const vector2d<T>& other) const { return vector2d<T>(X / other.X, Y / other.Y); }\r
59         vector2d<T>& operator/=(const vector2d<T>& other) { X/=other.X; Y/=other.Y; return *this; }\r
60         vector2d<T> operator/(const T v) const { return vector2d<T>(X / v, Y / v); }\r
61         vector2d<T>& operator/=(const T v) { X/=v; Y/=v; return *this; }\r
62 \r
63         T& operator [](u32 index)\r
64         {\r
65                 _IRR_DEBUG_BREAK_IF(index>1) // access violation\r
66 \r
67                 return *(&X+index);\r
68         }\r
69 \r
70         const T& operator [](u32 index) const\r
71         {\r
72                 _IRR_DEBUG_BREAK_IF(index>1) // access violation\r
73 \r
74                 return *(&X+index);\r
75         }\r
76 \r
77         //! sort in order X, Y. Equality with rounding tolerance.\r
78         bool operator<=(const vector2d<T>&other) const\r
79         {\r
80                 return (X<other.X || core::equals(X, other.X)) ||\r
81                                 (core::equals(X, other.X) && (Y<other.Y || core::equals(Y, other.Y)));\r
82         }\r
83 \r
84         //! sort in order X, Y. Equality with rounding tolerance.\r
85         bool operator>=(const vector2d<T>&other) const\r
86         {\r
87                 return (X>other.X || core::equals(X, other.X)) ||\r
88                                 (core::equals(X, other.X) && (Y>other.Y || core::equals(Y, other.Y)));\r
89         }\r
90 \r
91         //! sort in order X, Y. Difference must be above rounding tolerance.\r
92         bool operator<(const vector2d<T>&other) const\r
93         {\r
94                 return (X<other.X && !core::equals(X, other.X)) ||\r
95                                 (core::equals(X, other.X) && Y<other.Y && !core::equals(Y, other.Y));\r
96         }\r
97 \r
98         //! sort in order X, Y. Difference must be above rounding tolerance.\r
99         bool operator>(const vector2d<T>&other) const\r
100         {\r
101                 return (X>other.X && !core::equals(X, other.X)) ||\r
102                                 (core::equals(X, other.X) && Y>other.Y && !core::equals(Y, other.Y));\r
103         }\r
104 \r
105         bool operator==(const vector2d<T>& other) const { return equals(other); }\r
106         bool operator!=(const vector2d<T>& other) const { return !equals(other); }\r
107 \r
108         // functions\r
109 \r
110         //! Checks if this vector equals the other one.\r
111         /** Takes floating point rounding errors into account.\r
112         \param other Vector to compare with.\r
113         \param tolerance Epsilon value for both - comparing X and Y.\r
114         \return True if the two vector are (almost) equal, else false. */\r
115         bool equals(const vector2d<T>& other, const T tolerance = (T)ROUNDING_ERROR_f32 ) const\r
116         {\r
117                 return core::equals(X, other.X, tolerance) && core::equals(Y, other.Y, tolerance);\r
118         }\r
119 \r
120         vector2d<T>& set(T nx, T ny) {X=nx; Y=ny; return *this; }\r
121         vector2d<T>& set(const vector2d<T>& p) { X=p.X; Y=p.Y; return *this; }\r
122 \r
123         //! Gets the length of the vector.\r
124         /** \return The length of the vector. */\r
125         T getLength() const { return core::squareroot( X*X + Y*Y ); }\r
126 \r
127         //! Get the squared length of this vector\r
128         /** This is useful because it is much faster than getLength().\r
129         \return The squared length of the vector. */\r
130         T getLengthSQ() const { return X*X + Y*Y; }\r
131 \r
132         //! Get the dot product of this vector with another.\r
133         /** \param other Other vector to take dot product with.\r
134         \return The dot product of the two vectors. */\r
135         T dotProduct(const vector2d<T>& other) const\r
136         {\r
137                 return X*other.X + Y*other.Y;\r
138         }\r
139 \r
140         //! check if this vector is parallel to another vector\r
141         bool nearlyParallel( const vector2d<T> & other, const T factor = relativeErrorFactor<T>()) const\r
142         {\r
143                 // https://eagergames.wordpress.com/2017/04/01/fast-parallel-lines-and-vectors-test/\r
144                 // if a || b then  a.x/a.y = b.x/b.y (similiar triangles)\r
145                 // if a || b then either both x are 0 or both y are 0.\r
146 \r
147                 return  equalsRelative( X*other.Y, other.X* Y, factor)\r
148                 && // a bit counterintuitive, but makes sure  that\r
149                    // only y or only x are 0, and at same time deals\r
150                    // with the case where one vector is zero vector.\r
151                         (X*other.X + Y*other.Y) != 0;\r
152         }\r
153 \r
154         //! Gets distance from another point.\r
155         /** Here, the vector is interpreted as a point in 2-dimensional space.\r
156         \param other Other vector to measure from.\r
157         \return Distance from other point. */\r
158         T getDistanceFrom(const vector2d<T>& other) const\r
159         {\r
160                 return vector2d<T>(X - other.X, Y - other.Y).getLength();\r
161         }\r
162 \r
163         //! Returns squared distance from another point.\r
164         /** Here, the vector is interpreted as a point in 2-dimensional space.\r
165         \param other Other vector to measure from.\r
166         \return Squared distance from other point. */\r
167         T getDistanceFromSQ(const vector2d<T>& other) const\r
168         {\r
169                 return vector2d<T>(X - other.X, Y - other.Y).getLengthSQ();\r
170         }\r
171 \r
172         //! rotates the point anticlockwise around a center by an amount of degrees.\r
173         /** \param degrees Amount of degrees to rotate by, anticlockwise.\r
174         \param center Rotation center.\r
175         \return This vector after transformation. */\r
176         vector2d<T>& rotateBy(f64 degrees, const vector2d<T>& center=vector2d<T>())\r
177         {\r
178                 degrees *= DEGTORAD64;\r
179                 const f64 cs = cos(degrees);\r
180                 const f64 sn = sin(degrees);\r
181 \r
182                 X -= center.X;\r
183                 Y -= center.Y;\r
184 \r
185                 set((T)(X*cs - Y*sn), (T)(X*sn + Y*cs));\r
186 \r
187                 X += center.X;\r
188                 Y += center.Y;\r
189                 return *this;\r
190         }\r
191 \r
192         //! Normalize the vector.\r
193         /** The null vector is left untouched.\r
194         \return Reference to this vector, after normalization. */\r
195         vector2d<T>& normalize()\r
196         {\r
197                 f32 length = (f32)(X*X + Y*Y);\r
198                 if ( length == 0 )\r
199                         return *this;\r
200                 length = core::reciprocal_squareroot ( length );\r
201                 X = (T)(X * length);\r
202                 Y = (T)(Y * length);\r
203                 return *this;\r
204         }\r
205 \r
206         //! Calculates the angle of this vector in degrees in the trigonometric sense.\r
207         /** 0 is to the right (3 o'clock), values increase counter-clockwise.\r
208         This method has been suggested by Pr3t3nd3r.\r
209         \return Returns a value between 0 and 360. */\r
210         f64 getAngleTrig() const\r
211         {\r
212                 if (Y == 0)\r
213                         return X < 0 ? 180 : 0;\r
214                 else\r
215                 if (X == 0)\r
216                         return Y < 0 ? 270 : 90;\r
217 \r
218                 if ( Y > 0)\r
219                         if (X > 0)\r
220                                 return atan((irr::f64)Y/(irr::f64)X) * RADTODEG64;\r
221                         else\r
222                                 return 180.0-atan((irr::f64)Y/-(irr::f64)X) * RADTODEG64;\r
223                 else\r
224                         if (X > 0)\r
225                                 return 360.0-atan(-(irr::f64)Y/(irr::f64)X) * RADTODEG64;\r
226                         else\r
227                                 return 180.0+atan(-(irr::f64)Y/-(irr::f64)X) * RADTODEG64;\r
228         }\r
229 \r
230         //! Calculates the angle of this vector in degrees in the counter trigonometric sense.\r
231         /** 0 is to the right (3 o'clock), values increase clockwise.\r
232         \return Returns a value between 0 and 360. */\r
233         inline f64 getAngle() const\r
234         {\r
235                 if (Y == 0) // corrected thanks to a suggestion by Jox\r
236                         return X < 0 ? 180 : 0;\r
237                 else if (X == 0)\r
238                         return Y < 0 ? 90 : 270;\r
239 \r
240                 // don't use getLength here to avoid precision loss with s32 vectors\r
241                 // avoid floating-point trouble as sqrt(y*y) is occasionally larger than y, so clamp\r
242                 const f64 tmp = core::clamp(Y / sqrt((f64)(X*X + Y*Y)), -1.0, 1.0);\r
243                 const f64 angle = atan( core::squareroot(1 - tmp*tmp) / tmp) * RADTODEG64;\r
244 \r
245                 if (X>0 && Y>0)\r
246                         return angle + 270;\r
247                 else\r
248                 if (X>0 && Y<0)\r
249                         return angle + 90;\r
250                 else\r
251                 if (X<0 && Y<0)\r
252                         return 90 - angle;\r
253                 else\r
254                 if (X<0 && Y>0)\r
255                         return 270 - angle;\r
256 \r
257                 return angle;\r
258         }\r
259 \r
260         //! Calculates the angle between this vector and another one in degree.\r
261         /** \param b Other vector to test with.\r
262         \return Returns a value between 0 and 90. */\r
263         inline f64 getAngleWith(const vector2d<T>& b) const\r
264         {\r
265                 f64 tmp = (f64)(X*b.X + Y*b.Y);\r
266 \r
267                 if (tmp == 0.0)\r
268                         return 90.0;\r
269 \r
270                 tmp = tmp / core::squareroot((f64)((X*X + Y*Y) * (b.X*b.X + b.Y*b.Y)));\r
271                 if (tmp < 0.0)\r
272                         tmp = -tmp;\r
273                 if ( tmp > 1.0 ) //   avoid floating-point trouble\r
274                         tmp = 1.0;\r
275 \r
276                 return atan(sqrt(1 - tmp*tmp) / tmp) * RADTODEG64;\r
277         }\r
278 \r
279         //! Returns if this vector interpreted as a point is on a line between two other points.\r
280         /** It is assumed that the point is on the line.\r
281         \param begin Beginning vector to compare between.\r
282         \param end Ending vector to compare between.\r
283         \return True if this vector is between begin and end, false if not. */\r
284         bool isBetweenPoints(const vector2d<T>& begin, const vector2d<T>& end) const\r
285         {\r
286                 //             .  end\r
287                 //            /\r
288                 //           /\r
289                 //          /\r
290                 //         . begin\r
291                 //        -\r
292                 //       -\r
293                 //      . this point (am I inside or outside)?\r
294                 //\r
295                 if (begin.X != end.X)\r
296                 {\r
297                         return ((begin.X <= X && X <= end.X) ||\r
298                                         (begin.X >= X && X >= end.X));\r
299                 }\r
300                 else\r
301                 {\r
302                         return ((begin.Y <= Y && Y <= end.Y) ||\r
303                                         (begin.Y >= Y && Y >= end.Y));\r
304                 }\r
305         }\r
306 \r
307         //! Creates an interpolated vector between this vector and another vector.\r
308         /** \param other The other vector to interpolate with.\r
309         \param d Interpolation value between 0.0f (all the other vector) and 1.0f (all this vector).\r
310         Note that this is the opposite direction of interpolation to getInterpolated_quadratic()\r
311         \return An interpolated vector.  This vector is not modified. */\r
312         vector2d<T> getInterpolated(const vector2d<T>& other, f64 d) const\r
313         {\r
314                 const f64 inv = 1.0f - d;\r
315                 return vector2d<T>((T)(other.X*inv + X*d), (T)(other.Y*inv + Y*d));\r
316         }\r
317 \r
318         //! Creates a quadratically interpolated vector between this and two other vectors.\r
319         /** \param v2 Second vector to interpolate with.\r
320         \param v3 Third vector to interpolate with (maximum at 1.0f)\r
321         \param d Interpolation value between 0.0f (all this vector) and 1.0f (all the 3rd vector).\r
322         Note that this is the opposite direction of interpolation to getInterpolated() and interpolate()\r
323         \return An interpolated vector. This vector is not modified. */\r
324         vector2d<T> getInterpolated_quadratic(const vector2d<T>& v2, const vector2d<T>& v3, f64 d) const\r
325         {\r
326                 // this*(1-d)*(1-d) + 2 * v2 * (1-d) + v3 * d * d;\r
327                 const f64 inv = 1.0f - d;\r
328                 const f64 mul0 = inv * inv;\r
329                 const f64 mul1 = 2.0f * d * inv;\r
330                 const f64 mul2 = d * d;\r
331 \r
332                 return vector2d<T> ( (T)(X * mul0 + v2.X * mul1 + v3.X * mul2),\r
333                                         (T)(Y * mul0 + v2.Y * mul1 + v3.Y * mul2));\r
334         }\r
335 \r
336         /*! Test if this point and another 2 points taken as triplet\r
337                 are colinear, clockwise, anticlockwise. This can be used also\r
338                 to check winding order in triangles for 2D meshes.\r
339                 \return 0 if points are colinear, 1 if clockwise, 2 if anticlockwise\r
340         */\r
341         s32 checkOrientation( const vector2d<T> & b, const vector2d<T> & c) const\r
342         {\r
343                 // Example of clockwise points\r
344                 //\r
345                 //   ^ Y\r
346                 //   |       A\r
347                 //   |      . .\r
348                 //   |     .   .\r
349                 //   |    C.....B\r
350                 //   +---------------> X\r
351 \r
352                 T val = (b.Y - Y) * (c.X - b.X) -\r
353                         (b.X - X) * (c.Y - b.Y);\r
354 \r
355                 if (val == 0) return 0;  // colinear\r
356 \r
357                 return (val > 0) ? 1 : 2; // clock or counterclock wise\r
358         }\r
359 \r
360         /*! Returns true if points (a,b,c) are clockwise on the X,Y plane*/\r
361         inline bool areClockwise( const vector2d<T> & b, const vector2d<T> & c) const\r
362         {\r
363                 T val = (b.Y - Y) * (c.X - b.X) -\r
364                         (b.X - X) * (c.Y - b.Y);\r
365 \r
366                 return val > 0;\r
367         }\r
368 \r
369         /*! Returns true if points (a,b,c) are counterclockwise on the X,Y plane*/\r
370         inline bool areCounterClockwise( const vector2d<T> & b, const vector2d<T> & c) const\r
371         {\r
372                 T val = (b.Y - Y) * (c.X - b.X) -\r
373                         (b.X - X) * (c.Y - b.Y);\r
374 \r
375                 return val < 0;\r
376         }\r
377 \r
378         //! Sets this vector to the linearly interpolated vector between a and b.\r
379         /** \param a first vector to interpolate with, maximum at 1.0f\r
380         \param b second vector to interpolate with, maximum at 0.0f\r
381         \param d Interpolation value between 0.0f (all vector b) and 1.0f (all vector a)\r
382         Note that this is the opposite direction of interpolation to getInterpolated_quadratic()\r
383         */\r
384         vector2d<T>& interpolate( const vector2d<T>& a, const vector2d<T>& b, f64 d)\r
385         {\r
386                 X = (T)((f64)b.X + ( ( a.X - b.X ) * d ));\r
387                 Y = (T)((f64)b.Y + ( ( a.Y - b.Y ) * d ));\r
388                 return *this;\r
389         }\r
390 \r
391         //! X coordinate of vector.\r
392         T X;\r
393 \r
394         //! Y coordinate of vector.\r
395         T Y;\r
396 };\r
397 \r
398         //! Typedef for f32 2d vector.\r
399         typedef vector2d<f32> vector2df;\r
400 \r
401         //! Typedef for integer 2d vector.\r
402         typedef vector2d<s32> vector2di;\r
403 \r
404         template<class S, class T>\r
405         vector2d<T> operator*(const S scalar, const vector2d<T>& vector) { return vector*scalar; }\r
406 \r
407         // These methods are declared in dimension2d, but need definitions of vector2d\r
408         template<class T>\r
409         dimension2d<T>::dimension2d(const vector2d<T>& other) : Width(other.X), Height(other.Y) { }\r
410 \r
411         template<class T>\r
412         bool dimension2d<T>::operator==(const vector2d<T>& other) const { return Width == other.X && Height == other.Y; }\r
413 \r
414 } // end namespace core\r
415 } // end namespace irr\r
416 \r
417 #endif\r
418 \r