Nirtcpp 2.1.0
Nirtcpp is a high-performance c++ graphics engine.
Loading...
Searching...
No Matches
vector2d.hpp
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 nirtcpp/nirtcpp.hpp
4
5#ifndef NIRT_POINT_2D_HPP_INCLUDED
6#define NIRT_POINT_2D_HPP_INCLUDED
7
8#include <nirtcpp/core/engine/irrMath.hpp>
9#include <nirtcpp/core/engine/dimension2d.hpp>
10
11namespace nirt
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) {}
30
31 vector2d(const dimension2d<T>& other) : X(other.Width), Y(other.Height) {}
32
33 // operators
34
35 vector2d<T> operator-() const { return vector2d<T>(-X, -Y); }
36
37 vector2d<T>& operator=(const dimension2d<T>& other) { X = other.Width; Y = other.Height; return *this; }
38
39 vector2d<T> operator+(const vector2d<T>& other) const { return vector2d<T>(X + other.X, Y + other.Y); }
40 vector2d<T> operator+(const dimension2d<T>& other) const { return vector2d<T>(X + other.Width, Y + other.Height); }
41 vector2d<T>& operator+=(const vector2d<T>& other) { X+=other.X; Y+=other.Y; return *this; }
42 vector2d<T> operator+(const T v) const { return vector2d<T>(X + v, Y + v); }
43 vector2d<T>& operator+=(const T v) { X+=v; Y+=v; return *this; }
44 vector2d<T>& operator+=(const dimension2d<T>& other) { X += other.Width; Y += other.Height; return *this; }
45
46 vector2d<T> operator-(const vector2d<T>& other) const { return vector2d<T>(X - other.X, Y - other.Y); }
47 vector2d<T> operator-(const dimension2d<T>& other) const { return vector2d<T>(X - other.Width, Y - other.Height); }
48 vector2d<T>& operator-=(const vector2d<T>& other) { X-=other.X; Y-=other.Y; return *this; }
49 vector2d<T> operator-(const T v) const { return vector2d<T>(X - v, Y - v); }
50 vector2d<T>& operator-=(const T v) { X-=v; Y-=v; return *this; }
51 vector2d<T>& operator-=(const dimension2d<T>& other) { X -= other.Width; Y -= other.Height; return *this; }
52
53 vector2d<T> operator*(const vector2d<T>& other) const { return vector2d<T>(X * other.X, Y * other.Y); }
54 vector2d<T>& operator*=(const vector2d<T>& other) { X*=other.X; Y*=other.Y; return *this; }
55 vector2d<T> operator*(const T v) const { return vector2d<T>(X * v, Y * v); }
56 vector2d<T>& operator*=(const T v) { X*=v; Y*=v; return *this; }
57
58 vector2d<T> operator/(const vector2d<T>& other) const { return vector2d<T>(X / other.X, Y / other.Y); }
59 vector2d<T>& operator/=(const vector2d<T>& other) { X/=other.X; Y/=other.Y; return *this; }
60 vector2d<T> operator/(const T v) const { return vector2d<T>(X / v, Y / v); }
61 vector2d<T>& operator/=(const T v) { X/=v; Y/=v; return *this; }
62
63 T& operator [](u32 index)
64 {
65 NIRT_DEBUG_BREAK_IF(index>1) // access violation
66
67 return *(&X+index);
68 }
69
70 const T& operator [](u32 index) const
71 {
72 NIRT_DEBUG_BREAK_IF(index>1) // access violation
73
74 return *(&X+index);
75 }
76
78 bool operator<=(const vector2d<T>&other) const
79 {
80 return (X<other.X || core::equals(X, other.X)) ||
81 (core::equals(X, other.X) && (Y<other.Y || core::equals(Y, other.Y)));
82 }
83
85 bool operator>=(const vector2d<T>&other) const
86 {
87 return (X>other.X || core::equals(X, other.X)) ||
88 (core::equals(X, other.X) && (Y>other.Y || core::equals(Y, other.Y)));
89 }
90
92 bool operator<(const vector2d<T>&other) const
93 {
94 return (X<other.X && !core::equals(X, other.X)) ||
95 (core::equals(X, other.X) && Y<other.Y && !core::equals(Y, other.Y));
96 }
97
99 bool operator>(const vector2d<T>&other) const
100 {
101 return (X>other.X && !core::equals(X, other.X)) ||
102 (core::equals(X, other.X) && Y>other.Y && !core::equals(Y, other.Y));
103 }
104
105 bool operator==(const vector2d<T>& other) const { return equals(other); }
106 bool operator!=(const vector2d<T>& other) const { return !equals(other); }
107
108 // functions
109
111
115 bool equals(const vector2d<T>& other, const T tolerance = (T)ROUNDING_ERROR_f32 ) const
116 {
118 }
119
120 vector2d<T>& set(T nx, T ny) {X=nx; Y=ny; return *this; }
121 vector2d<T>& set(const vector2d<T>& p) { X=p.X; Y=p.Y; return *this; }
122
124
125 T getLength() const { return core::squareroot( X*X + Y*Y ); }
126
128
130 T getLengthSQ() const { return X*X + Y*Y; }
131
133
136 {
137 return X*other.X + Y*other.Y;
138 }
139
142 {
143 // https://eagergames.wordpress.com/2017/04/01/fast-parallel-lines-and-vectors-test/
144 // if a || b then a.x/a.y = b.x/b.y (similiar triangles)
145 // if a || b then either both x are 0 or both y are 0.
146
147 return equalsRelative( X*other.Y, other.X* Y, factor)
148 && // a bit counterintuitive, but makes sure that
149 // only y or only x are 0, and at same time deals
150 // with the case where one vector is zero vector.
151 (X*other.X + Y*other.Y) != 0;
152 }
153
155
159 {
160 return vector2d<T>(X - other.X, Y - other.Y).getLength();
161 }
162
164
168 {
169 return vector2d<T>(X - other.X, Y - other.Y).getLengthSQ();
170 }
171
173
177 {
179 const f64 cs = cos(degrees);
180 const f64 sn = sin(degrees);
181
182 X -= center.X;
183 Y -= center.Y;
184
185 set((T)(X*cs - Y*sn), (T)(X*sn + Y*cs));
186
187 X += center.X;
188 Y += center.Y;
189 return *this;
190 }
191
193
196 {
197 f32 length = (f32)(X*X + Y*Y);
198 if ( length == 0 )
199 return *this;
200 length = core::reciprocal_squareroot ( length );
201 X = (T)(X * length);
202 Y = (T)(Y * length);
203 return *this;
204 }
205
207
211 {
212 if (Y == 0)
213 return X < 0 ? 180 : 0;
214 else
215 if (X == 0)
216 return Y < 0 ? 270 : 90;
217
218 if ( Y > 0)
219 if (X > 0)
220 return atan((nirt::f64)Y/(nirt::f64)X) * RADTODEG64;
221 else
222 return 180.0-atan((nirt::f64)Y/-(nirt::f64)X) * RADTODEG64;
223 else
224 if (X > 0)
225 return 360.0-atan(-(nirt::f64)Y/(nirt::f64)X) * RADTODEG64;
226 else
227 return 180.0+atan(-(nirt::f64)Y/-(nirt::f64)X) * RADTODEG64;
228 }
229
231
233 inline f64 getAngle() const
234 {
235 if (Y == 0) // corrected thanks to a suggestion by Jox
236 return X < 0 ? 180 : 0;
237 else if (X == 0)
238 return Y < 0 ? 90 : 270;
239
240 // don't use getLength here to avoid precision loss with s32 vectors
241 // avoid floating-point trouble as sqrt(y*y) is occasionally larger than y, so clamp
242 const f64 tmp = core::clamp(Y / sqrt((f64)(X*X + Y*Y)), -1.0, 1.0);
243 const f64 angle = atan( core::squareroot(1 - tmp*tmp) / tmp) * RADTODEG64;
244
245 if (X>0 && Y>0)
246 return angle + 270;
247 else
248 if (X>0 && Y<0)
249 return angle + 90;
250 else
251 if (X<0 && Y<0)
252 return 90 - angle;
253 else
254 if (X<0 && Y>0)
255 return 270 - angle;
256
257 return angle;
258 }
259
261
263 inline f64 getAngleWith(const vector2d<T>& b) const
264 {
265 f64 tmp = (f64)(X*b.X + Y*b.Y);
266
267 if (tmp == 0.0)
268 return 90.0;
269
270 tmp = tmp / core::squareroot((f64)((X*X + Y*Y) * (b.X*b.X + b.Y*b.Y)));
271 if (tmp < 0.0)
272 tmp = -tmp;
273 if ( tmp > 1.0 ) // avoid floating-point trouble
274 tmp = 1.0;
275
276 return atan(sqrt(1 - tmp*tmp) / tmp) * RADTODEG64;
277 }
278
280
284 bool isBetweenPoints(const vector2d<T>& begin, const vector2d<T>& end) const
285 {
286 // . end
287 // /
288 // /
289 // /
290 // . begin
291 // -
292 // -
293 // . this point (am I inside or outside)?
294 //
295 if (begin.X != end.X)
296 {
297 return ((begin.X <= X && X <= end.X) ||
298 (begin.X >= X && X >= end.X));
299 }
300 else
301 {
302 return ((begin.Y <= Y && Y <= end.Y) ||
303 (begin.Y >= Y && Y >= end.Y));
304 }
305 }
306
308
313 {
314 const f64 inv = 1.0f - d;
315 return vector2d<T>((T)(other.X*inv + X*d), (T)(other.Y*inv + Y*d));
316 }
317
319
325 {
326 // this*(1-d)*(1-d) + 2 * v2 * (1-d) + v3 * d * d;
327 const f64 inv = 1.0f - d;
328 const f64 mul0 = inv * inv;
329 const f64 mul1 = 2.0f * d * inv;
330 const f64 mul2 = d * d;
331
332 return vector2d<T> ( (T)(X * mul0 + v2.X * mul1 + v3.X * mul2),
333 (T)(Y * mul0 + v2.Y * mul1 + v3.Y * mul2));
334 }
335
341 s32 checkOrientation( const vector2d<T> & b, const vector2d<T> & c) const
342 {
343 // Example of clockwise points
344 //
345 // ^ Y
346 // | A
347 // | . .
348 // | . .
349 // | C.....B
350 // +---------------> X
351
352 T val = (b.Y - Y) * (c.X - b.X) -
353 (b.X - X) * (c.Y - b.Y);
354
355 if (val == 0) return 0; // colinear
356
357 return (val > 0) ? 1 : 2; // clock or counterclock wise
358 }
359
361 inline bool areClockwise( const vector2d<T> & b, const vector2d<T> & c) const
362 {
363 T val = (b.Y - Y) * (c.X - b.X) -
364 (b.X - X) * (c.Y - b.Y);
365
366 return val > 0;
367 }
368
370 inline bool areCounterClockwise( const vector2d<T> & b, const vector2d<T> & c) const
371 {
372 T val = (b.Y - Y) * (c.X - b.X) -
373 (b.X - X) * (c.Y - b.Y);
374
375 return val < 0;
376 }
377
379
385 {
386 X = (T)((f64)b.X + ( ( a.X - b.X ) * d ));
387 Y = (T)((f64)b.Y + ( ( a.Y - b.Y ) * d ));
388 return *this;
389 }
390
393
396};
397
400
403
404 template<class S, class T>
405 vector2d<T> operator*(const S scalar, const vector2d<T>& vector) { return vector*scalar; }
406
407 // These methods are declared in dimension2d, but need definitions of vector2d
408 template<class T>
409 dimension2d<T>::dimension2d(const vector2d<T>& other) : Width(other.X), Height(other.Y) { }
410
411 template<class T>
412 bool dimension2d<T>::operator==(const vector2d<T>& other) const { return Width == other.X && Height == other.Y; }
413
414} // end namespace core
415} // end namespace nirt
416
417#endif
Axis aligned bounding box in 3d dimensional space.
Definition aabbox3d.hpp:22
dimension2d()
Default constructor for empty dimension.
Definition dimension2d.hpp:24
2d vector template class with lots of operators and methods.
Definition vector2d.hpp:22
vector2d(T nx, T ny)
Constructor with two different values.
Definition vector2d.hpp:27
T getLengthSQ() const
Get the squared length of this vector.
Definition vector2d.hpp:130
vector2d< T > getInterpolated(const vector2d< T > &other, f64 d) const
Creates an interpolated vector between this vector and another vector.
Definition vector2d.hpp:312
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.hpp:324
vector2d()
Default constructor (null vector)
Definition vector2d.hpp:25
T Y
Y coordinate of vector.
Definition vector2d.hpp:395
vector2d< T > & normalize()
Normalize the vector.
Definition vector2d.hpp:195
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.hpp:176
bool areClockwise(const vector2d< T > &b, const vector2d< T > &c) const
Definition vector2d.hpp:361
bool operator>(const vector2d< T > &other) const
sort in order X, Y. Difference must be above rounding tolerance.
Definition vector2d.hpp:99
f64 getAngleWith(const vector2d< T > &b) const
Calculates the angle between this vector and another one in degree.
Definition vector2d.hpp:263
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.hpp:284
bool areCounterClockwise(const vector2d< T > &b, const vector2d< T > &c) const
Definition vector2d.hpp:370
s32 checkOrientation(const vector2d< T > &b, const vector2d< T > &c) const
Definition vector2d.hpp:341
bool operator<(const vector2d< T > &other) const
sort in order X, Y. Difference must be above rounding tolerance.
Definition vector2d.hpp:92
bool operator>=(const vector2d< T > &other) const
sort in order X, Y. Equality with rounding tolerance.
Definition vector2d.hpp:85
T getDistanceFromSQ(const vector2d< T > &other) const
Returns squared distance from another point.
Definition vector2d.hpp:167
T getDistanceFrom(const vector2d< T > &other) const
Gets distance from another point.
Definition vector2d.hpp:158
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.hpp:384
T getLength() const
Gets the length of the vector.
Definition vector2d.hpp:125
T dotProduct(const vector2d< T > &other) const
Get the dot product of this vector with another.
Definition vector2d.hpp:135
vector2d(T n)
Constructor with the same value for both members.
Definition vector2d.hpp:29
f64 getAngleTrig() const
Calculates the angle of this vector in degrees in the trigonometric sense.
Definition vector2d.hpp:210
f64 getAngle() const
Calculates the angle of this vector in degrees in the counter trigonometric sense.
Definition vector2d.hpp:233
T X
X coordinate of vector.
Definition vector2d.hpp:392
bool equals(const vector2d< T > &other, const T tolerance=(T) ROUNDING_ERROR_f32) const
Checks if this vector equals the other one.
Definition vector2d.hpp:115
bool nearlyParallel(const vector2d< T > &other, const T factor=relativeErrorFactor< T >()) const
check if this vector is parallel to another vector
Definition vector2d.hpp:141
bool equalsRelative(const T a, const T b, const T factor=relativeErrorFactor< T >())
Definition irrMath.hpp:252
const f64 RADTODEG64
64bit constant for converting from radians to degrees
Definition irrMath.hpp:81
const T clamp(const T &value, const T &low, const T &high)
clamps a value between low and high
Definition irrMath.hpp:164
const f64 DEGTORAD64
64bit constant for converting from degrees to radians (formally known as GRAD_PI2)
Definition irrMath.hpp:78
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.hpp:243
As of Nirtcpp 1.6, position2d is a synonym for vector2d.
Definition vector3d.hpp:11
signed int s32
32 bit signed variable.
Definition irrTypes.hpp:72
unsigned int u32
32 bit unsigned variable.
Definition irrTypes.hpp:64
double f64
64 bit floating point variable.
Definition irrTypes.hpp:114
float f32
32 bit floating point variable.
Definition irrTypes.hpp:110

Nirtcpp    @cppfx.xyz

Utxcpp    utx::print