Bomberman
Vector2.hpp
Go to the documentation of this file.
1 //
2 // Created by Zoe Roux on 5/17/21.
3 //
4 
5 
6 #pragma once
7 
8 #include <iostream>
9 #include <cmath>
10 #include "Vector/Vector2.hpp"
11 
12 #define PI_NUMBER 3.14159265359
13 
14 namespace BBM
15 {
17  template<typename T>
18  class Vector2
19  {
20  public:
22  T x;
24  T y;
25 
28  : x(0), y(0)
29  {}
30 
32  Vector2(T _x, T _y)
33  : x(_x), y(_y)
34  {}
35 
37  ~Vector2() = default;
38 
39  bool operator==(const Vector2<T> &other) const
40  {
41  return this->x == other.x && this->y == other.y;
42  }
43 
44  bool operator!=(const Vector2<T> &other) const
45  {
46  return !this->operator==(other);
47  }
48 
49  template<typename T2>
51  {
52  this->x += vec.x;
53  this->y += vec.y;
54  return *this;
55  }
56 
57  template<typename T2>
58  Vector2<T> operator+(const Vector2<T2> &vec) const
59  {
60  return Vector2<T>(this->x + vec.x, this->y + vec.y);
61  }
62 
63  template<typename T2>
65  {
66  this->x -= vec.x;
67  this->y -= vec.y;
68  return *this;
69  }
70 
71  template<typename T2>
72  Vector2<T> operator-(const Vector2<T2> &vec) const
73  {
74  return Vector2<T>(this->x - vec.x, this->y - vec.y);
75  }
76 
77 
78  template<typename T2>
80  {
81  this->x *= d;
82  this->y *= d;
83  return *this;
84  }
85 
86  template<typename T2>
87  Vector2<T> operator*(T2 d) const
88  {
89  return Vector2<T>(this->x * d, this->y * d);
90  }
91 
92  template<typename T2>
94  {
95  return Vector2<T>(this->x * b.x, this->y * b.y);
96  }
97 
98  template<typename T2>
100  {
101  this->x /= b.x;
102  this->y /= b.y;
103  return this;
104  }
105 
106  template<typename T2>
108  {
109  return Vector2<T>(this->x / b.x, this->y / b.y);
110  }
111 
112  template<typename T2>
114  {
115  this->x /= b;
116  this->y /= b;
117  return this;
118  }
119 
120  template<typename T2>
121  Vector2<T> operator/(T2 b) const
122  {
123  return Vector2<T>(this->x / b, this->y / b);
124  }
125 
126  template<typename T2>
127  double distance(const Vector2<T2> &o) const
128  {
129  return std::sqrt(std::pow(this->x - o.x, 2) + std::pow(this->y - o.y, 2));
130  }
131 
132  double angle(const Vector2<T> &o) const
133  {
134  float dot = this->x * o.x + this->y * o.y;
135  float det = this->x * o.y - this->y * o.x;
136  return (std::atan2(det, dot) * (180.0f / PI_NUMBER));
137  }
138 
139  double magnitude() const
140  {
141  return std::sqrt(std::pow(this->x, 2) + std::pow(this->y, 2));
142  }
143 
145  {
146  double mag = this->magnitude();
147 
148  if (mag == 0) {
149  this->x = 0;
150  this->y = 0;
151  return *this;
152  }
153  this->x /= mag;
154  this->y /= mag;
155  return *this;
156  }
157 
159  {
160  T mag = this->magnitude();
161 
162  if (mag == 0)
163  return Vector2<T>();
164  return Vector2<T>(this->x / mag, this->y / mag);
165  }
166 
167  Vector2<T> projection(const Vector2<T> &point) const
168  {
169  return (point * this) / std::pow(this->magnitude(), 2) * this;
170  }
171 
172  operator RAY::Vector2() const requires(std::is_same_v<T, float>)
173  {
174  return RAY::Vector2(this->x, this->y);
175  }
176  };
177 
181 }
182 
183 
184 template<typename T>
185 std::ostream &operator<<(std::ostream &s, const BBM::Vector2<T> &v)
186 {
187  s << "Vector2<" << typeid(T).name() << ">("<< v.x << ", " << v.y << ")";
188  return s;
189 }
BBM::Vector2::operator==
bool operator==(const Vector2< T > &other) const
Definition: Vector2.hpp:39
BBM::Vector2::projection
Vector2< T > projection(const Vector2< T > &point) const
Definition: Vector2.hpp:167
BBM::Vector2
A Vector2 data type. (templated to allow any kind of vector2)
Definition: Vector2.hpp:18
BBM::Vector2u
Vector2< unsigned > Vector2u
Definition: Vector2.hpp:179
BBM::Vector2::operator+=
Vector2< T > & operator+=(const Vector2< T2 > &vec)
Definition: Vector2.hpp:50
BBM::Vector2::operator/
Vector2< T > operator/(const Vector2< T2 > &b) const
Definition: Vector2.hpp:107
BBM::Vector2::operator/
Vector2< T > operator/(T2 b) const
Definition: Vector2.hpp:121
BBM::Vector2::operator!=
bool operator!=(const Vector2< T > &other) const
Definition: Vector2.hpp:44
BBM::Vector2::x
T x
The x value of the vector.
Definition: Vector2.hpp:22
BBM::Vector2::~Vector2
~Vector2()=default
A default destructor.
BBM::Vector2::operator+
Vector2< T > operator+(const Vector2< T2 > &vec) const
Definition: Vector2.hpp:58
RAY::Vector2
A Two-dimensionnal Vector data type.
Definition: Vector2.hpp:15
BBM
Definition: AnimationsComponent.cpp:9
BBM::Vector2::Vector2
Vector2(T _x, T _y)
Create a new vector2 representing a specific coordinate.
Definition: Vector2.hpp:32
BBM::Vector2::operator*
Vector2< T > operator*(T2 d) const
Definition: Vector2.hpp:87
BBM::Vector2::distance
double distance(const Vector2< T2 > &o) const
Definition: Vector2.hpp:127
BBM::Vector2i
Vector2< int > Vector2i
Definition: Vector2.hpp:180
BBM::Vector2::operator-
Vector2< T > operator-(const Vector2< T2 > &vec) const
Definition: Vector2.hpp:72
BBM::Vector2::Vector2
Vector2()
Create a new nil vector2.
Definition: Vector2.hpp:27
operator<<
std::ostream & operator<<(std::ostream &s, const BBM::Vector2< T > &v)
Definition: Vector2.hpp:185
BBM::Vector2::normalize
Vector2< T > normalize()
Definition: Vector2.hpp:144
BBM::Vector2::operator*=
Vector2< T > & operator*=(T2 d)
Definition: Vector2.hpp:79
BBM::Vector2::operator-=
Vector2< T > & operator-=(const Vector2< T2 > &vec)
Definition: Vector2.hpp:64
BBM::Vector2::operator/=
Vector2< T > operator/=(T2 b)
Definition: Vector2.hpp:113
std
Definition: View.hpp:210
BBM::Vector2::y
T y
The y value of the vector.
Definition: Vector2.hpp:24
BBM::Vector2::angle
double angle(const Vector2< T > &o) const
Definition: Vector2.hpp:132
BBM::Vector2::normalized
Vector2< T > normalized() const
Definition: Vector2.hpp:158
Vector2.hpp
BBM::Vector2::operator/=
Vector2< T > operator/=(const Vector2< T2 > &b)
Definition: Vector2.hpp:99
PI_NUMBER
#define PI_NUMBER
Definition: Vector2.hpp:12
BBM::Vector2::magnitude
double magnitude() const
Definition: Vector2.hpp:139
BBM::Vector2f
Vector2< float > Vector2f
Definition: Vector2.hpp:178
BBM::Vector2::operator*
Vector2< T > operator*(const Vector2< T2 > &b) const
Definition: Vector2.hpp:93