Irrlicht 3D Engine
 
Loading...
Searching...
No Matches
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
11namespace irr
12{
13namespace core
14{
15
16
18
20template <class T>
22{
23public:
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) {}
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
104 bool equals(const vector2d<T>& other) const
105 {
106 return core::equals(X, other.X) && core::equals(Y, other.Y);
107 }
108
109 vector2d<T>& set(T nx, T ny) {X=nx; Y=ny; return *this; }
110 vector2d<T>& set(const vector2d<T>& p) { X=p.X; Y=p.Y; return *this; }
111
113
114 T getLength() const { return core::squareroot( X*X + Y*Y ); }
115
117
119 T getLengthSQ() const { return X*X + Y*Y; }
120
122
125 {
126 return X*other.X + Y*other.Y;
127 }
128
130
134 {
135 return vector2d<T>(X - other.X, Y - other.Y).getLength();
136 }
137
139
143 {
144 return vector2d<T>(X - other.X, Y - other.Y).getLengthSQ();
145 }
146
148
152 {
154 const f64 cs = cos(degrees);
155 const f64 sn = sin(degrees);
156
157 X -= center.X;
158 Y -= center.Y;
159
160 set((T)(X*cs - Y*sn), (T)(X*sn + Y*cs));
161
162 X += center.X;
163 Y += center.Y;
164 return *this;
165 }
166
168
171 {
172 f32 length = (f32)(X*X + Y*Y);
173 if ( length == 0 )
174 return *this;
176 X = (T)(X * length);
177 Y = (T)(Y * length);
178 return *this;
179 }
180
182
186 {
187 if (Y == 0)
188 return X < 0 ? 180 : 0;
189 else
190 if (X == 0)
191 return Y < 0 ? 270 : 90;
192
193 if ( Y > 0)
194 if (X > 0)
195 return atan((irr::f64)Y/(irr::f64)X) * RADTODEG64;
196 else
197 return 180.0-atan((irr::f64)Y/-(irr::f64)X) * RADTODEG64;
198 else
199 if (X > 0)
200 return 360.0-atan(-(irr::f64)Y/(irr::f64)X) * RADTODEG64;
201 else
202 return 180.0+atan(-(irr::f64)Y/-(irr::f64)X) * RADTODEG64;
203 }
204
206
208 inline f64 getAngle() const
209 {
210 if (Y == 0) // corrected thanks to a suggestion by Jox
211 return X < 0 ? 180 : 0;
212 else if (X == 0)
213 return Y < 0 ? 90 : 270;
214
215 // don't use getLength here to avoid precision loss with s32 vectors
216 // avoid floating-point trouble as sqrt(y*y) is occasionally larger than y, so clamp
217 const f64 tmp = core::clamp(Y / sqrt((f64)(X*X + Y*Y)), -1.0, 1.0);
218 const f64 angle = atan( core::squareroot(1 - tmp*tmp) / tmp) * RADTODEG64;
219
220 if (X>0 && Y>0)
221 return angle + 270;
222 else
223 if (X>0 && Y<0)
224 return angle + 90;
225 else
226 if (X<0 && Y<0)
227 return 90 - angle;
228 else
229 if (X<0 && Y>0)
230 return 270 - angle;
231
232 return angle;
233 }
234
236
238 inline f64 getAngleWith(const vector2d<T>& b) const
239 {
240 f64 tmp = (f64)(X*b.X + Y*b.Y);
241
242 if (tmp == 0.0)
243 return 90.0;
244
245 tmp = tmp / core::squareroot((f64)((X*X + Y*Y) * (b.X*b.X + b.Y*b.Y)));
246 if (tmp < 0.0)
247 tmp = -tmp;
248 if ( tmp > 1.0 ) // avoid floating-point trouble
249 tmp = 1.0;
250
251 return atan(sqrt(1 - tmp*tmp) / tmp) * RADTODEG64;
252 }
253
255
259 bool isBetweenPoints(const vector2d<T>& begin, const vector2d<T>& end) const
260 {
261 if (begin.X != end.X)
262 {
263 return ((begin.X <= X && X <= end.X) ||
264 (begin.X >= X && X >= end.X));
265 }
266 else
267 {
268 return ((begin.Y <= Y && Y <= end.Y) ||
269 (begin.Y >= Y && Y >= end.Y));
270 }
271 }
272
274
279 {
280 f64 inv = 1.0f - d;
281 return vector2d<T>((T)(other.X*inv + X*d), (T)(other.Y*inv + Y*d));
282 }
283
285
291 {
292 // this*(1-d)*(1-d) + 2 * v2 * (1-d) + v3 * d * d;
293 const f64 inv = 1.0f - d;
294 const f64 mul0 = inv * inv;
295 const f64 mul1 = 2.0f * d * inv;
296 const f64 mul2 = d * d;
297
298 return vector2d<T> ( (T)(X * mul0 + v2.X * mul1 + v3.X * mul2),
299 (T)(Y * mul0 + v2.Y * mul1 + v3.Y * mul2));
300 }
301
303
309 {
310 X = (T)((f64)b.X + ( ( a.X - b.X ) * d ));
311 Y = (T)((f64)b.Y + ( ( a.Y - b.Y ) * d ));
312 return *this;
313 }
314
317
320};
321
324
327
328 template<class S, class T>
329 vector2d<T> operator*(const S scalar, const vector2d<T>& vector) { return vector*scalar; }
330
331 // These methods are declared in dimension2d, but need definitions of vector2d
332 template<class T>
333 dimension2d<T>::dimension2d(const vector2d<T>& other) : Width(other.X), Height(other.Y) { }
334
335 template<class T>
336 bool dimension2d<T>::operator==(const vector2d<T>& other) const { return Width == other.X && Height == other.Y; }
337
338} // end namespace core
339} // end namespace irr
340
341#endif
342
Axis aligned bounding box in 3d dimensional space.
Definition aabbox3d.h:22
bool operator==(const dimension2d< T > &other) const
Equality operator.
Definition dimension2d.h:46
dimension2d()
Default constructor for empty dimension.
Definition dimension2d.h:24
2d vector template class with lots of operators and methods.
Definition vector2d.h:22
vector2d< T > & operator+=(const vector2d< T > &other)
Definition vector2d.h:45
vector2d< T > operator*(const T v) const
Definition vector2d.h:59
vector2d< T > & operator-=(const vector2d< T > &other)
Definition vector2d.h:52
vector2d< T > operator-() const
Definition vector2d.h:37
bool operator<(const vector2d< T > &other) const
sort in order X, Y. Difference must be above rounding tolerance.
Definition vector2d.h:82
vector2d< T > operator-(const vector2d< T > &other) const
Definition vector2d.h:50
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:308
vector2d< T > & operator-=(const dimension2d< T > &other)
Definition vector2d.h:55
vector2d< T > operator+(const dimension2d< T > &other) const
Definition vector2d.h:44
vector2d< T > operator+(const vector2d< T > &other) const
Definition vector2d.h:43
T getDistanceFrom(const vector2d< T > &other) const
Gets distance from another point.
Definition vector2d.h:133
vector2d< T > operator-(const dimension2d< T > &other) const
Definition vector2d.h:51
vector2d< T > operator/(const vector2d< T > &other) const
Definition vector2d.h:62
vector2d< T > & operator*=(const T v)
Definition vector2d.h:60
f64 getAngleTrig() const
Calculates the angle of this vector in degrees in the trigonometric sense.
Definition vector2d.h:185
vector2d< T > & operator/=(const T v)
Definition vector2d.h:65
vector2d< T > & operator*=(const vector2d< T > &other)
Definition vector2d.h:58
vector2d< T > operator/(const T v) const
Definition vector2d.h:64
vector2d< T > & operator=(const vector2d< T > &other)
Definition vector2d.h:39
vector2d()
Default constructor (null vector)
Definition vector2d.h:25
bool operator>(const vector2d< T > &other) const
sort in order X, Y. Difference must be above rounding tolerance.
Definition vector2d.h:89
vector2d< T > operator+(const T v) const
Definition vector2d.h:46
vector2d< T > & set(T nx, T ny)
Definition vector2d.h:109
vector2d< T > operator*(const vector2d< T > &other) const
Definition vector2d.h:57
f64 getAngleWith(const vector2d< T > &b) const
Calculates the angle between this vector and another one in degree.
Definition vector2d.h:238
T getLength() const
Gets the length of the vector.
Definition vector2d.h:114
vector2d(const vector2d< T > &other)
Copy constructor.
Definition vector2d.h:31
T X
X coordinate of vector.
Definition vector2d.h:316
vector2d< T > getInterpolated(const vector2d< T > &other, f64 d) const
Creates an interpolated vector between this vector and another vector.
Definition vector2d.h:278
bool equals(const vector2d< T > &other) const
Checks if this vector equals the other one.
Definition vector2d.h:104
vector2d< T > & operator=(const dimension2d< T > &other)
Definition vector2d.h:41
T Y
Y coordinate of vector.
Definition vector2d.h:319
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:290
vector2d< T > & operator+=(const dimension2d< T > &other)
Definition vector2d.h:48
bool operator!=(const vector2d< T > &other) const
Definition vector2d.h:96
T getDistanceFromSQ(const vector2d< T > &other) const
Returns squared distance from another point.
Definition vector2d.h:142
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:151
vector2d< T > & operator-=(const T v)
Definition vector2d.h:54
bool operator>=(const vector2d< T > &other) const
sort in order X, Y. Equality with rounding tolerance.
Definition vector2d.h:75
T getLengthSQ() const
Get the squared length of this vector.
Definition vector2d.h:119
vector2d< T > & set(const vector2d< T > &p)
Definition vector2d.h:110
bool operator<=(const vector2d< T > &other) const
sort in order X, Y. Equality with rounding tolerance.
Definition vector2d.h:68
T dotProduct(const vector2d< T > &other) const
Get the dot product of this vector with another.
Definition vector2d.h:124
vector2d(const dimension2d< T > &other)
Definition vector2d.h:33
f64 getAngle() const
Calculates the angle of this vector in degrees in the counter trigonometric sense.
Definition vector2d.h:208
vector2d(T nx, T ny)
Constructor with two different values.
Definition vector2d.h:27
vector2d< T > operator-(const T v) const
Definition vector2d.h:53
vector2d< T > & operator/=(const vector2d< T > &other)
Definition vector2d.h:63
vector2d< T > & normalize()
Normalize the vector.
Definition vector2d.h:170
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:259
vector2d< T > & operator+=(const T v)
Definition vector2d.h:47
bool operator==(const vector2d< T > &other) const
Definition vector2d.h:95
vector2d< f32 > vector2df
Typedef for f32 2d vector.
Definition vector2d.h:323
CMatrix4< T > operator*(const T scalar, const CMatrix4< T > &mat)
Definition matrix4.h:2228
const T clamp(const T &value, const T &low, const T &high)
clamps a value between low and high
Definition irrMath.h:166
const f64 DEGTORAD64
64bit constant for converting from degrees to radians (formally known as GRAD_PI2)
Definition irrMath.h:80
vector2d< s32 > vector2di
Typedef for integer 2d vector.
Definition vector2d.h:326
bool equals(const f64 a, const f64 b, const f64 tolerance=ROUNDING_ERROR_f64)
returns if a equals b, taking possible rounding errors into account
Definition irrMath.h:185
REALINLINE f64 reciprocal_squareroot(const f64 x)
Definition irrMath.h:497
const f64 RADTODEG64
64bit constant for converting from radians to degrees
Definition irrMath.h:83
REALINLINE f32 squareroot(const f32 f)
Definition irrMath.h:471
Everything in the Irrlicht Engine can be found in this namespace.
Definition aabbox3d.h:13
float f32
32 bit floating point variable.
Definition irrTypes.h:104
double f64
64 bit floating point variable.
Definition irrTypes.h:108