R-Type
Vector.hpp
Go to the documentation of this file.
1 /*
2 ** EPITECH PROJECT, 2023
3 ** R-Type
4 ** File description:
5 ** Vector
6 */
7 
8 #ifndef VECTOR_HPP_
9 #define VECTOR_HPP_
10 #include <cmath>
11 #define VECTOR_HPP_
12 #include <cmath>
13 
14 namespace GameEngine
15 {
18  template <typename T>
19  class Vector2
20  {
21  public:
23  Vector2() = default;
27  Vector2(T x, T y) : x(x), y(y){};
29  ~Vector2() = default;
32  float length() const { return (sqrt(pow(x, 2) + pow(y, 2))); };
35  Vector2 normalize() const { return Vector2(x / length(), y / length()); };
39  float dotProduct(Vector2 vec) const { return (x * vec.x + y * vec.y); };
43  bool operator==(const Vector2 &other) const { return (x == other.x && y == other.y); };
47  bool operator!=(const Vector2 &other) const { return !(*this == other); };
51  Vector2 operator+(const Vector2 &vec) const { return (Vector2(x + vec.x, y + vec.y)); };
55  Vector2 &operator+=(const Vector2 &vec)
56  {
57  x += vec.x;
58  y += vec.y;
59  return *this;
60  };
64  Vector2 operator-(const Vector2 &vec) const { return (Vector2(x + (-vec.x), y + (-vec.y))); };
68  Vector2 &operator-=(const Vector2 &vec)
69  {
70  *this += vec * -1;
71  return *this;
72  };
76  Vector2<T> operator*(const T &val) const { return Vector2(x * val, y * val); };
80  Vector2<T> &operator*=(const T &val)
81  {
82  x *= val;
83  y *= val;
84  return *this;
85  };
89  Vector2<T> operator/(const T &val) const { return Vector2(x / val, y / val); };
93  Vector2<T> &operator/=(const T &val)
94  {
95  x /= val;
96  y /= val;
97  return *this;
98  };
99 
100  T x;
101  T y;
102  };
103 } // namespace GameEngine
104 
105 #endif /* !VECTOR_HPP_ */
a Vector2 class
Definition: Vector.hpp:20
bool operator==(const Vector2 &other) const
overload operator == to compare two Vector2
Definition: Vector.hpp:43
Vector2 & operator-=(const Vector2 &vec)
overload of the -= operator
Definition: Vector.hpp:68
float dotProduct(Vector2 vec) const
calculate the dot product of the vector
Definition: Vector.hpp:39
Vector2< T > operator/(const T &val) const
overload of the / operator
Definition: Vector.hpp:89
Vector2 & operator+=(const Vector2 &vec)
overload of the += operator
Definition: Vector.hpp:55
~Vector2()=default
destructor
float length() const
calculate the length of the vector
Definition: Vector.hpp:32
Vector2(T x, T y)
constructor
Definition: Vector.hpp:27
Vector2 operator+(const Vector2 &vec) const
overload of the + operator
Definition: Vector.hpp:51
Vector2 normalize() const
normalize the vector
Definition: Vector.hpp:35
Vector2< T > & operator*=(const T &val)
overload of the *= operator
Definition: Vector.hpp:80
T y
Definition: Vector.hpp:101
T x
Definition: Vector.hpp:98
Vector2< T > & operator/=(const T &val)
overload of the /= operator
Definition: Vector.hpp:93
Vector2 operator-(const Vector2 &vec) const
overload of the - operator
Definition: Vector.hpp:64
Vector2()=default
default constructor
bool operator!=(const Vector2 &other) const
overload operator != to compare two Vector2
Definition: Vector.hpp:47
Vector2< T > operator*(const T &val) const
overload of the * operator
Definition: Vector.hpp:76
@ T
Definition: Keyboard.hpp:49
Definition: AssetManager.hpp:15