Nazara Engine  0.4
A fast, complete, cross-platform API designed for game development
Vector2.hpp
1 // Copyright (C) 2017 Jérôme Leclercq
2 // This file is part of the "Nazara Engine - Mathematics module"
3 // For conditions of distribution and use, see copyright notice in Config.hpp
4 
5 #pragma once
6 
7 #ifndef NAZARA_VECTOR2_HPP
8 #define NAZARA_VECTOR2_HPP
9 
10 #include <Nazara/Core/String.hpp>
11 #include <functional>
12 
13 namespace Nz
14 {
15  struct SerializationContext;
16 
17  template<typename T> class Vector3;
18  template<typename T> class Vector4;
19 
20  template<typename T>
21  class Vector2
22  {
23  public:
24  Vector2() = default;
25  Vector2(T X, T Y);
26  explicit Vector2(T scale);
27  Vector2(const T vec[2]);
28  template<typename U> explicit Vector2(const Vector2<U>& vec);
29  Vector2(const Vector2& vec) = default;
30  explicit Vector2(const Vector3<T>& vec);
31  explicit Vector2(const Vector4<T>& vec);
32  ~Vector2() = default;
33 
34  T AbsDotProduct(const Vector2& vec) const;
35  T AngleBetween(const Vector2& vec) const;
36 
37  T Distance(const Vector2& vec) const;
38  float Distancef(const Vector2& vec) const;
39  T DotProduct(const Vector2& vec) const;
40 
41  T GetLength() const;
42  float GetLengthf() const;
43  Vector2 GetNormal(T* length = nullptr) const;
44  T GetSquaredLength() const;
45 
46  Vector2& MakeUnit();
47  Vector2& MakeUnitX();
48  Vector2& MakeUnitY();
49  Vector2& MakeZero();
50 
51  Vector2& Maximize(const Vector2& vec);
52  Vector2& Minimize(const Vector2& vec);
53 
54  Vector2& Normalize(T* length = nullptr);
55 
56  Vector2& Set(T X, T Y);
57  Vector2& Set(T scale);
58  Vector2& Set(const T vec[2]);
59  Vector2& Set(const Vector2& vec);
60  Vector2& Set(const Vector3<T>& vec);
61  Vector2& Set(const Vector4<T>& vec);
62  template<typename U> Vector2& Set(const Vector2<U>& vec);
63 
64  T SquaredDistance(const Vector2& vec) const;
65 
66  String ToString() const;
67 
68  operator T* ();
69  operator const T* () const;
70 
71  const Vector2& operator+() const;
72  Vector2 operator-() const;
73 
74  Vector2 operator+(const Vector2& vec) const;
75  Vector2 operator-(const Vector2& vec) const;
76  Vector2 operator*(const Vector2& vec) const;
77  Vector2 operator*(T scale) const;
78  Vector2 operator/(const Vector2& vec) const;
79  Vector2 operator/(T scale) const;
80  Vector2& operator=(const Vector2& other) = default;
81 
82  Vector2& operator+=(const Vector2& vec);
83  Vector2& operator-=(const Vector2& vec);
84  Vector2& operator*=(const Vector2& vec);
85  Vector2& operator*=(T scale);
86  Vector2& operator/=(const Vector2& vec);
87  Vector2& operator/=(T scale);
88 
89  bool operator==(const Vector2& vec) const;
90  bool operator!=(const Vector2& vec) const;
91  bool operator<(const Vector2& vec) const;
92  bool operator<=(const Vector2& vec) const;
93  bool operator>(const Vector2& vec) const;
94  bool operator>=(const Vector2& vec) const;
95 
96  static T DotProduct(const Vector2& vec1, const Vector2& vec2);
97  static Vector2 Lerp(const Vector2& from, const Vector2& to, T interpolation);
98  static Vector2 Normalize(const Vector2& vec);
99  static Vector2 Unit();
100  static Vector2 UnitX();
101  static Vector2 UnitY();
102  static Vector2 Zero();
103 
104  T x, y;
105  };
106 
107  typedef Vector2<double> Vector2d;
108  typedef Vector2<float> Vector2f;
109  typedef Vector2<int> Vector2i;
110  typedef Vector2<unsigned int> Vector2ui;
111  typedef Vector2<Int32> Vector2i32;
112  typedef Vector2<UInt32> Vector2ui32;
113 
114  template<typename T> bool Serialize(SerializationContext& context, const Vector2<T>& vector);
115  template<typename T> bool Unserialize(SerializationContext& context, Vector2<T>* vector);
116 }
117 
118 template<typename T> std::ostream& operator<<(std::ostream& out, const Nz::Vector2<T>& vec);
119 
120 template<typename T> Nz::Vector2<T> operator*(T scale, const Nz::Vector2<T>& vec);
121 template<typename T> Nz::Vector2<T> operator/(T scale, const Nz::Vector2<T>& vec);
122 
123 namespace std
124 {
125  template<class T> struct hash<Nz::Vector2<T>>;
126 }
127 
128 #include <Nazara/Math/Vector2.inl>
129 
130 #endif // NAZARA_VECTOR2_HPP
bool operator<(const Vector2 &vec) const
Compares the vector to other one.
Definition: Vector2.inl:800
bool operator<=(const Vector2 &vec) const
Compares the vector to other one.
Definition: Vector2.inl:816
Vector2 & Minimize(const Vector2 &vec)
Sets this vector&#39;s components to the minimum of its own and other components.
Definition: Vector2.inl:312
T GetLength() const
Calculates the length (magnitude) of the vector.
Definition: Vector2.inl:180
Vector2 & MakeUnit()
Makes the vector (1, 1)
Definition: Vector2.inl:237
TODO: Inherit SoundEmitter from Node.
Definition: Algorithm.hpp:12
bool operator==(const Vector2 &vec) const
Compares the vector to other one.
Definition: Vector2.inl:773
Vector2 & MakeUnitX()
Makes the vector (1, 0)
Definition: Vector2.inl:250
Vector2 & operator+=(const Vector2 &vec)
Adds the components of other vector to this vector.
Definition: Vector2.inl:651
bool Unserialize(SerializationContext &context, bool *value)
Unserializes a boolean.
Definition: Algorithm.inl:279
String ToString() const
Gives a string representation.
Definition: Vector2.inl:484
float Distancef(const Vector2 &vec) const
Calculates the distance between two vectors.
Definition: Vector2.inl:152
Vector2 & operator*=(const Vector2 &vec)
Multiplies the components of other vector to this vector.
Definition: Vector2.inl:683
STL namespace.
T SquaredDistance(const Vector2 &vec) const
Calculates the squared distance between two vectors.
Definition: Vector2.inl:473
static Vector2 UnitY()
Shorthand for the vector (0, 1)
Definition: Vector2.inl:945
Vector2 operator/(const Vector2 &vec) const
Divides the components of the vector with other vector.
Definition: Vector2.inl:602
Vector2 & operator/=(const Vector2 &vec)
Multiplies the components of other vector to this vector.
Definition: Vector2.inl:718
static Vector2 Lerp(const Vector2 &from, const Vector2 &to, T interpolation)
Interpolates the vector to other one with a factor of interpolation.
Definition: Vector2.inl:880
Vector2 & MakeZero()
Makes the vector (0, 0)
Definition: Vector2.inl:276
Vector2 & Set(T X, T Y)
Sets the components of the vector.
Definition: Vector2.inl:360
static Vector2 Unit()
Shorthand for the vector (1, 1)
Definition: Vector2.inl:914
Vector2 operator*(const Vector2 &vec) const
Multiplies the components of the vector with other vector.
Definition: Vector2.inl:573
bool operator!=(const Vector2 &vec) const
Compares the vector to other one.
Definition: Vector2.inl:787
T AngleBetween(const Vector2 &vec) const
Calculates the angle between two vectors in orthonormal basis.
Definition: Vector2.inl:124
bool operator>(const Vector2 &vec) const
Compares the vector to other one.
Definition: Vector2.inl:832
T AbsDotProduct(const Vector2 &vec) const
Calculates the absolute dot (scalar) product with two vectors.
Definition: Vector2.inl:107
T GetSquaredLength() const
Calculates the squared length (magnitude) of the vector.
Definition: Vector2.inl:224
static Vector2 UnitX()
Shorthand for the vector (1, 0)
Definition: Vector2.inl:930
float GetLengthf() const
Calculates the length (magnitude) of the vector.
Definition: Vector2.inl:191
T DotProduct(const Vector2 &vec) const
Calculates the dot (scalar) product with two vectors.
Definition: Vector2.inl:167
Vector2 & MakeUnitY()
Makes the vector (0, 1)
Definition: Vector2.inl:263
Vector2 GetNormal(T *length=nullptr) const
Gets a copy normalized of the vector.
Definition: Vector2.inl:208
Vector2 & operator-=(const Vector2 &vec)
Substracts the components of other vector to this vector.
Definition: Vector2.inl:667
static Vector2 Zero()
Shorthand for the vector (0, 0)
Definition: Vector2.inl:960
bool Serialize(SerializationContext &context, bool value)
Serializes a boolean.
Definition: Algorithm.inl:214
T Distance(const Vector2 &vec) const
Calculates the distance between two vectors.
Definition: Vector2.inl:139
Vector2 & Maximize(const Vector2 &vec)
Sets this vector&#39;s components to the maximum of its own and other components.
Definition: Vector2.inl:291
const Vector2 & operator+() const
Helps to represent the sign of the vector.
Definition: Vector2.inl:523
Vector2 & Normalize(T *length=nullptr)
Normalizes the current vector.
Definition: Vector2.inl:335
Vector2 operator-() const
Negates the components of the vector.
Definition: Vector2.inl:534
bool operator>=(const Vector2 &vec) const
Compares the vector to other one.
Definition: Vector2.inl:845
Math class that represents an element of the two dimensional vector space.
Definition: Matrix4.hpp:21