Libftpp
A modern C++ library
ivector2.hpp
Go to the documentation of this file.
1 #ifndef IVECTOR2_HPP
2 #define IVECTOR2_HPP
3 
4 #include <cmath>
5 #include <stdexcept>
6 
7 template <typename TType>
8 struct IVector2
9 {
10  TType x;
11  TType y;
12 
13  IVector2() : x(0), y(0) {}
14  IVector2(TType x, TType y) : x(x), y(y) {}
15 
16  IVector2 operator+(const IVector2& other) const
17  {
18  return IVector2(x + other.x, y + other.y);
19  }
20 
21  IVector2 operator-(const IVector2& other) const
22  {
23  return IVector2(x - other.x, y - other.y);
24  }
25 
26  IVector2 operator*(const IVector2& other) const
27  {
28  return IVector2(x * other.x, y * other.y);
29  }
30  IVector2 operator/(const IVector2& other) const
31  {
32  if (other.x == 0 || other.y == 0)
33  throw std::invalid_argument("Division by zero: denominator has a zero component");
34 
35  return IVector2(x / other.x, y / other.y);
36  }
37 
38  bool operator==(const IVector2& other) const
39  {
40  return (x == other.x) && (y == other.y);
41  }
42 
43  bool operator!=(const IVector2& other) const
44  {
45  return !(*this == other);
46  }
47 
48  // On retourne une reference pour permettre le chainage
49  IVector2& operator+=(const IVector2& other)
50  {
51  x += other.x;
52  y += other.y;
53  return *this;
54  }
55  IVector2& operator-=(const IVector2& other)
56  {
57  x -= other.x;
58  y -= other.y;
59  return *this;
60  }
61 
62  float length() const
63  {
64  // C++11 feature, plus stable sur les overflows et underflows
65  return std::hypot(x, y);
66  }
67 
69  {
70  float length = this->length();
71  if (length == 0)
72  throw std::invalid_argument("Cannot normalized zero vector");
73  return IVector2<TType>(x / length, y / length);
74  }
75 
76  // Ceci n'est pas un vrai dot, renvoie juste le scalaire du |v| carre
77  float dot()
78  {
79  return (x * x + y * y);
80  }
81 
82  float dot(const IVector2& other)
83  {
84  return (x * other.x + y * other.y);
85  }
86 
87  // Pseudo Cross, le cross d'un vecteur 2d devrait renvoyer un scalaire du coup j'ai fais une
88  // rotation pour renvoyer un vector perpendiculaire (plus utile dans les calculs 2D)
90  {
91  return IVector2(-y, x);
92  }
93 
94  // Scalaire représentant l’aire signée du parallélogramme formé par 2 vectorA et vectorB
95  // Signe positif : vectorA est à gauche de vectorB
96  // Signe négatif : vectorA est à droite de vectorB
97  float cross(const IVector2& other) const
98  {
99  return (x * other.y - y * other.x);
100  }
101 };
102 #endif
IVector2(TType x, TType y)
Definition: ivector2.hpp:14
IVector2 & operator+=(const IVector2 &other)
Definition: ivector2.hpp:49
IVector2 & operator-=(const IVector2 &other)
Definition: ivector2.hpp:55
float cross(const IVector2 &other) const
Definition: ivector2.hpp:97
float dot(const IVector2 &other)
Definition: ivector2.hpp:82
IVector2 operator*(const IVector2 &other) const
Definition: ivector2.hpp:26
IVector2 operator/(const IVector2 &other) const
Definition: ivector2.hpp:30
IVector2 operator-(const IVector2 &other) const
Definition: ivector2.hpp:21
TType x
Definition: ivector2.hpp:10
IVector2< TType > normalize()
Definition: ivector2.hpp:68
float length() const
Definition: ivector2.hpp:62
IVector2 cross()
Definition: ivector2.hpp:89
bool operator!=(const IVector2 &other) const
Definition: ivector2.hpp:43
IVector2 operator+(const IVector2 &other) const
Definition: ivector2.hpp:16
TType y
Definition: ivector2.hpp:11
IVector2()
Definition: ivector2.hpp:13
bool operator==(const IVector2 &other) const
Definition: ivector2.hpp:38
float dot()
Definition: ivector2.hpp:77