GameKit  0.0.1a
C++ gamedev tools
Vector2.hpp
Go to the documentation of this file.
1 /*
2  * =====================================================================================
3  *
4  * Filename: Vector2.hpp
5  *
6  * Description:
7  *
8  * Created: 12/05/2015 18:45:17
9  *
10  * Author: Quentin Bazin, <gnidmoo@gmail.com>
11  *
12  * =====================================================================================
13  */
14 #ifndef GK_VECTOR2_HPP_
15 #define GK_VECTOR2_HPP_
16 
17 #include <stdexcept>
18 #include <utility>
19 
20 #include "IntTypes.hpp"
21 
22 namespace gk {
23 
24 template<typename T>
25 class Vector2 {
26  public:
27  Vector2() = default;
28 
29  Vector2(T _x, T _y) : x(_x), y(_y) {}
30 
31  template<typename U>
32  Vector2(const Vector2<U> &vector2) : x(vector2.x), y(vector2.y) {}
33 
34  Vector2 operator+(const Vector2<T> &vector2) const { return Vector2{x + vector2.x, y + vector2.y}; }
35  Vector2 operator-(const Vector2<T> &vector2) const { return Vector2{x - vector2.x, y - vector2.y}; }
36  Vector2 operator-() const { return Vector2{-x, -y}; }
37  Vector2 operator*(T n) const { return Vector2{x * n, y * n}; }
38 
39  Vector2 operator/(T n) const {
40  if(n != 0) {
41  return Vector2{x / n, y / n};
42  } else {
43  throw std::overflow_error("Divide by zero exception");
44  }
45  }
46 
47  Vector2 &operator=(T n) { x = n; y = n; return *this; }
48  Vector2 &operator+=(const Vector2 &vector2) { *this = operator+(vector2); return *this; }
49  Vector2 &operator-=(const Vector2 &vector2) { *this = operator-(vector2); return *this; }
50  Vector2 &operator*=(T n) { *this = operator*(n); return *this; }
51  Vector2 &operator/=(T n) { *this = operator/(n); return *this; }
52 
53  bool operator==(const Vector2<T> &vector2) const { return x == vector2.x && y == vector2.y; }
54  bool operator!=(const Vector2<T> &vector2) const { return !operator==(vector2); }
55 
56  // Needed if Vector2 is used as a key in a std::map
57  bool operator<(const Vector2<T> &vector2) const { return x < vector2.x && y <= vector2.y; }
58  bool operator>(const Vector2<T> &vector2) const { return x > vector2.x && y >= vector2.y; }
59 
60  T x;
61  T y;
62 };
63 
64 template<typename T>
65 Vector2<T> operator*(T n, Vector2<T> &vector2) { return vector2.operator*(n); }
66 
71 
72 } // namespace gk
73 
74 #endif // GK_VECTOR2_HPP_
Vector2 operator+(const Vector2< T > &vector2) const
Definition: Vector2.hpp:34
Vector2()=default
Vector2 & operator=(T n)
Definition: Vector2.hpp:47
Vector2 & operator*=(T n)
Definition: Vector2.hpp:50
Vector2 operator/(T n) const
Definition: Vector2.hpp:39
Vector2(T _x, T _y)
Definition: Vector2.hpp:29
Vector2 & operator-=(const Vector2 &vector2)
Definition: Vector2.hpp:49
bool operator>(const Vector2< T > &vector2) const
Definition: Vector2.hpp:58
Vector2 operator*(T n) const
Definition: Vector2.hpp:37
Vector2 & operator/=(T n)
Definition: Vector2.hpp:51
Vector2 operator-() const
Definition: Vector2.hpp:36
Vector2(const Vector2< U > &vector2)
Definition: Vector2.hpp:32
Vector2 operator-(const Vector2< T > &vector2) const
Definition: Vector2.hpp:35
bool operator==(const Vector2< T > &vector2) const
Definition: Vector2.hpp:53
Vector2 & operator+=(const Vector2 &vector2)
Definition: Vector2.hpp:48
bool operator!=(const Vector2< T > &vector2) const
Definition: Vector2.hpp:54