vec2.hpp (3551B)
1 #pragma once 2 3 #ifndef VEC2_HPP 4 #define VEC2_HPP 5 6 #include <cmath> 7 #include <iostream> 8 9 template <class T = float> 10 struct vec2 { 11 vec2() = default; 12 vec2(const T x, const T y) 13 : x(x) 14 , y(y) {}; 15 template <class O> 16 vec2(const vec2<O>& other) 17 : x(other.x) 18 , y(other.y) {}; 19 bool operator==(const vec2& rhs) const; 20 bool operator!=(const vec2& rhs) const; 21 vec2& operator=(const vec2& rhs); 22 vec2 operator+(const vec2& rhs) const; 23 vec2& operator+=(const vec2& rhs); 24 vec2 operator-(const vec2& rhs) const; 25 vec2& operator-=(const vec2& rhs); 26 vec2 operator*(float rhs) const; 27 vec2& operator*=(float rhs); 28 vec2 operator/(float rhs) const; 29 vec2& operator/=(float rhs); 30 T GetMagnitudeSq() const; 31 float GetMagnitude() const; 32 vec2<T> GetNormalized() const; 33 vec2<T>& Normalize(); 34 T GetMagnitudeSq(const vec2<T>& v) const; 35 float GetMagnitude(const vec2<T>& v) const; 36 vec2<T> GetNormalized(const vec2<T>& v) const; 37 void Normalize(vec2<T>& v); 38 template <typename U> 39 friend std::ostream& operator<<(std::ostream& os, const vec2<U>& v); 40 41 T x; 42 T y; 43 }; 44 45 template <typename T> 46 bool vec2<T>::operator==(const vec2& rhs) const 47 { 48 return x == rhs.x && y == rhs.y; 49 } 50 51 template <typename T> 52 bool vec2<T>::operator!=(const vec2& rhs) const 53 { 54 return x != rhs.x || y != rhs.y; 55 } 56 57 template <typename T> 58 vec2<T>& vec2<T>::operator=(const vec2& rhs) 59 { 60 x = rhs.x; 61 y = rhs.y; 62 return *this; 63 } 64 65 template <typename T> 66 vec2<T> vec2<T>::operator+(const vec2& rhs) const 67 { 68 return vec2<T>(x + rhs.x, y + rhs.y); // Can I get away with no T here? 69 } 70 71 template <typename T> 72 vec2<T>& vec2<T>::operator+=(const vec2& rhs) 73 { 74 return *this = *this + rhs; 75 } 76 77 template <typename T> 78 vec2<T> vec2<T>::operator-(const vec2& rhs) const 79 { 80 return vec2<T>(x - rhs.x, y - rhs.y); 81 } 82 83 template <typename T> 84 vec2<T>& vec2<T>::operator-=(const vec2& rhs) 85 { 86 return *this = *this - rhs; 87 } 88 89 template <typename T> 90 vec2<T> vec2<T>::operator*(float rhs) const 91 { 92 return vec2<T>(x * rhs, y * rhs); 93 } 94 95 template <typename T> 96 vec2<T>& vec2<T>::operator*=(float rhs) 97 { 98 return *this = *this * rhs; 99 } 100 101 template <typename T> 102 vec2<T> vec2<T>::operator/(float rhs) const 103 { 104 return vec2<T>(x / rhs, y / rhs); 105 } 106 107 template <typename T> 108 vec2<T>& vec2<T>::operator/=(float rhs) 109 { 110 return *this = *this / rhs; 111 } 112 113 template <typename T> 114 T vec2<T>::GetMagnitudeSq() const 115 { 116 return (x * x) + (y * y); 117 } 118 119 template <typename T> 120 float vec2<T>::GetMagnitude() const 121 { 122 return std::sqrt(GetMagnitudeSq()); 123 } 124 125 template <typename T> 126 vec2<T> vec2<T>::GetNormalized() const 127 { 128 const float mag = GetMagnitude(); 129 if (mag != 0.0f) { 130 return *this / mag; 131 } 132 return *this; 133 } 134 135 template <typename T> 136 vec2<T>& vec2<T>::Normalize() 137 { 138 return *this = this->GetNormalized(); 139 } 140 141 template <typename T> 142 T vec2<T>::GetMagnitudeSq(const vec2<T>& v) const 143 { 144 return (v.x * v.x) + (v.y * v.y); 145 } 146 147 template <typename T> 148 float vec2<T>::GetMagnitude(const vec2<T>& v) const 149 { 150 return std::sqrt(GetMagnitudeSq(v)); 151 } 152 153 template <typename T> 154 vec2<T> vec2<T>::GetNormalized(const vec2<T>& v) const 155 { 156 const float mag = GetMagnitude(v); 157 if (mag != 0.0f) { 158 return v / mag; 159 } 160 return v; 161 } 162 163 template <typename T> 164 void vec2<T>::Normalize(vec2<T>& v) 165 { 166 v = GetNormalized(v); 167 } 168 169 template <typename T> 170 std::ostream& operator<<(std::ostream& os, const vec2<T>& v) 171 { 172 return os << "(" << v.x << ", " << v.y << ")"; 173 } 174 175 #endif // VEC2_HPP