 Irrlicht 3D Engine vector2d.h
Go to the documentation of this file.
1 // Copyright (C) 2002-2012 Nikolaus Gebhardt
2 // This file is part of the "Irrlicht Engine".
3 // For conditions of distribution and use, see copyright notice in irrlicht.h
4
5 #ifndef __IRR_POINT_2D_H_INCLUDED__
6 #define __IRR_POINT_2D_H_INCLUDED__
7
8 #include "irrMath.h"
9 #include "dimension2d.h"
10
11 namespace irr
12 {
13 namespace core
14 {
15
16
18
20 template <class T>
21 class vector2d
22 {
23 public:
25  vector2d() : X(0), Y(0) {}
27  vector2d(T nx, T ny) : X(nx), Y(ny) {}
29  explicit vector2d(T n) : X(n), Y(n) {}
31  vector2d(const vector2d<T>& other) : X(other.X), Y(other.Y) {}
32
33  vector2d(const dimension2d<T>& other) : X(other.Width), Y(other.Height) {}
34
35  // operators
36
37  vector2d<T> operator-() const { return vector2d<T>(-X, -Y); }
38
39  vector2d<T>& operator=(const vector2d<T>& other) { X = other.X; Y = other.Y; return *this; }
40
41  vector2d<T>& operator=(const dimension2d<T>& other) { X = other.Width; Y = other.Height; return *this; }
42
43  vector2d<T> operator+(const vector2d<T>& other) const { return vector2d<T>(X + other.X, Y + other.Y); }
44  vector2d<T> operator+(const dimension2d<T>& other) const { return vector2d<T>(X + other.Width, Y + other.Height); }
45  vector2d<T>& operator+=(const vector2d<T>& other) { X+=other.X; Y+=other.Y; return *this; }
46  vector2d<T> operator+(const T v) const { return vector2d<T>(X + v, Y + v); }
47  vector2d<T>& operator+=(const T v) { X+=v; Y+=v; return *this; }
48  vector2d<T>& operator+=(const dimension2d<T>& other) { X += other.Width; Y += other.Height; return *this; }
49
50  vector2d<T> operator-(const vector2d<T>& other) const { return vector2d<T>(X - other.X, Y - other.Y); }
51  vector2d<T> operator-(const dimension2d<T>& other) const { return vector2d<T>(X - other.Width, Y - other.Height); }
52  vector2d<T>& operator-=(const vector2d<T>& other) { X-=other.X; Y-=other.Y; return *this; }
53  vector2d<T> operator-(const T v) const { return vector2d<T>(X - v, Y - v); }
54  vector2d<T>& operator-=(const T v) { X-=v; Y-=v; return *this; }
55  vector2d<T>& operator-=(const dimension2d<T>& other) { X -= other.Width; Y -= other.Height; return *this; }
56
57  vector2d<T> operator*(const vector2d<T>& other) const { return vector2d<T>(X * other.X, Y * other.Y); }
58  vector2d<T>& operator*=(const vector2d<T>& other) { X*=other.X; Y*=other.Y; return *this; }
59  vector2d<T> operator*(const T v) const { return vector2d<T>(X * v, Y * v); }
60  vector2d<T>& operator*=(const T v) { X*=v; Y*=v; return *this; }
61
62  vector2d<T> operator/(const vector2d<T>& other) const { return vector2d<T>(X / other.X, Y / other.Y); }
63  vector2d<T>& operator/=(const vector2d<T>& other) { X/=other.X; Y/=other.Y; return *this; }
64  vector2d<T> operator/(const T v) const { return vector2d<T>(X / v, Y / v); }
65  vector2d<T>& operator/=(const T v) { X/=v; Y/=v; return *this; }
66
68  bool operator<=(const vector2d<T>&other) const
69  {
70  return (X<other.X || core::equals(X, other.X)) ||
71  (core::equals(X, other.X) && (Y<other.Y || core::equals(Y, other.Y)));
72  }
73
75  bool operator>=(const vector2d<T>&other) const
76  {
77  return (X>other.X || core::equals(X, other.X)) ||
78  (core::equals(X, other.X) && (Y>other.Y || core::equals(Y, other.Y)));
79  }
80
82  bool operator<(const vector2d<T>&other) const
83  {
84  return (X<other.X && !core::equals(X, other.X)) ||
85  (core::equals(X, other.X) && Y<other.Y && !core::equals(Y, other.Y));
86  }
87
89  bool operator>(const vector2d<T>&other) const
90  {
91  return (X>other.X && !core::equals(X, other.X)) ||
92  (core::equals(X, other.X) && Y>other.Y && !core::equals(Y, other.Y));
93  }
94
95  bool operator==(const vector2d<T>& other) const { return equals(other); }
96  bool operator!=(const vector2d<T>& other) const { return !equals(other); }
97
98  // functions
99
101
105  bool equals(const vector2d<T>& other, const T tolerance = (T)ROUNDING_ERROR_f32 ) const
106  {
107  return core::equals(X, other.X, tolerance) && core::equals(Y, other.Y, tolerance);
108  }
109
110  vector2d<T>& set(T nx, T ny) {X=nx; Y=ny; return *this; }
111  vector2d<T>& set(const vector2d<T>& p) { X=p.X; Y=p.Y; return *this; }
112
114
115  T getLength() const { return core::squareroot( X*X + Y*Y ); }
116
118
120  T getLengthSQ() const { return X*X + Y*Y; }
121
123
125  T dotProduct(const vector2d<T>& other) const
126  {
127  return X*other.X + Y*other.Y;
128  }
129
131  bool nearlyParallel( const vector2d<T> & other, const T factor = relativeErrorFactor<T>()) const
132  {
133  // https://eagergames.wordpress.com/2017/04/01/fast-parallel-lines-and-vectors-test/
134  // if a || b then a.x/a.y = b.x/b.y (similiar triangles)
135  // if a || b then either both x are 0 or both y are 0.
136
137  return equalsRelative( X*other.Y, other.X* Y, factor)
138  && // a bit counterintuitive, but makes sure that
139  // only y or only x are 0, and at same time deals
140  // with the case where one vector is zero vector.
141  (X*other.X + Y*other.Y) != 0;
142  }
143
145
148  T getDistanceFrom(const vector2d<T>& other) const
149  {
150  return vector2d<T>(X - other.X, Y - other.Y).getLength();
151  }
152
154
157  T getDistanceFromSQ(const vector2d<T>& other) const
158  {
159  return vector2d<T>(X - other.X, Y - other.Y).getLengthSQ();
160  }
161
163
166  vector2d<T>& rotateBy(f64 degrees, const vector2d<T>& center=vector2d<T>())
167  {
169  const f64 cs = cos(degrees);
170  const f64 sn = sin(degrees);
171
172  X -= center.X;
173  Y -= center.Y;
174
175  set((T)(X*cs - Y*sn), (T)(X*sn + Y*cs));
176
177  X += center.X;
178  Y += center.Y;
179  return *this;
180  }
181
183
186  {
187  f32 length = (f32)(X*X + Y*Y);
188  if ( length == 0 )
189  return *this;
190  length = core::reciprocal_squareroot ( length );
191  X = (T)(X * length);
192  Y = (T)(Y * length);
193  return *this;
194  }
195
197
201  {
202  if (Y == 0)
203  return X < 0 ? 180 : 0;
204  else
205  if (X == 0)
206  return Y < 0 ? 270 : 90;
207
208  if ( Y > 0)
209  if (X > 0)
211  else
213  else
214  if (X > 0)
216  else
218  }
219
221
223  inline f64 getAngle() const
224  {
225  if (Y == 0) // corrected thanks to a suggestion by Jox
226  return X < 0 ? 180 : 0;
227  else if (X == 0)
228  return Y < 0 ? 90 : 270;
229
230  // don't use getLength here to avoid precision loss with s32 vectors
231  // avoid floating-point trouble as sqrt(y*y) is occasionally larger than y, so clamp
232  const f64 tmp = core::clamp(Y / sqrt((f64)(X*X + Y*Y)), -1.0, 1.0);
233  const f64 angle = atan( core::squareroot(1 - tmp*tmp) / tmp) * RADTODEG64;
234
235  if (X>0 && Y>0)
236  return angle + 270;
237  else
238  if (X>0 && Y<0)
239  return angle + 90;
240  else
241  if (X<0 && Y<0)
242  return 90 - angle;
243  else
244  if (X<0 && Y>0)
245  return 270 - angle;
246
247  return angle;
248  }
249
251
253  inline f64 getAngleWith(const vector2d<T>& b) const
254  {
255  f64 tmp = (f64)(X*b.X + Y*b.Y);
256
257  if (tmp == 0.0)
258  return 90.0;
259
260  tmp = tmp / core::squareroot((f64)((X*X + Y*Y) * (b.X*b.X + b.Y*b.Y)));
261  if (tmp < 0.0)
262  tmp = -tmp;
263  if ( tmp > 1.0 ) // avoid floating-point trouble
264  tmp = 1.0;
265
266  return atan(sqrt(1 - tmp*tmp) / tmp) * RADTODEG64;
267  }
268
270
274  bool isBetweenPoints(const vector2d<T>& begin, const vector2d<T>& end) const
275  {
276  // . end
277  // /
278  // /
279  // /
280  // . begin
281  // -
282  // -
283  // . this point (am I inside or outside)?
284  //
285  if (begin.X != end.X)
286  {
287  return ((begin.X <= X && X <= end.X) ||
288  (begin.X >= X && X >= end.X));
289  }
290  else
291  {
292  return ((begin.Y <= Y && Y <= end.Y) ||
293  (begin.Y >= Y && Y >= end.Y));
294  }
295  }
296
298
303  {
304  f64 inv = 1.0f - d;
305  return vector2d<T>((T)(other.X*inv + X*d), (T)(other.Y*inv + Y*d));
306  }
307
309
315  {
316  // this*(1-d)*(1-d) + 2 * v2 * (1-d) + v3 * d * d;
317  const f64 inv = 1.0f - d;
318  const f64 mul0 = inv * inv;
319  const f64 mul1 = 2.0f * d * inv;
320  const f64 mul2 = d * d;
321
322  return vector2d<T> ( (T)(X * mul0 + v2.X * mul1 + v3.X * mul2),
323  (T)(Y * mul0 + v2.Y * mul1 + v3.Y * mul2));
324  }
325
331  s32 checkOrientation( const vector2d<T> & b, const vector2d<T> & c) const
332  {
333  // Example of clockwise points
334  //
335  // ^ Y
336  // | A
337  // | . .
338  // | . .
339  // | C.....B
340  // +---------------> X
341
342  T val = (b.Y - Y) * (c.X - b.X) -
343  (b.X - X) * (c.Y - b.Y);
344
345  if (val == 0) return 0; // colinear
346
347  return (val > 0) ? 1 : 2; // clock or counterclock wise
348  }
349
351  inline bool areClockwise( const vector2d<T> & b, const vector2d<T> & c) const
352  {
353  T val = (b.Y - Y) * (c.X - b.X) -
354  (b.X - X) * (c.Y - b.Y);
355
356  return val > 0;
357  }
358
360  inline bool areCounterClockwise( const vector2d<T> & b, const vector2d<T> & c) const
361  {
362  T val = (b.Y - Y) * (c.X - b.X) -
363  (b.X - X) * (c.Y - b.Y);
364
365  return val < 0;
366  }
367
369
375  {
376  X = (T)((f64)b.X + ( ( a.X - b.X ) * d ));
377  Y = (T)((f64)b.Y + ( ( a.Y - b.Y ) * d ));
378  return *this;
379  }
380
382  T X;
383
385  T Y;
386 };
387
390
393
394  template<class S, class T>
395  vector2d<T> operator*(const S scalar, const vector2d<T>& vector) { return vector*scalar; }
396
397  // These methods are declared in dimension2d, but need definitions of vector2d
398  template<class T>
399  dimension2d<T>::dimension2d(const vector2d<T>& other) : Width(other.X), Height(other.Y) { }
400
401  template<class T>
402  bool dimension2d<T>::operator==(const vector2d<T>& other) const { return Width == other.X && Height == other.Y; }
403
404 } // end namespace core
405 } // end namespace irr
406
407 #endif
408
bool operator<(const vector2d< T > &other) const
sort in order X, Y. Difference must be above rounding tolerance.
Definition: vector2d.h:82
64bit constant for converting from radians to degrees
Definition: irrMath.h:84
vector2d< T > operator-(const T v) const
Definition: vector2d.h:53
vector2d< T > operator-() const
Definition: vector2d.h:37
dimension2d()
Default constructor for empty dimension.
Definition: dimension2d.h:24
vector2d< T > & set(T nx, T ny)
Definition: vector2d.h:110
s32 checkOrientation(const vector2d< T > &b, const vector2d< T > &c) const
Definition: vector2d.h:331
vector2d< T > operator-(const vector2d< T > &other) const
Definition: vector2d.h:50
float f32
32 bit floating point variable.
Definition: irrTypes.h:108
REALINLINE f32 squareroot(const f32 f)
Definition: irrMath.h:506
T Y
Y coordinate of vector.
Definition: vector2d.h:385
vector2d< T > operator *(const vector2d< T > &other) const
Definition: vector2d.h:57
vector2d< T > & set(const vector2d< T > &p)
Definition: vector2d.h:111
bool areClockwise(const vector2d< T > &b, const vector2d< T > &c) const
Definition: vector2d.h:351
bool equalsRelative(const T a, const T b, const T factor=relativeErrorFactor< T >())
Definition: irrMath.h:255
vector2d< T > & rotateBy(f64 degrees, const vector2d< T > &center=vector2d< T >())
rotates the point anticlockwise around a center by an amount of degrees.
Definition: vector2d.h:166
vector2d< T > & operator=(const vector2d< T > &other)
Definition: vector2d.h:39
Everything in the Irrlicht Engine can be found in this namespace.
Definition: aabbox3d.h:12
vector2d< T > getInterpolated_quadratic(const vector2d< T > &v2, const vector2d< T > &v3, f64 d) const
Creates a quadratically interpolated vector between this and two other vectors.
Definition: vector2d.h:314
bool operator!=(const vector2d< T > &other) const
Definition: vector2d.h:96
f64 getAngleWith(const vector2d< T > &b) const
Calculates the angle between this vector and another one in degree.
Definition: vector2d.h:253
Specifies a 2 dimensional size.
Definition: dimension2d.h:20
bool nearlyParallel(const vector2d< T > &other, const T factor=relativeErrorFactor< T >()) const
check if this vector is parallel to another vector
Definition: vector2d.h:131
double f64
64 bit floating point variable.
Definition: irrTypes.h:112
vector2d(const dimension2d< T > &other)
Definition: vector2d.h:33
bool equals(const vector2d< T > &other, const T tolerance=(T) ROUNDING_ERROR_f32) const
Checks if this vector equals the other one.
Definition: vector2d.h:105
vector2d(T nx, T ny)
Constructor with two different values.
Definition: vector2d.h:27
vector2d< T > getInterpolated(const vector2d< T > &other, f64 d) const
Creates an interpolated vector between this vector and another vector.
Definition: vector2d.h:302
vector2d< T > & operator+=(const dimension2d< T > &other)
Definition: vector2d.h:48
64bit constant for converting from degrees to radians (formally known as GRAD_PI2)
Definition: irrMath.h:81
const f32 ROUNDING_ERROR_f32
Definition: irrMath.h:50
CMatrix4< T > operator *(const T scalar, const CMatrix4< T > &mat)
Definition: matrix4.h:2370
vector2d()
Default constructor (null vector)
Definition: vector2d.h:25
bool operator==(const vector2d< T > &other) const
Definition: vector2d.h:95
bool equals(const T a, const T b, const T tolerance=roundingError< T >())
returns if a equals b, taking possible rounding errors into account
Definition: irrMath.h:246
vector2d< T > operator-(const dimension2d< T > &other) const
Definition: vector2d.h:51
signed int s32
32 bit signed variable.
Definition: irrTypes.h:70
bool operator<=(const vector2d< T > &other) const
sort in order X, Y. Equality with rounding tolerance.
Definition: vector2d.h:68
vector2d< T > & operator *=(const vector2d< T > &other)
Definition: vector2d.h:58
f64 getAngle() const
Calculates the angle of this vector in degrees in the counter trigonometric sense.
Definition: vector2d.h:223
vector2d< T > operator/(const vector2d< T > &other) const
Definition: vector2d.h:62
vector2d< T > operator+(const vector2d< T > &other) const
Definition: vector2d.h:43
vector2d< T > & normalize()
Normalize the vector.
Definition: vector2d.h:185
T getLength() const
Gets the length of the vector.
Definition: vector2d.h:115
T Width
Width of the dimension.
Definition: dimension2d.h:204
vector2d< T > & interpolate(const vector2d< T > &a, const vector2d< T > &b, f64 d)
Sets this vector to the linearly interpolated vector between a and b.
Definition: vector2d.h:374
T dotProduct(const vector2d< T > &other) const
Get the dot product of this vector with another.
Definition: vector2d.h:125
vector2d< T > & operator+=(const vector2d< T > &other)
Definition: vector2d.h:45
T getDistanceFromSQ(const vector2d< T > &other) const
Returns squared distance from another point.
Definition: vector2d.h:157
vector2d< T > & operator-=(const dimension2d< T > &other)
Definition: vector2d.h:55
vector2d(T n)
Constructor with the same value for both members.
Definition: vector2d.h:29
vector2d(const vector2d< T > &other)
Copy constructor.
Definition: vector2d.h:31
vector2d< T > & operator-=(const vector2d< T > &other)
Definition: vector2d.h:52
vector2d< T > operator+(const T v) const
Definition: vector2d.h:46
vector2d< T > & operator/=(const T v)
Definition: vector2d.h:65
f64 getAngleTrig() const
Calculates the angle of this vector in degrees in the trigonometric sense.
Definition: vector2d.h:200
bool areCounterClockwise(const vector2d< T > &b, const vector2d< T > &c) const
Definition: vector2d.h:360
2d vector template class with lots of operators and methods.
Definition: dimension2d.h:16
bool isBetweenPoints(const vector2d< T > &begin, const vector2d< T > &end) const
Returns if this vector interpreted as a point is on a line between two other points.
Definition: vector2d.h:274
vector2d< T > & operator+=(const T v)
Definition: vector2d.h:47
vector2d< T > & operator-=(const T v)
Definition: vector2d.h:54
vector2d< T > operator+(const dimension2d< T > &other) const
Definition: vector2d.h:44
T Height
Height of the dimension.
Definition: dimension2d.h:206
vector2d< T > operator/(const T v) const
Definition: vector2d.h:64
bool operator>(const vector2d< T > &other) const
sort in order X, Y. Difference must be above rounding tolerance.
Definition: vector2d.h:89
REALINLINE f64 reciprocal_squareroot(const f64 x)
Definition: irrMath.h:532
vector2d< T > & operator/=(const vector2d< T > &other)
Definition: vector2d.h:63
const T clamp(const T &value, const T &low, const T &high)
clamps a value between low and high
Definition: irrMath.h:167
vector2d< s32 > vector2di
Typedef for integer 2d vector.
Definition: vector2d.h:392
vector2d< T > & operator=(const dimension2d< T > &other)
Definition: vector2d.h:41
vector2d< f32 > vector2df
Typedef for f32 2d vector.
Definition: vector2d.h:389
T X
X coordinate of vector.
Definition: vector2d.h:382
T getLengthSQ() const
Get the squared length of this vector.
Definition: vector2d.h:120
T getDistanceFrom(const vector2d< T > &other) const
Gets distance from another point.
Definition: vector2d.h:148
bool operator>=(const vector2d< T > &other) const
sort in order X, Y. Equality with rounding tolerance.
Definition: vector2d.h:75