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