Vector2D.hpp
Go to the documentation of this file.
1 
7 #include <etk/math/Vector3D.hpp>
8 #include <etk/types.hpp>
9 
10 #pragma once
11 
12 #include <cmath>
13 
14 namespace etk {
18  template <typename T> class Vector2D {
19  public:
20  T m_floats[2];
21  public:
22  /* ****************************************************
23  * Constructor
24  *****************************************************/
25  Vector2D() {
26  #ifdef DEBUG
27  // in debug mode we set supid value to prevent forget of the inits ...
28  m_floats[0] = (T)34673363;
29  m_floats[1] = (T)34523535;
30  #endif
31  }
37  Vector2D(T _xxx, T _yyy) {
38  m_floats[0] = _xxx;
39  m_floats[1] = _yyy;
40  }
45  Vector2D(const Vector2D<double>& _obj) {
46  m_floats[0] = (T)_obj.x();
47  m_floats[1] = (T)_obj.y();
48  }
53  Vector2D(const Vector2D<float>& _obj) {
54  m_floats[0] = (T)_obj.x();
55  m_floats[1] = (T)_obj.y();
56  }
61  Vector2D(const Vector2D<int32_t>& _obj) {
62  m_floats[0] = (T)_obj.x();
63  m_floats[1] = (T)_obj.y();
64  }
69  Vector2D(const std::string& _str);
70  #if __CPP_VERSION__ >= 2011
71  Vector2D(const std::u32string& _str);
72  #endif
73 
78  const Vector2D<T>& operator= (const Vector2D<T>& _obj ) {
79  m_floats[0] = _obj.m_floats[0];
80  m_floats[1] = _obj.m_floats[1];
81  return *this;
82  }
88  const Vector2D<T>& operator= (const T _val ) {
89  m_floats[0] = _val;
90  m_floats[1] = _val;
91  return *this;
92  }
99  bool operator== (const Vector2D<T>& _obj) const {
100  return ( (T)_obj.m_floats[0] == m_floats[0]
101  && (T)_obj.m_floats[1] == m_floats[1]);
102  }
109  bool operator!= (const Vector2D<T>& _obj) const {
110  return ( (T)_obj.m_floats[0] != m_floats[0]
111  || (T)_obj.m_floats[1] != m_floats[1]);
112  }
118  const Vector2D<T>& operator+= (const Vector2D<T>& _obj) {
119  m_floats[0] += _obj.m_floats[0];
120  m_floats[1] += _obj.m_floats[1];
121  return *this;
122  }
128  const Vector2D<T>& operator+= (const T _val) {
129  m_floats[0] += _val;
130  m_floats[1] += _val;
131  return *this;
132  }
138  Vector2D<T> operator+ (const Vector2D<T>& _obj) const {
139  Vector2D<T> tmpp(m_floats[0],m_floats[1]);
140  tmpp.m_floats[0] += _obj.m_floats[0];
141  tmpp.m_floats[1] += _obj.m_floats[1];
142  return tmpp;
143  }
149  Vector2D<T> operator+ (const T _val) const {
150  Vector2D<T> tmpp(m_floats[0],m_floats[1]);
151  tmpp.m_floats[0] += _val;
152  tmpp.m_floats[1] += _val;
153  return tmpp;
154  }
160  const Vector2D<T>& operator-= (const Vector2D<T>& _obj) {
161  m_floats[0] -= _obj.m_floats[0];
162  m_floats[1] -= _obj.m_floats[1];
163  return *this;
164  }
170  const Vector2D<T>& operator-= (const T _val) {
171  m_floats[0] -= _val;
172  m_floats[1] -= _val;
173  return *this;
174  }
180  Vector2D<T> operator- (const Vector2D<T>& _obj) const {
181  Vector2D<T> tmpp(m_floats[0],m_floats[1]);
182  tmpp.m_floats[0] -= _obj.m_floats[0];
183  tmpp.m_floats[1] -= _obj.m_floats[1];
184  return tmpp;
185  }
191  Vector2D<T> operator- (const T _val) const {
192  Vector2D<T> tmpp(m_floats[0],m_floats[1]);
193  tmpp.m_floats[0] -= _val;
194  tmpp.m_floats[1] -= _val;
195  return tmpp;
196  }
202  const Vector2D<T>& operator*= (const Vector2D<T>& _obj) {
203  m_floats[0] *= _obj.m_floats[0];
204  m_floats[1] *= _obj.m_floats[1];
205  return *this;
206  }
212  const Vector2D<T>& operator*= (const T _val) {
213  m_floats[0] *= _val;
214  m_floats[1] *= _val;
215  return *this;
216  }
222  Vector2D<T> operator* (const Vector2D<T>& _obj) const {
223  Vector2D<T> tmpp(m_floats[0],m_floats[1]);
224  tmpp.m_floats[0] *= _obj.m_floats[0];
225  tmpp.m_floats[1] *= _obj.m_floats[1];
226  return tmpp;
227  }
233  Vector2D<T> operator* (const T _val) const {
234  Vector2D<T> tmpp(m_floats[0],m_floats[1]);
235  tmpp.m_floats[0] *= _val;
236  tmpp.m_floats[1] *= _val;
237  return tmpp;
238  }
244  Vector2D<T> operator/ (const Vector2D<T>& _obj) const{
245  Vector2D<T> tmpp(m_floats[0], m_floats[1]);
246  tmpp.m_floats[0] /= _obj.m_floats[0];
247  tmpp.m_floats[1] /= _obj.m_floats[1];
248  return tmpp;
249  }
255  Vector2D<T> operator/ (const T _val) const {
256  Vector2D<T> tmpp(m_floats[0], m_floats[1]);
257  tmpp.m_floats[0] /= _val;
258  tmpp.m_floats[1] /= _val;
259  return tmpp;
260  }
266  const Vector2D<T>& operator/= (const Vector2D<T>& _obj) {
267  m_floats[0] /= _obj.m_floats[0];
268  m_floats[1] /= _obj.m_floats[1];
269  return *this;
270  }
276  const Vector2D<T>& operator/= (const T _val) {
277  m_floats[0] /= _val;
278  m_floats[1] /= _val;
279  return *this;
280  }
286  ++m_floats[0];
287  ++m_floats[1];
288  return *this;
289  }
295  Vector2D<T> result = *this;
296  ++(*this);
297  return result;
298  }
304  --m_floats[0];
305  --m_floats[1];
306  return *this;
307  }
313  Vector2D<T> result = *this;
314  --(*this);
315  return result;
316  }
322  T cross(const Vector2D<T>& _obj) const {
323  return m_floats[0] * _obj.m_floats[1]
324  - m_floats[1] * _obj.m_floats[0];
325  }
331  T dot(const Vector2D<T>& _obj) const {
332  return m_floats[0] * _obj.m_floats[0]
333  + m_floats[1] * _obj.m_floats[1];
334  }
339  T length2() const {
340  return dot(*this);
341  }
346  float length() const {
347  #if __CPP_VERSION__ >= 2011 && !defined(__STDCPP_LLVM__)
348  return std::sqrt(length2());
349  #else
350  return sqrt(length2());
351  #endif
352  }
359  T distance2(const Vector2D<T>& _obj) const {
360  return (_obj - *this).length2();
361  }
368  float distance(const Vector2D<T>& _obj) const {
369  return (_obj - *this).length();
370  }
376  *this /= length();
377  return *this;
378  }
384  T tmp = length();
385  if (tmp != 0) {
386  *this /= length();
387  return *this;
388  }
389  setValue(1,0);
390  return *this;
391  }
397  return *this / length();
398  }
404  return Vector2D<T>( abs(m_floats[0]),
405  abs(m_floats[1]));
406  }
411  int32_t minAxis() const {
412  return m_floats[0] < m_floats[1] ? 0 : 1;
413  }
418  int32_t maxAxis() const {
419  return m_floats[0] < m_floats[1] ? 1 : 0;
420  }
425  int32_t furthestAxis() const {
426  return absolute().minAxis();
427  }
432  int32_t closestAxis() const {
433  return absolute().maxAxis();
434  }
439  const T& getX() const {
440  return m_floats[0];
441  }
446  const T& getY() const {
447  return m_floats[1];
448  }
453  void setX(T _xxx) {
454  m_floats[0] = _xxx;
455  };
460  void setY(T _yyy) {
461  m_floats[1] = _yyy;
462  };
467  const T& x() const {
468  return m_floats[0];
469  }
474  const T& y() const {
475  return m_floats[1];
476  }
481  operator T *() {
482  return &m_floats[0];
483  }
488  operator const T *() const {
489  return &m_floats[0];
490  }
495  void setMax(const Vector2D<T>& _other) {
496  m_floats[0] = std::max(m_floats[0], _other.m_floats[0]);
497  m_floats[1] = std::max(m_floats[1], _other.m_floats[1]);
498  }
503  void setMin(const Vector2D<T>& _other) {
504  m_floats[0] = std::min(m_floats[0], _other.m_floats[0]);
505  m_floats[1] = std::min(m_floats[1], _other.m_floats[1]);
506  }
512  void setValue(const T& _xxx, const T& _yyy) {
513  m_floats[0] = _xxx;
514  m_floats[1] = _yyy;
515  }
519  void setZero() {
520  setValue(0,0);
521  }
527  bool isZero() const {
528  return m_floats[0] == 0
529  && m_floats[1] == 0;
530  }
535  operator std::string() const;
536  #if __CPP_VERSION__ >= 2011
537 
541  operator std::u32string() const;
542  #endif
543  };
545  std::ostream& operator <<(std::ostream& _os, const etk::Vector2D<int32_t>& _obj);
547  std::ostream& operator <<(std::ostream& _os, const etk::Vector2D<float>& _obj);
549  std::ostream& operator <<(std::ostream& _os, const etk::Vector2D<uint32_t>& _obj);
551  std::ostream& operator <<(std::ostream& _os, const etk::Vector2D<bool>& _obj);
552 };
553 // To siplify the writing of the code ==> this permit to have the same name with the glsl language...
556 // not compatible with glsl ... but it is better to have a same writing
559 
565 inline vec2 vec2ClipInt32(const vec2& _val) {
566  return vec2((int32_t)_val.x(), (int32_t)_val.y());
567 }
573 inline vec2 vec2ClipInt64(const vec2& _val) {
574  return vec2((int64_t)_val.x(), (int64_t)_val.y());
575 }
576 
584 vec2 vec2rotate(const vec2& _obj, const vec2& _point, float _angle);
585 
586 namespace etk {
588  std::ostream& operator <<(std::ostream& _os, const std::vector<vec2 >& _obj);
590  std::ostream& operator <<(std::ostream& _os, const std::vector<ivec2 >& _obj);
592  std::ostream& operator <<(std::ostream& _os, const std::vector<uivec2 >& _obj);
594  std::ostream& operator <<(std::ostream& _os, const std::vector<bvec2 >& _obj);
595 }
596 
const T & x() const
Get X value.
Definition: Vector2D.hpp:467
Vector2D< T > & normalize()
Normalize this vector x^2 + y^2 = 1.
Definition: Vector2D.hpp:375
int32_t maxAxis() const
Return the axis with the largest value.
Definition: Vector2D.hpp:418
void setMin(const Vector2D< T > &_other)
Set each element to the min of the current values and the values of another vector.
Definition: Vector2D.hpp:503
T m_floats[2]
all internal values
Definition: Vector2D.hpp:20
Vector2D(T _xxx, T _yyy)
Constructor from scalars.
Definition: Vector2D.hpp:37
const Vector2D< T > & operator/=(const Vector2D< T > &_obj)
Operator/ Dividing an other vertor with this one.
Definition: Vector2D.hpp:266
const T & getY() const
Get Y value.
Definition: Vector2D.hpp:446
float distance(const Vector2D< T > &_obj) const
Return the distance between the ends of this and another vector This is symantically treating the vec...
Definition: Vector2D.hpp:368
Vector2D< T > operator*(const Vector2D< T > &_obj) const
Operator* Multiplication an other vertor with this one.
Definition: Vector2D.hpp:222
Vector2D< T > operator-(const Vector2D< T > &_obj) const
Operator- Decrement an other vertor with this one.
Definition: Vector2D.hpp:180
Vector2D< T > & operator++()
Operator++ Pre-incrementation of this vector.
Definition: Vector2D.hpp:285
Vector2D< T > absolute() const
Return a vector will the absolute values of each element.
Definition: Vector2D.hpp:403
basic namespace of the etk library. (it might contain all the etk fuctions/class/structures without m...
Definition: Archive.hpp:16
const T & getX() const
Get X value.
Definition: Vector2D.hpp:439
T cross(const Vector2D< T > &_obj) const
Return the cross product / determinant.
Definition: Vector2D.hpp:322
Vector2D< T > normalized() const
Return a normalized version of this vector.
Definition: Vector2D.hpp:396
void setY(T _yyy)
Set the y value.
Definition: Vector2D.hpp:460
int32_t minAxis() const
Return the axis with the smallest value.
Definition: Vector2D.hpp:411
void setX(T _xxx)
Set the x value.
Definition: Vector2D.hpp:453
const Vector2D< T > & operator*=(const Vector2D< T > &_obj)
Operator*= Multiplication an other vertor with this one.
Definition: Vector2D.hpp:202
const Vector2D< T > & operator-=(const Vector2D< T > &_obj)
Operator-= Decrement an other vertor with this one.
Definition: Vector2D.hpp:160
vec2 vec2ClipInt64(const vec2 &_val)
Limit at integer value the input vector: vec3(1.2, -2.9) ==> vec3(1.0, -2.0)
Definition: Vector2D.hpp:573
vec2 vec2rotate(const vec2 &_obj, const vec2 &_point, float _angle)
Rotate the vector at a specific position with a specific angle.
const T & y() const
Get Y value.
Definition: Vector2D.hpp:474
const Vector2D< T > & operator+=(const Vector2D< T > &_obj)
Operator+= Addition an other vertor with this one.
Definition: Vector2D.hpp:118
Vector2D< T > operator/(const Vector2D< T > &_obj) const
Operator/= Dividing an other vertor with this one.
Definition: Vector2D.hpp:244
T dot(const Vector2D< T > &_obj) const
Return the dot product.
Definition: Vector2D.hpp:331
T distance2(const Vector2D< T > &_obj) const
Return the distance squared between the ends of this and another vector This is symantically treating...
Definition: Vector2D.hpp:359
bool operator!=(const Vector2D< T > &_obj) const
In-Equality compare operator with an other object.
Definition: Vector2D.hpp:109
int32_t furthestAxis() const
Return the axis with the smallest ABSOLUTE value.
Definition: Vector2D.hpp:425
Vector2D(const Vector2D< double > &_obj)
Constructor with external vector.
Definition: Vector2D.hpp:45
etk::Vector2D< float > vec2
wrapper on etk::Vector2D<float> to have the same naming has OpenGL shader
Definition: Vector2D.hpp:554
Vectorial 2-dimention vector (x/y)
Definition: Vector2D.hpp:18
T length2() const
Get the length of the vector squared.
Definition: Vector2D.hpp:339
float length() const
Get the length of the vector.
Definition: Vector2D.hpp:346
void setZero()
Set 0 value on all the vector.
Definition: Vector2D.hpp:519
int32_t closestAxis() const
Return the axis with the largest ABSOLUTE value.
Definition: Vector2D.hpp:432
Vector2D< T > & operator--()
Operator++ Pre-decrementation of this vector.
Definition: Vector2D.hpp:303
bool isZero() const
Check if the vector is equal to (0,0)
Definition: Vector2D.hpp:527
Vector2D< T > operator--(int)
Operator++ Post-decrementation of this vector.
Definition: Vector2D.hpp:312
Vector2D(const Vector2D< int32_t > &_obj)
Constructor with external vector.
Definition: Vector2D.hpp:61
bool operator==(const Vector2D< T > &_obj) const
Equality compare operator with an other object.
Definition: Vector2D.hpp:99
Vector2D(const Vector2D< float > &_obj)
Constructor with external vector.
Definition: Vector2D.hpp:53
Vector2D< T > & safeNormalize()
Normalize this vector x^2 + y^2 = 1 (check if not deviding by 0, if it is the case ==> return (1...
Definition: Vector2D.hpp:383
Vector2D< T > operator+(const Vector2D< T > &_obj) const
Operator+ Addition an other vertor with this one.
Definition: Vector2D.hpp:138
Vector2D< T > operator++(int)
Operator++ Post-incrementation of this vector.
Definition: Vector2D.hpp:294
const Vector2D< T > & operator=(const Vector2D< T > &_obj)
Operator= Asign the current object with an other object.
Definition: Vector2D.hpp:78
void setValue(const T &_xxx, const T &_yyy)
Set Value on the vector.
Definition: Vector2D.hpp:512
void setMax(const Vector2D< T > &_other)
Set each element to the max of the current values and the values of another vector.
Definition: Vector2D.hpp:495
vec2 vec2ClipInt32(const vec2 &_val)
Limit at integer value the input vector: vec3(1.2, -2.9) ==> vec3(1.0, -2.0)
Definition: Vector2D.hpp:565