Leonetienne/Eule
Homemade math library, mainly targetted towards computer graphics
Vector2.h
Go to the documentation of this file.
1 #pragma once
2 #include <cstdlib>
3 #include <sstream>
4 
5 namespace Eule
6 {
7  template <typename T> class Vector3;
8  template <typename T> class Vector4;
9 
13  template <typename T>
14  class Vector2
15  {
16  public:
17  Vector2() : x{ 0 }, y{ 0 } {}
18  Vector2(T _x, T _y) : x{ _x }, y{ _y } {}
19  Vector2(const Vector2<T>& other) = default;
20  Vector2(Vector2<T>&& other) noexcept = default;
21 
23  double DotProduct(const Vector2<T>& other) const;
24 
26  double CrossProduct(const Vector2<T>& other) const;
27 
29  double SqrMagnitude() const;
30 
32  double Magnitude() const;
33 
35  [[nodiscard]] Vector2<double> Normalize() const;
36 
38  void NormalizeSelf();
39 
41  Vector2<T> VectorScale(const Vector2<T>& scalar) const;
42 
44  void LerpSelf(const Vector2<T>& other, double t);
45 
47  [[nodiscard]] Vector2<double> Lerp(const Vector2<T>& other, double t) const;
48 
50  [[nodiscard]] bool Similar(const Vector2<T>& other, double epsilon = 0.00001) const;
51 
53  [[nodiscard]] Vector2<int> ToInt() const;
54 
56  [[nodiscard]] Vector2<double> ToDouble() const;
57 
58  T& operator[](std::size_t idx);
59  const T& operator[](std::size_t idx) const;
60 
61  Vector2<T> operator+(const Vector2<T>& other) const;
62  void operator+=(const Vector2<T>& other);
63  Vector2<T> operator-(const Vector2<T>& other) const;
64  void operator-=(const Vector2<T>& other);
65  Vector2<T> operator*(const T scale) const;
66  void operator*=(const T scale);
67  Vector2<T> operator/(const T scale) const;
68  void operator/=(const T scale);
69  Vector2<T> operator-() const;
70 
71  operator Vector3<T>() const;
72  operator Vector4<T>() const;
73 
74  void operator=(const Vector2<T>& other);
75  void operator=(Vector2<T>&& other) noexcept;
76 
77  bool operator==(const Vector2<T>& other) const;
78  bool operator!=(const Vector2<T>& other) const;
79 
80  friend std::ostream& operator<< (std::ostream& os, const Vector2<T>& v)
81  {
82  return os << "[x: " << v.x << " y: " << v.y << "]";
83  }
84  friend std::wostream& operator<< (std::wostream& os, const Vector2<T>& v)
85  {
86  return os << L"[x: " << v.x << L" y: " << v.y << L"]";
87  }
88 
89  T x;
90  T y;
91 
92  // Some handy predefines
93  static const Vector2<double> up;
94  static const Vector2<double> down;
95  static const Vector2<double> right;
96  static const Vector2<double> left;
97  static const Vector2<double> one;
98  static const Vector2<double> zero;
99  };
100 
103 }
Eule::Vector2::Normalize
Vector2< double > Normalize() const
Will return the normalization of this vector.
Definition: Vector2.cpp:137
Eule::Vector2::operator!=
bool operator!=(const Vector2< T > &other) const
Definition: Vector2.cpp:656
Eule::Vector2::Vector2
Vector2()
Definition: Vector2.h:17
Eule::Vector2::operator+
Vector2< T > operator+(const Vector2< T > &other) const
Definition: Vector2.cpp:346
Eule::Vector2::operator+=
void operator+=(const Vector2< T > &other)
Definition: Vector2.cpp:385
Eule::Vector2::DotProduct
double DotProduct(const Vector2< T > &other) const
Will compute the dot product to another Vector2.
Definition: Vector2.cpp:47
Eule::Vector2::left
static const Vector2< double > left
Definition: Vector2.h:96
Eule::Vector2::up
static const Vector2< double > up
Definition: Vector2.h:93
Eule::Vector2::Magnitude
double Magnitude() const
Will compute the magnitude.
Definition: Vector2.cpp:90
Eule::Vector2::operator*=
void operator*=(const T scale)
Definition: Vector2.cpp:542
Eule::Vector2::operator/
Vector2< T > operator/(const T scale) const
Definition: Vector2.cpp:582
Eule::Vector2::zero
static const Vector2< double > zero
Definition: Vector2.h:98
Eule::Vector2::VectorScale
Vector2< T > VectorScale(const Vector2< T > &scalar) const
Will scale self.n by scalar.n.
Definition: Vector2.cpp:127
Eule::Vector2::ToInt
Vector2< int > ToInt() const
Will convert this vector to a Vector2i.
Definition: Vector2.cpp:304
Eule::Vector2::Vector2
Vector2(T _x, T _y)
Definition: Vector2.h:18
Eule::Vector2::operator[]
T & operator[](std::size_t idx)
Definition: Vector2.cpp:267
Eule::Vector2::one
static const Vector2< double > one
Definition: Vector2.h:97
Eule::Vector2::operator*
Vector2< T > operator*(const T scale) const
Definition: Vector2.cpp:503
Eule::Vector2::operator/=
void operator/=(const T scale)
Definition: Vector2.cpp:620
Eule::Vector2::SqrMagnitude
double SqrMagnitude() const
Will compute the square magnitude.
Definition: Vector2.cpp:83
Eule::Vector2::Lerp
Vector2< double > Lerp(const Vector2< T > &other, double t) const
Will return a lerp result between this and another vector.
Definition: Vector2.cpp:256
Eule::Vector2::CrossProduct
double CrossProduct(const Vector2< T > &other) const
Will compute the cross product to another Vector2.
Definition: Vector2.cpp:65
Eule::Vector2::ToDouble
Vector2< double > ToDouble() const
Will convert this vector to a Vector2d.
Definition: Vector2.cpp:310
Eule::Vector2::NormalizeSelf
void NormalizeSelf()
Will normalize this vector.
Definition: Vector2.cpp:187
Eule::Vector2::operator-
Vector2< T > operator-() const
Definition: Vector2.cpp:662
Eule::Vector2::operator<<
friend std::ostream & operator<<(std::ostream &os, const Vector2< T > &v)
Definition: Vector2.h:80
Eule::Vector2
Representation of a 2d vector.
Definition: Vector2.h:14
Eule::Vector2d
Vector2< double > Vector2d
Definition: Vector2.h:102
Eule::Vector2::operator=
void operator=(const Vector2< T > &other)
Conversion method.
Definition: Vector2.cpp:630
Eule::Vector2::x
T x
Definition: Vector2.h:89
Eule::Vector2::y
T y
Definition: Vector2.h:90
Eule::Vector2::Similar
bool Similar(const Vector2< T > &other, double epsilon=0.00001) const
Will compare if two vectors are similar to a certain epsilon value.
Definition: Vector2.cpp:295
Eule::Vector2::right
static const Vector2< double > right
Definition: Vector2.h:95
Eule::Vector2::operator-=
void operator-=(const Vector2< T > &other)
Definition: Vector2.cpp:463
Eule::Vector2::down
static const Vector2< double > down
Definition: Vector2.h:94
Eule::Vector2::operator==
bool operator==(const Vector2< T > &other) const
Definition: Vector2.cpp:648
Eule
Definition: Collider.h:4
Eule::Vector2::LerpSelf
void LerpSelf(const Vector2< T > &other, double t)
Will lerp itself towards other by t.
Definition: Vector2.cpp:238
Eule::Vector4
Representation of a 4d vector.
Definition: Vector2.h:8
Eule::Vector2i
Vector2< int > Vector2i
Definition: Vector2.h:101