philsupertramp/game-math
Loading...
Searching...
No Matches
vec2.h
Go to the documentation of this file.
1#pragma once
2
3#include <cmath>
4#include <ostream>
5
10template<class T>
11struct vec2 {
12public:
13#ifdef MATH_SILENCE_WARNING
14 #if COMPILER_GCC
15 #pragma GCC diagnostic push
16 #pragma GCC diagnostic ignored "-Wpedantic"
17 #elif COMPILER_CLANG
18 #pragma clang diagnostic push
19 #pragma clang diagnostic ignored "-Wgnu-anonymous-struct"
20 #pragma clang diagnostic ignored "-Wnested-anon-types"
21 #elif COMPILER_VC
22 #pragma warning(push)
23 #pragma warning(disable : 4201) // nonstandard extension used : nameless struct/union
24 #endif
25#endif
27 union {
28 struct {
29 T x, y;
30 };
31 };
32
33#ifdef MATH_SILENCE_WARNING
34 #if COMPILER_CLANG
35 #pragma clang diagnostic pop
36 #elif COMPILER_GCC
37 #pragma GCC diagnostic pop
38 #elif COMPILER_VC
39 #pragma warning(pop)
40 #endif
41#endif
42
44 const size_t dim = 2;
45
49 vec2() {
50 x = static_cast<T>(0.0f);
51 y = static_cast<T>(0.0f);
52 }
58 vec2(const T& _x, const T& _y) {
59 x = _x;
60 y = _y;
61 }
62
67 explicit vec2(const T& _v) {
68 x = _v;
69 y = _v;
70 }
71
76 vec2(const vec2<T>& _v) {
77 x = _v.x;
78 y = _v.y;
79 }
80
81 /* Misc functions */
86 inline float length() const { return sqrtf(x * x + y * y); }
91 inline vec2<T> normalize() const { return vec2(x / length(), y / length()); }
92
93 /* Arithmetic operators */
100 friend vec2<T> operator+(vec2<T> lhs, const vec2<T>& rhs) { return lhs += rhs; }
107 friend vec2<T> operator-(vec2<T> lhs, const vec2<T>& rhs) { return lhs -= rhs; }
114 friend vec2<T> operator*(vec2<T> lhs, const T& rhs) { return lhs *= rhs; }
121 friend vec2<T> operator*(const T& lhs, vec2<T> rhs) { return rhs *= lhs; }
122
123 friend vec2<T> operator/(const vec2<T>& lhs, vec2<T> rhs) { return rhs /= lhs; }
124
131 friend T operator*(vec2<T> lhs, const vec2<T>& rhs) { return lhs.x * rhs.x + lhs.y * rhs.y; }
132
139 friend vec2<T> operator/(vec2<T> lhs, const T& rhs) { return lhs /= rhs; }
140
141 /* compound assignment */
148 x += rhs.x;
149 y += rhs.y;
150 return *this;
151 }
158 x -= rhs.x;
159 y -= rhs.y;
160 return *this;
161 }
167 vec2<T>& operator*=(const T& rhs) {
168 x *= rhs;
169 y *= rhs;
170 return *this;
171 }
177 vec2<T>& operator/=(const T& rhs) {
178 x /= rhs;
179 y /= rhs;
180 return *this;
181 }
183 x /= rhs.x;
184 y /= rhs.y;
185 return *this;
186 }
187
188 /* comparison operators */
189
196 friend bool operator==(const vec2<T>& lhs, const vec2<T>& rhs) { return lhs.x == rhs.x && lhs.y == rhs.y; }
197 /* explicit type casts */
198
204 constexpr vec2<T>& operator=(vec2<T> const& V) {
205 this->x = static_cast<T>(V.x);
206 this->y = static_cast<T>(V.y);
207 return *this;
208 }
209
210 /* Member access */
216 T& operator[](const int& index) {
217 switch(index) {
218 default:
219 case 0: return x;
220 case 1: return y;
221 }
222 }
228 const T& operator[](const int& index) const {
229 switch(index) {
230 default:
231 case 0: return x;
232 case 1: return y;
233 }
234 }
235
241 template<typename U>
242 explicit operator vec2<U>() {
243 return vec2<U>(x, y);
244 }
245
246 // LCOV_EXCL_START
247 /* stream operators */
253 template<class U>
254 friend std::ostream& operator<<(std::ostream&, const vec2<U>&);
255 // LCOV_EXCL_STOP
256};
257
258template<class T>
259std::ostream& operator<<(std::ostream& os, const vec2<T>& obj) {
260 os.precision(17);
261 os << obj.x << ", " << obj.y << std::endl;
262 return os;
263}
264
271template<class T>
273 T* values = (T*)in;
274 return vec2<T>(values[0], values[1]);
275}
276
Definition: vec2.h:11
vec2< T > & operator+=(const vec2< T > &rhs)
Definition: vec2.h:147
friend vec2< T > operator/(const vec2< T > &lhs, vec2< T > rhs)
Definition: vec2.h:123
vec2< T > & operator/=(const T &rhs)
Definition: vec2.h:177
friend T operator*(vec2< T > lhs, const vec2< T > &rhs)
Definition: vec2.h:131
vec2< T > & operator*=(const T &rhs)
Definition: vec2.h:167
vec2< T > normalize() const
Definition: vec2.h:91
vec2()
Definition: vec2.h:49
const T & operator[](const int &index) const
Definition: vec2.h:228
vec2(const vec2< T > &_v)
Definition: vec2.h:76
friend vec2< T > operator+(vec2< T > lhs, const vec2< T > &rhs)
Definition: vec2.h:100
T y
Definition: vec2.h:29
constexpr vec2< T > & operator=(vec2< T > const &V)
Definition: vec2.h:204
friend bool operator==(const vec2< T > &lhs, const vec2< T > &rhs)
Definition: vec2.h:196
vec2< T > & operator/=(const vec2< T > &rhs)
Definition: vec2.h:182
friend vec2< T > operator*(const T &lhs, vec2< T > rhs)
Definition: vec2.h:121
float length() const
Definition: vec2.h:86
const size_t dim
object dimension
Definition: vec2.h:44
friend vec2< T > operator-(vec2< T > lhs, const vec2< T > &rhs)
Definition: vec2.h:107
friend std::ostream & operator<<(std::ostream &, const vec2< U > &)
vec2< T > & operator-=(const vec2< T > &rhs)
Definition: vec2.h:157
T x
Definition: vec2.h:29
T & operator[](const int &index)
Definition: vec2.h:216
vec2(const T &_x, const T &_y)
Definition: vec2.h:58
friend vec2< T > operator*(vec2< T > lhs, const T &rhs)
Definition: vec2.h:114
vec2(const T &_v)
Definition: vec2.h:67
friend vec2< T > operator/(vec2< T > lhs, const T &rhs)
Definition: vec2.h:139
vec2< T > build_vec2(void *in)
Definition: vec2.h:272
std::ostream & operator<<(std::ostream &os, const vec2< T > &obj)
Definition: vec2.h:259