arsa  2.7
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  {
69  _IRR_DEBUG_BREAK_IF(index>1) // access violation
70 
71  return *(&X+index);
72  }
73 
74  const T& operator [](u32 index) const
75  {
76  _IRR_DEBUG_BREAK_IF(index>1) // access violation
77 
78  return *(&X+index);
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 
96  bool operator<(const vector2d<T>&other) const
97  {
98  return (X<other.X && !core::equals(X, other.X)) ||
99  (core::equals(X, other.X) && Y<other.Y && !core::equals(Y, other.Y));
100  }
101 
103  bool operator>(const vector2d<T>&other) const
104  {
105  return (X>other.X && !core::equals(X, other.X)) ||
106  (core::equals(X, other.X) && Y>other.Y && !core::equals(Y, other.Y));
107  }
108 
109  bool operator==(const vector2d<T>& other) const { return equals(other); }
110  bool operator!=(const vector2d<T>& other) const { return !equals(other); }
111 
112  // functions
113 
115 
119  bool equals(const vector2d<T>& other, const T tolerance = (T)ROUNDING_ERROR_f32 ) const
120  {
121  return core::equals(X, other.X, tolerance) && core::equals(Y, other.Y, tolerance);
122  }
123 
124  vector2d<T>& set(T nx, T ny) {X=nx; Y=ny; return *this; }
125  vector2d<T>& set(const vector2d<T>& p) { X=p.X; Y=p.Y; return *this; }
126 
128 
129  T getLength() const { return core::squareroot( X*X + Y*Y ); }
130 
132 
134  T getLengthSQ() const { return X*X + Y*Y; }
135 
137 
139  T dotProduct(const vector2d<T>& other) const
140  {
141  return X*other.X + Y*other.Y;
142  }
143 
145  bool nearlyParallel( const vector2d<T> & other, const T factor = relativeErrorFactor<T>()) const
146  {
147  // https://eagergames.wordpress.com/2017/04/01/fast-parallel-lines-and-vectors-test/
148  // if a || b then a.x/a.y = b.x/b.y (similiar triangles)
149  // if a || b then either both x are 0 or both y are 0.
150 
151  return equalsRelative( X*other.Y, other.X* Y, factor)
152  && // a bit counterintuitive, but makes sure that
153  // only y or only x are 0, and at same time deals
154  // with the case where one vector is zero vector.
155  (X*other.X + Y*other.Y) != 0;
156  }
157 
159 
162  T getDistanceFrom(const vector2d<T>& other) const
163  {
164  return vector2d<T>(X - other.X, Y - other.Y).getLength();
165  }
166 
168 
171  T getDistanceFromSQ(const vector2d<T>& other) const
172  {
173  return vector2d<T>(X - other.X, Y - other.Y).getLengthSQ();
174  }
175 
177 
180  vector2d<T>& rotateBy(f64 degrees, const vector2d<T>& center=vector2d<T>())
181  {
182  degrees *= DEGTORAD64;
183  const f64 cs = cos(degrees);
184  const f64 sn = sin(degrees);
185 
186  X -= center.X;
187  Y -= center.Y;
188 
189  set((T)(X*cs - Y*sn), (T)(X*sn + Y*cs));
190 
191  X += center.X;
192  Y += center.Y;
193  return *this;
194  }
195 
197 
200  {
201  f32 length = (f32)(X*X + Y*Y);
202  if ( length == 0 )
203  return *this;
205  X = (T)(X * length);
206  Y = (T)(Y * length);
207  return *this;
208  }
209 
211 
215  {
216  if (Y == 0)
217  return X < 0 ? 180 : 0;
218  else
219  if (X == 0)
220  return Y < 0 ? 270 : 90;
221 
222  if ( Y > 0)
223  if (X > 0)
224  return atan((irr::f64)Y/(irr::f64)X) * RADTODEG64;
225  else
226  return 180.0-atan((irr::f64)Y/-(irr::f64)X) * RADTODEG64;
227  else
228  if (X > 0)
229  return 360.0-atan(-(irr::f64)Y/(irr::f64)X) * RADTODEG64;
230  else
231  return 180.0+atan(-(irr::f64)Y/-(irr::f64)X) * RADTODEG64;
232  }
233 
235 
237  inline f64 getAngle() const
238  {
239  if (Y == 0) // corrected thanks to a suggestion by Jox
240  return X < 0 ? 180 : 0;
241  else if (X == 0)
242  return Y < 0 ? 90 : 270;
243 
244  // don't use getLength here to avoid precision loss with s32 vectors
245  // avoid floating-point trouble as sqrt(y*y) is occasionally larger than y, so clamp
246  const f64 tmp = core::clamp(Y / sqrt((f64)(X*X + Y*Y)), -1.0, 1.0);
247  const f64 angle = atan( core::squareroot(1 - tmp*tmp) / tmp) * RADTODEG64;
248 
249  if (X>0 && Y>0)
250  return angle + 270;
251  else
252  if (X>0 && Y<0)
253  return angle + 90;
254  else
255  if (X<0 && Y<0)
256  return 90 - angle;
257  else
258  if (X<0 && Y>0)
259  return 270 - angle;
260 
261  return angle;
262  }
263 
265 
267  inline f64 getAngleWith(const vector2d<T>& b) const
268  {
269  f64 tmp = (f64)(X*b.X + Y*b.Y);
270 
271  if (tmp == 0.0)
272  return 90.0;
273 
274  tmp = tmp / core::squareroot((f64)((X*X + Y*Y) * (b.X*b.X + b.Y*b.Y)));
275  if (tmp < 0.0)
276  tmp = -tmp;
277  if ( tmp > 1.0 ) // avoid floating-point trouble
278  tmp = 1.0;
279 
280  return atan(sqrt(1 - tmp*tmp) / tmp) * RADTODEG64;
281  }
282 
284 
288  bool isBetweenPoints(const vector2d<T>& begin, const vector2d<T>& end) const
289  {
290  // . end
291  // /
292  // /
293  // /
294  // . begin
295  // -
296  // -
297  // . this point (am I inside or outside)?
298  //
299  if (begin.X != end.X)
300  {
301  return ((begin.X <= X && X <= end.X) ||
302  (begin.X >= X && X >= end.X));
303  }
304  else
305  {
306  return ((begin.Y <= Y && Y <= end.Y) ||
307  (begin.Y >= Y && Y >= end.Y));
308  }
309  }
310 
312 
317  {
318  const f64 inv = 1.0f - d;
319  return vector2d<T>((T)(other.X*inv + X*d), (T)(other.Y*inv + Y*d));
320  }
321 
323 
329  {
330  // this*(1-d)*(1-d) + 2 * v2 * (1-d) + v3 * d * d;
331  const f64 inv = 1.0f - d;
332  const f64 mul0 = inv * inv;
333  const f64 mul1 = 2.0f * d * inv;
334  const f64 mul2 = d * d;
335 
336  return vector2d<T> ( (T)(X * mul0 + v2.X * mul1 + v3.X * mul2),
337  (T)(Y * mul0 + v2.Y * mul1 + v3.Y * mul2));
338  }
339 
345  s32 checkOrientation( const vector2d<T> & b, const vector2d<T> & c) const
346  {
347  // Example of clockwise points
348  //
349  // ^ Y
350  // | A
351  // | . .
352  // | . .
353  // | C.....B
354  // +---------------> X
355 
356  T val = (b.Y - Y) * (c.X - b.X) -
357  (b.X - X) * (c.Y - b.Y);
358 
359  if (val == 0) return 0; // colinear
360 
361  return (val > 0) ? 1 : 2; // clock or counterclock wise
362  }
363 
365  inline bool areClockwise( const vector2d<T> & b, const vector2d<T> & c) const
366  {
367  T val = (b.Y - Y) * (c.X - b.X) -
368  (b.X - X) * (c.Y - b.Y);
369 
370  return val > 0;
371  }
372 
374  inline bool areCounterClockwise( const vector2d<T> & b, const vector2d<T> & c) const
375  {
376  T val = (b.Y - Y) * (c.X - b.X) -
377  (b.X - X) * (c.Y - b.Y);
378 
379  return val < 0;
380  }
381 
383 
389  {
390  X = (T)((f64)b.X + ( ( a.X - b.X ) * d ));
391  Y = (T)((f64)b.Y + ( ( a.Y - b.Y ) * d ));
392  return *this;
393  }
394 
396  T X;
397 
399  T Y;
400 };
401 
404 
407 
408  template<class S, class T>
409  vector2d<T> operator*(const S scalar, const vector2d<T>& vector) { return vector*scalar; }
410 
411  // These methods are declared in dimension2d, but need definitions of vector2d
412  template<class T>
413  dimension2d<T>::dimension2d(const vector2d<T>& other) : Width(other.X), Height(other.Y) { }
414 
415  template<class T>
416  bool dimension2d<T>::operator==(const vector2d<T>& other) const { return Width == other.X && Height == other.Y; }
417 
418 } // end namespace core
419 } // end namespace irr
420 
421 #endif
422 
bool operator<(const vector2d< T > &other) const
sort in order X, Y. Difference must be above rounding tolerance.
Definition: vector2d.h:96
const f64 RADTODEG64
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:124
GLdouble n
s32 checkOrientation(const vector2d< T > &b, const vector2d< T > &c) const
Definition: vector2d.h:345
vector2d< T > operator-(const vector2d< T > &other) const
Definition: vector2d.h:50
float f32
32 bit floating point variable.
Definition: irrTypes.h:108
T & operator [](u32 index)
Definition: vector2d.h:67
GLuint GLuint end
Definition: SDL_opengl.h:1571
GLfloat GLfloat p
REALINLINE f32 squareroot(const f32 f)
Definition: irrMath.h:495
T Y
Y coordinate of vector.
Definition: vector2d.h:399
vector2d< T > operator *(const vector2d< T > &other) const
Definition: vector2d.h:57
vector2d< T > & set(const vector2d< T > &p)
Definition: vector2d.h:125
bool areClockwise(const vector2d< T > &b, const vector2d< T > &c) const
Definition: vector2d.h:365
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:180
vector2d< T > & operator=(const vector2d< T > &other)
Definition: vector2d.h:39
Everything in the Irrlicht Engine can be found in this namespace.
Definition: CARSADPad.h:6
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:328
GLfloat GLfloat GLfloat v2
bool operator!=(const vector2d< T > &other) const
Definition: vector2d.h:110
f64 getAngleWith(const vector2d< T > &b) const
Calculates the angle between this vector and another one in degree.
Definition: vector2d.h:267
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:145
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:119
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:316
vector2d< T > & operator+=(const dimension2d< T > &other)
Definition: vector2d.h:48
const GLdouble * v
Definition: SDL_opengl.h:2064
const f64 DEGTORAD64
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
GLfloat GLfloat GLfloat GLfloat v3
GLfixed ny
bool operator==(const vector2d< T > &other) const
Definition: vector2d.h:109
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
GLuint GLfloat * val
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:82
bool operator==(const CustomAllocator< T > &, const CustomAllocator< U > &)
Definition: Effekseer.h:788
unsigned int u32
32 bit unsigned variable.
Definition: irrTypes.h:62
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:237
GLbyte nx
vector2d< T > operator/(const vector2d< T > &other) const
Definition: vector2d.h:62
#define _IRR_DEBUG_BREAK_IF(_CONDITION_)
define a break macro for debugging.
Definition: irrTypes.h:185
vector2d< T > operator+(const vector2d< T > &other) const
Definition: vector2d.h:43
vector2d< T > & normalize()
Normalize the vector.
Definition: vector2d.h:199
T getLength() const
Gets the length of the vector.
Definition: vector2d.h:129
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:388
T dotProduct(const vector2d< T > &other) const
Get the dot product of this vector with another.
Definition: vector2d.h:139
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:171
GLuint index
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:214
bool areCounterClockwise(const vector2d< T > &b, const vector2d< T > &c) const
Definition: vector2d.h:374
GLboolean GLboolean GLboolean b
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:288
vector2d< T > & operator+=(const T v)
Definition: vector2d.h:47
vector2d< T > & operator-=(const T v)
Definition: vector2d.h:54
GLfloat angle
vector2d< T > operator+(const dimension2d< T > &other) const
Definition: vector2d.h:44
GLboolean GLboolean GLboolean GLboolean a
T Height
Height of the dimension.
Definition: dimension2d.h:206
const T & operator [](u32 index) const
Definition: vector2d.h:74
GLuint GLsizei GLsizei * length
const GLubyte * c
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:103
REALINLINE f64 reciprocal_squareroot(const f64 x)
Definition: irrMath.h:521
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:406
vector2d< T > & operator=(const dimension2d< T > &other)
Definition: vector2d.h:41
vector2d< f32 > vector2df
Typedef for f32 2d vector.
Definition: vector2d.h:403
T X
X coordinate of vector.
Definition: vector2d.h:396
T getLengthSQ() const
Get the squared length of this vector.
Definition: vector2d.h:134
T getDistanceFrom(const vector2d< T > &other) const
Gets distance from another point.
Definition: vector2d.h:162
bool operator>=(const vector2d< T > &other) const
sort in order X, Y. Equality with rounding tolerance.
Definition: vector2d.h:89