TDME2  1.9.200
Vector2.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <array>
4 
5 #include <tdme/tdme.h>
6 #include <tdme/math/fwd-tdme.h>
7 #include <tdme/math/Math.h>
8 #include <tdme/utilities/Float.h>
9 
10 using std::array;
11 
12 using tdme::math::Math;
14 
15 /**
16  * Vector2 class representing vector2 mathematical structure and operations with x, y components
17  * @author Andreas Drewke
18  */
20 {
21  friend class Matrix3x3;
22 
23 private:
24  array<float, 2> data { 0.0f, 0.0f };
25 
26 public:
27  /**
28  * Public constructor
29  */
30  inline Vector2() {
31  }
32 
33  /**
34  * Public constructor
35  * @param x x component
36  * @param y y component
37  */
38  inline Vector2(float x, float y) {
39  data[0] = x;
40  data[1] = y;
41  }
42 
43  /**
44  * Public constructor
45  * @param vector2 vector2 as array
46  */
47  inline Vector2(const array<float, 2>& vector2) {
48  data = vector2;
49  }
50 
51  /**
52  * Public constructor
53  * @param vector2 vector2
54  */
55  inline Vector2(const Vector2& vector2) {
56  data = vector2.data;
57  }
58 
59  /**
60  * Sets this vector2 by its components
61  * @param x x component
62  * @param y y component
63  * @return this vector2
64  */
65  inline Vector2& set(float x, float y) {
66  data[0] = x;
67  data[1] = y;
68  return *this;
69  }
70 
71  /**
72  * Sets this vector2 by array
73  * @param vector2 vector2 as array
74  * @return this vector2
75  */
76  inline Vector2& set(const array<float, 2>& vector2) {
77  data = vector2;
78  return *this;
79  }
80 
81  /**
82  * Sets this vector2 by given vector2
83  * @param vector2 vector2
84  * @return this vector2
85  */
86  inline Vector2& set(const Vector2& vector2) {
87  data = vector2.data;
88  return *this;
89  }
90 
91  /**
92  * @return x component
93  */
94  inline float getX() const {
95  return data[0];
96  }
97 
98  /**
99  * Sets x component
100  * @param x x component
101  * @return this vector2
102  */
103  inline Vector2& setX(float x) {
104  data[0] = x;
105  return *this;
106  }
107 
108  /**
109  * @return y component
110  */
111  inline float getY() const {
112  return data[1];
113  }
114 
115  /**
116  * Sets y component
117  * @param y y component
118  * @return this vector2
119  */
120  inline Vector2& setY(float y) {
121  data[1] = y;
122  return *this;
123  }
124 
125  /**
126  * Adds a scalar
127  * @param scalar scalar
128  * @return this vector2
129  */
130  inline Vector2& add(float scalar) {
131  data[0] += scalar;
132  data[1] += scalar;
133  return *this;
134  }
135 
136  /**
137  * Adds a vector2
138  * @param vector2 vector2
139  * @return this vector2
140  */
141  inline Vector2& add(const Vector2& vector2) {
142  data[0] += vector2.data[0];
143  data[1] += vector2.data[1];
144  return *this;
145  }
146 
147  /**
148  * Subtracts a scalar
149  * @param scalar scalar
150  * @return this vector2
151  */
152  inline Vector2& sub(float scalar) {
153  data[0] -= scalar;
154  data[1] -= scalar;
155  return *this;
156  }
157 
158  /**
159  * Subtracts a vector2
160  * @param vector2 vector2
161  * @return this vector2
162  */
163  inline Vector2& sub(const Vector2& vector2) {
164  data[0] -= vector2.data[0];
165  data[1] -= vector2.data[1];
166  return *this;
167  }
168 
169  /**
170  * Scales by scalar
171  * @param scalar scalar
172  * @return this vector2
173  */
174  inline Vector2& scale(const float scalar) {
175  data[0] *= scalar;
176  data[1] *= scalar;
177  return *this;
178  }
179 
180  /**
181  * Scales by vector2
182  * @param scale scale vector2
183  * @return this vector2
184  */
185  inline Vector2& scale(const Vector2& scale) {
186  data[0] *= scale.data[0];
187  data[1] *= scale.data[1];
188  return *this;
189  }
190 
191  /**
192  * Compares this vector2 with given vector2
193  * @param vector2 vector2
194  * @param tolerance tolerance per vector2 component
195  * @return equality
196  */
197  inline bool equals(const Vector2& vector2, float tolerance = Math::EPSILON) const {
198  return (this == &vector2) ||
199  (
200  Math::abs(data[0] - vector2.data[0]) < tolerance &&
201  Math::abs(data[1] - vector2.data[1]) < tolerance
202  );
203  }
204 
205  /**
206  * Normalizes this vector2
207  * @return this vector2
208  */
209  inline Vector2& normalize() {
210  auto length = computeLength();
211  data[0] /= length;
212  data[1] /= length;
213  return *this;
214  }
215 
216  /**
217  * Computes the dot product of a and b
218  * @param a vector2 a
219  * @param b vector2 b
220  * @return dot product
221  */
222  inline static float computeDotProduct(const Vector2& a, const Vector2& b) {
223  return (a.data[0] * b.data[0]) + (a.data[1] * b.data[1]);
224  }
225 
226  /**
227  * @return the vectors length
228  */
229  inline float computeLength() const {
230  return Math::sqrt((data[0] * data[0]) + (data[1] * data[1]));
231  }
232 
233  /**
234  * @return the vectors length squared
235  */
236  inline float computeLengthSquared() const {
237  return (data[0] * data[0]) + (data[1] * data[1]);
238  }
239 
240  /**
241  * Interpolates between a and b by 0f<=t<=1f linearly
242  * @param a vector2 a
243  * @param b vector2 b
244  * @param t t
245  * @return interpolated vector2
246  */
247  inline static Vector2 interpolateLinear(const Vector2& a, const Vector2& b, float t) {
248  return Vector2(
249  (b.data[0] * t) + ((1.0f - t) * a.data[0]),
250  (b.data[1] * t) + ((1.0f - t) * a.data[1])
251  );
252  }
253 
254  /**
255  * @return vector2 as array
256  */
257  inline const array<float, 2>& getArray() const {
258  return data;
259  }
260 
261  /**
262  * Clones this vector2
263  * @return cloned vector2
264  */
265  inline Vector2 clone() const {
266  return Vector2(*this);
267  }
268 
269  /**
270  * Array access operator
271  * @param i index
272  * @return vector2 component
273  */
274  inline float& operator[](int i) {
275  return data[i];
276  }
277 
278  /**
279  * Const array access operator
280  * @param i index
281  * @return vector2 component
282  */
283  inline const float& operator[](int i) const {
284  return data[i];
285  }
286 
287  /**
288  * Operator + scalar
289  * @param scalar scalar
290  * @return new vector2 (this + scalar)
291  */
292  inline Vector2 operator +(const float scalar) const {
293  auto r = this->clone().add(scalar);
294  return r;
295  }
296 
297  /**
298  * Operator + vector2
299  * @param vector2 vector2
300  * @return new vector2 (this + vector2)
301  */
302  inline Vector2 operator +(const Vector2& vector2) const {
303  auto r = this->clone().add(vector2);
304  return r;
305  }
306 
307  /**
308  * Operator - scalar
309  * @param scalar scalar
310  * @return new vector2 (this - scalar)
311  */
312  inline Vector2 operator -(const float scalar) const {
313  auto r = this->clone().sub(scalar);
314  return r;
315  }
316 
317  /**
318  * Operator - vector2
319  * @param vector2 vector2
320  * @return new vector2 (this - vector2)
321  */
322  inline Vector2 operator -(const Vector2& vector2) const {
323  auto r = this->clone().sub(vector2);
324  return r;
325  }
326 
327  /**
328  * Operator * scalar
329  * @param scalar scalar
330  * @return new vector2 (this * scalar)
331  */
332  inline Vector2 operator *(const float scalar) const {
333  auto r = this->clone().scale(scalar);
334  return r;
335  }
336 
337  /**
338  * Operator * vector2
339  * @param vector2 vector2
340  * @return new vector2 (this * vector2)
341  */
342  inline Vector2 operator *(const Vector2& vector2) const {
343  auto r = this->clone().scale(vector2);
344  return r;
345  }
346 
347  /**
348  * Operator / scalar
349  * @param scalar scalar
350  * @return new vector2 (this / scalar)
351  */
352  inline Vector2 operator /(const float scalar) const {
353  auto r = this->clone().scale(1.0f / scalar);
354  return r;
355  }
356 
357  /**
358  * Operator / vector2
359  * @param vector2 vector2
360  * @return new vector2 (this / vector2)
361  */
362  inline Vector2 operator /(const Vector2& vector2) const {
363  auto vInverted = Vector2(1.0f / vector2[0], 1.0f / vector2[1]);
364  auto r = this->clone().scale(vInverted);
365  return r;
366  }
367 
368  /**
369  * Operator += scalar
370  * @param scalar scalar
371  * @return this vector2
372  */
373  inline Vector2& operator +=(const float scalar) {
374  return this->add(scalar);
375  }
376 
377  /**
378  * Operator += vector2
379  * @param vector2 vector2
380  * @return this vector2
381  */
382  inline Vector2& operator +=(const Vector2& vector2) {
383  return this->add(vector2);
384  }
385 
386  /**
387  * Operator -= scalar
388  * @param scalar scalar
389  * @return this vector2
390  */
391  inline Vector2& operator -=(const float scalar) {
392  return this->sub(scalar);
393  }
394 
395  /**
396  * Operator -= vector2
397  * @param vector2 vector2
398  * @return this vector2
399  */
400  inline Vector2& operator -=(const Vector2& vector2) {
401  return this->sub(vector2);
402  }
403 
404  /**
405  * Operator *= scalar
406  * @param scalar scalar
407  * @return this vector2
408  */
409  inline Vector2& operator *=(const float scalar) {
410  return this->scale(scalar);
411  }
412 
413  /**
414  * Operator *= vector2
415  * @param vector2 vector2
416  * @return this vector2
417  */
418  inline Vector2& operator *=(const Vector2& vector2) {
419  return this->scale(vector2);
420  }
421 
422  /**
423  * Operator /= scalar
424  * @param scalar scalar
425  * @return this vector2
426  */
427  inline Vector2& operator /=(const float scalar) {
428  auto vInverted = Vector2(1.0f / scalar, 1.0f / scalar);
429  return this->scale(vInverted);
430  }
431 
432  /**
433  * Operator /= vector2
434  * @param vector2 vector2
435  * @return this vector2
436  */
437  inline Vector2& operator /=(const Vector2& vector2) {
438  auto vInverted = Vector2(1.0f / vector2[0], 1.0f / vector2[1]);
439  return this->scale(vInverted);
440  }
441 
442  /**
443  * Equality comparison operator
444  * @param vector2 vector2
445  * @return equality
446  */
447  inline bool operator ==(const Vector2& vector2) const {
448  return this->equals(vector2);
449  }
450 
451  /**
452  * Non equality comparison operator
453  * @param vector2 vector2
454  * @return non equality
455  */
456  inline bool operator !=(const Vector2& vector2) const {
457  return this->equals(vector2) == false;
458  }
459 
460 };
Standard math functions.
Definition: Math.h:19
static float sqrt(float value)
Returns the square root of given value.
Definition: Math.h:192
static constexpr float EPSILON
Definition: Math.h:22
static auto abs(auto value)
Returns absolute value.
Definition: Math.h:63
Matrix3x3 class representing matrix3x3 mathematical structure and operations for 2d space.
Definition: Matrix3x3.h:20
Vector2 class representing vector2 mathematical structure and operations with x, y components.
Definition: Vector2.h:20
Vector2 & operator+=(const float scalar)
Operator += scalar.
Definition: Vector2.h:373
float getY() const
Definition: Vector2.h:111
Vector2(const array< float, 2 > &vector2)
Public constructor.
Definition: Vector2.h:47
const float & operator[](int i) const
Const array access operator.
Definition: Vector2.h:283
Vector2(const Vector2 &vector2)
Public constructor.
Definition: Vector2.h:55
Vector2 & sub(float scalar)
Subtracts a scalar.
Definition: Vector2.h:152
float getX() const
Definition: Vector2.h:94
float & operator[](int i)
Array access operator.
Definition: Vector2.h:274
float computeLength() const
Definition: Vector2.h:229
Vector2 & scale(const float scalar)
Scales by scalar.
Definition: Vector2.h:174
Vector2 clone() const
Clones this vector2.
Definition: Vector2.h:265
Vector2 & operator*=(const float scalar)
Operator *= scalar.
Definition: Vector2.h:409
Vector2 operator-(const float scalar) const
Operator - scalar.
Definition: Vector2.h:312
Vector2 & operator-=(const float scalar)
Operator -= scalar.
Definition: Vector2.h:391
static Vector2 interpolateLinear(const Vector2 &a, const Vector2 &b, float t)
Interpolates between a and b by 0f<=t<=1f linearly.
Definition: Vector2.h:247
Vector2()
Public constructor.
Definition: Vector2.h:30
Vector2 & normalize()
Normalizes this vector2.
Definition: Vector2.h:209
array< float, 2 > data
Definition: Vector2.h:24
Vector2 operator+(const float scalar) const
Operator + scalar.
Definition: Vector2.h:292
Vector2 operator*(const float scalar) const
Operator * scalar.
Definition: Vector2.h:332
Vector2 & setX(float x)
Sets x component.
Definition: Vector2.h:103
Vector2 & set(const Vector2 &vector2)
Sets this vector2 by given vector2.
Definition: Vector2.h:86
Vector2 & add(float scalar)
Adds a scalar.
Definition: Vector2.h:130
const array< float, 2 > & getArray() const
Definition: Vector2.h:257
Vector2 & set(const array< float, 2 > &vector2)
Sets this vector2 by array.
Definition: Vector2.h:76
bool equals(const Vector2 &vector2, float tolerance=Math::EPSILON) const
Compares this vector2 with given vector2.
Definition: Vector2.h:197
float computeLengthSquared() const
Definition: Vector2.h:236
Vector2 & scale(const Vector2 &scale)
Scales by vector2.
Definition: Vector2.h:185
Vector2 operator/(const float scalar) const
Operator / scalar.
Definition: Vector2.h:352
static float computeDotProduct(const Vector2 &a, const Vector2 &b)
Computes the dot product of a and b.
Definition: Vector2.h:222
Vector2 & setY(float y)
Sets y component.
Definition: Vector2.h:120
Vector2 & sub(const Vector2 &vector2)
Subtracts a vector2.
Definition: Vector2.h:163
bool operator!=(const Vector2 &vector2) const
Non equality comparison operator.
Definition: Vector2.h:456
bool operator==(const Vector2 &vector2) const
Equality comparison operator.
Definition: Vector2.h:447
Vector2 & operator/=(const float scalar)
Operator /= scalar.
Definition: Vector2.h:427
Vector2 & add(const Vector2 &vector2)
Adds a vector2.
Definition: Vector2.h:141
Vector2(float x, float y)
Public constructor.
Definition: Vector2.h:38
Vector2 & set(float x, float y)
Sets this vector2 by its components.
Definition: Vector2.h:65
Float class.
Definition: Float.h:27