EHS
Vec2.h
Go to the documentation of this file.
1#pragma once
2
3#include "Types.h"
4#include "UTF.h"
5#include "Str.h"
6#include "Math.h"
7#include "Log.h"
8
9namespace ehs
10{
11 template<typename T = float>
12 class Vec2
13 {
14 public:
15 T x;
16 T y;
17
18 Vec2(const T x, const T y)
19 : x(x), y(y)
20 {
21 }
22
23 template<typename C>
24 Vec2(const Vec2<C>& vec)
25 : x((T)vec.x), y((T)vec.y)
26 {
27 }
28
29 Vec2(const T scalar = 0)
30 : x(scalar), y(scalar)
31 {
32 }
33
34 template<typename C>
36 {
37 x = (T)vec.x;
38 y = (T)vec.y;
39
40 return *this;
41 }
42
43 bool operator==(const Vec2<T>& vec) const
44 {
45 return x == vec.x && y == vec.y;
46 }
47
48 bool operator!=(const Vec2<T>& vec) const
49 {
50 return x != vec.x || y != vec.y;
51 }
52
54 {
55 x += vec.x;
56 y += vec.y;
57
58 return *this;
59 }
60
61 Vec2<T> operator+(const Vec2<T>& vec) const
62 {
63 Vec2<T> tmp;
64
65 tmp.x = x + vec.x;
66 tmp.y = y + vec.y;
67
68 return tmp;
69 }
70
71 Vec2<T>& operator+=(const T scalar)
72 {
73 x += scalar;
74 y += scalar;
75
76 return *this;
77 }
78
79 Vec2<T> operator+(const T scalar) const
80 {
81 Vec2<T> tmp;
82
83 tmp.x = x + scalar;
84 tmp.y = y + scalar;
85
86 return tmp;
87 }
88
90 {
91 x -= vec.x;
92 y -= vec.y;
93
94 return *this;
95 }
96
97 Vec2<T> operator-(const Vec2<T>& vec) const
98 {
99 Vec2<T> tmp;
100
101 tmp.x = x - vec.x;
102 tmp.y = y - vec.y;
103
104 return tmp;
105 }
106
107 Vec2<T>& operator-=(const T scalar)
108 {
109 x -= scalar;
110 y -= scalar;
111
112 return *this;
113 }
114
115 Vec2<T> operator-(const T scalar) const
116 {
117 Vec2<T> tmp;
118
119 tmp.x = x - scalar;
120 tmp.y = y - scalar;
121
122 return tmp;
123 }
124
126 {
127 x /= vec.x;
128 y /= vec.y;
129
130 return *this;
131 }
132
133 Vec2<T> operator/(const Vec2<T>& vec) const
134 {
135 Vec2<T> tmp;
136
137 tmp.x = x / vec.x;
138 tmp.y = y / vec.y;
139
140 return tmp;
141 }
142
143 Vec2<T>& operator/=(const T scalar)
144 {
145 x /= scalar;
146 y /= scalar;
147
148 return *this;
149 }
150
151 Vec2<T> operator/(const T scalar) const
152 {
153
154 Vec2<T> tmp;
155
156 tmp.x = x / scalar;
157 tmp.y = y / scalar;
158
159 return tmp;
160 }
161
163 {
164 x *= vec.x;
165 y *= vec.y;
166
167 return *this;
168 }
169
170 Vec2<T> operator*(const Vec2<T>& vec) const
171 {
172 Vec2<T> tmp;
173
174 tmp.x = x * vec.x;
175 tmp.y = y * vec.y;
176
177 return tmp;
178 }
179
180 Vec2<T>& operator*=(const T scalar)
181 {
182 x *= scalar;
183 y *= scalar;
184
185 return *this;
186 }
187
188 Vec2<T> operator*(const T scalar) const
189 {
190 Vec2<T> tmp;
191
192 tmp.x = x * scalar;
193 tmp.y = y * scalar;
194
195 return tmp;
196 }
197
198 bool operator<=(const Vec2<T>& other) const
199 {
200 return x <= other.x && y <= other.y;
201 }
202
203 bool operator<(const Vec2<T>& other) const
204 {
205 return x < other.x && y < other.y;
206 }
207
208 bool operator>=(const Vec2<T>& other) const
209 {
210 return x >= other.x && y >= other.y;
211 }
212
213 bool operator>(const Vec2<T>& other) const
214 {
215 return x > other.x && y > other.y;
216 }
217
219 {
220 return {-x, -y};
221 }
222
223 T operator[](const UInt_64 index) const
224 {
225 switch (index)
226 {
227 case 0:
228 return x;
229 case 1:
230 return y;
231 default:
232 EHS_LOG_INT(LogType::ERR, 0, "Index of, \"" + Str_8::FromNum(index) + "\" is out of range for a Vector 3.");
233 return x;
234 }
235 }
236
237 T& operator[](const UInt_64 index)
238 {
239 switch (index)
240 {
241 case 0:
242 return x;
243 case 1:
244 return y;
245 default:
246 EHS_LOG_INT(LogType::ERR, 0, "Index of, \"" + Str_8::FromNum(index) + "\" is out of range for a Vector 3.");
247 return x;
248 }
249 }
250
252 {
253 Vec2<T> tmp;
254
255 tmp.x = Math::Abs(x);
256 tmp.y = Math::Abs(y);
257
258 return tmp;
259 }
260
261 void Abs()
262 {
263 x = Math::Abs(x);
264 y = Math::Abs(y);
265 }
266
269 T GetDot(const Vec2<T>& vec) const
270 {
271 return x * vec.x + y * vec.y;
272 }
273
274 T GetAngle(const Vec2<T>& vec) const
275 {
276 return Math::ACos(GetDot(vec) / Math::Sqrt(GetMagnitude2() * vec.GetMagnitude2()));
277 }
278
279 Vec2<T> GetProjection(const Vec2<T>& length) const
280 {
281 return operator*(length.GetDot(*this) / GetMagnitude2());
282 }
283
284 Vec2<T> GetPerpendicular(const Vec2<T>& length) const
285 {
286 return length - GetProjection(length);
287 }
288
289 Vec2<T> GetReflection(const Vec2<T>& normal) const
290 {
291 return operator-(normal * (GetDot(normal) * 2));
292 }
293
294 T GetMagnitude() const
295 {
296 return Math::Sqrt(x * x + y * y);
297 }
298
300 {
301 return x * x + y * y;
302 }
303
304 T GetDistance(const Vec2<T>& vec) const
305 {
306 return Math::Sqrt(Math::Pow(vec.x - x, 2) + Math::Pow(vec.y - y, 2));
307 }
308
309 T GetDistance2(const Vec2<T>& vec) const
310 {
311 return Math::Pow(vec.x - x, 2) + Math::Pow(vec.y - y, 2);
312 }
313
315 {
316 Vec2<T> norm;
317
318 T dis = GetMagnitude();
319
320 norm.x = x / dis;
321 norm.y = y / dis;
322
323 return norm;
324 }
325
326 void Norm()
327 {
328 T dis = GetMagnitude();
329
330 x /= dis;
331 y /= dis;
332 }
333
335 {
336 Vec2<T> tmp;
337
338 tmp.x = Math::Rads(x);
339 tmp.y = Math::Rads(y);
340
341 return tmp;
342 }
343
344 void ToRads()
345 {
346 x = Math::Rads(x);
347 y = Math::Rads(y);
348 }
349
351 {
352 Vec2<T> tmp;
353
354 tmp.x = Math::Degr(x);
355 tmp.y = Math::Degr(y);
356
357 return tmp;
358 }
359
360 void ToDegr()
361 {
362 x = Math::Degr(x);
363 y = Math::Degr(y);
364 }
365
366 static Vec2 Lerp(const Vec2& start, const Vec2& finish, const T t)
367 {
368 return start + (finish - start) * t;
369 }
370 };
371
386}
#define EHS_LOG_INT(type, code, msg)
Definition: Log.h:137
static T Pow(const T base, const I exponent)
Definition: Math.h:173
static T Degr(const T from)
Definition: Math.h:99
static T Rads(const T from)
Definition: Math.h:89
static R ACos(const R xPos, const T precision=10)
Definition: Math.h:328
static T Abs(const T from)
Definition: Math.h:70
static T Sqrt(const T from)
Definition: Math.h:238
static Str< Char_8, UInt_64 > FromNum(const SInt_64 num)
Definition: Str.h:1348
Definition: Vec2.h:13
T y
Definition: Vec2.h:16
Vec2< T > GetPerpendicular(const Vec2< T > &length) const
Definition: Vec2.h:284
bool operator!=(const Vec2< T > &vec) const
Definition: Vec2.h:48
void Norm()
Definition: Vec2.h:326
Vec2(const Vec2< C > &vec)
Definition: Vec2.h:24
T x
Definition: Vec2.h:15
Vec2< T > & operator-=(const Vec2< T > &vec)
Definition: Vec2.h:89
void Abs()
Definition: Vec2.h:261
bool operator>=(const Vec2< T > &other) const
Definition: Vec2.h:208
T & operator[](const UInt_64 index)
Definition: Vec2.h:237
Vec2(const T x, const T y)
Definition: Vec2.h:18
Vec2 operator-()
Definition: Vec2.h:218
static Vec2 Lerp(const Vec2 &start, const Vec2 &finish, const T t)
Definition: Vec2.h:366
Vec2< T > & operator+=(const Vec2< T > &vec)
Definition: Vec2.h:53
Vec2< T > & operator/=(const T scalar)
Definition: Vec2.h:143
T GetMagnitude2() const
Definition: Vec2.h:299
Vec2< T > operator*(const Vec2< T > &vec) const
Definition: Vec2.h:170
Vec2< T > operator/(const Vec2< T > &vec) const
Definition: Vec2.h:133
T GetAngle(const Vec2< T > &vec) const
Definition: Vec2.h:274
T GetMagnitude() const
Definition: Vec2.h:294
bool operator<=(const Vec2< T > &other) const
Definition: Vec2.h:198
Vec2< T > & operator+=(const T scalar)
Definition: Vec2.h:71
void ToRads()
Definition: Vec2.h:344
Vec2< T > GetAbs() const
Definition: Vec2.h:251
void ToDegr()
Definition: Vec2.h:360
Vec2< T > GetReflection(const Vec2< T > &normal) const
Definition: Vec2.h:289
Vec2< T > & operator*=(const T scalar)
Definition: Vec2.h:180
T GetDot(const Vec2< T > &vec) const
Definition: Vec2.h:269
Vec2< T > & operator=(const Vec2< C > &vec)
Definition: Vec2.h:35
T GetDistance(const Vec2< T > &vec) const
Definition: Vec2.h:304
Vec2< T > GetNorm() const
Definition: Vec2.h:314
Vec2< T > operator-(const Vec2< T > &vec) const
Definition: Vec2.h:97
Vec2< T > operator+(const Vec2< T > &vec) const
Definition: Vec2.h:61
bool operator<(const Vec2< T > &other) const
Definition: Vec2.h:203
Vec2< T > & operator-=(const T scalar)
Definition: Vec2.h:107
Vec2< T > GetRads() const
Definition: Vec2.h:334
Vec2< T > & operator*=(const Vec2< T > &vec)
Definition: Vec2.h:162
Vec2< T > operator/(const T scalar) const
Definition: Vec2.h:151
bool operator==(const Vec2< T > &vec) const
Definition: Vec2.h:43
Vec2< T > GetDegr() const
Definition: Vec2.h:350
bool operator>(const Vec2< T > &other) const
Definition: Vec2.h:213
Vec2< T > & operator/=(const Vec2< T > &vec)
Definition: Vec2.h:125
Vec2< T > operator*(const T scalar) const
Definition: Vec2.h:188
T operator[](const UInt_64 index) const
Definition: Vec2.h:223
Vec2< T > operator+(const T scalar) const
Definition: Vec2.h:79
Vec2(const T scalar=0)
Definition: Vec2.h:29
Vec2< T > GetProjection(const Vec2< T > &length) const
Definition: Vec2.h:279
T GetDistance2(const Vec2< T > &vec) const
Definition: Vec2.h:309
Vec2< T > operator-(const T scalar) const
Definition: Vec2.h:115
Definition: Anchor.h:6
Vec2< Int_64 > Vec2_64
Definition: Vec2.h:374
Vec2< double > Vec2_d
Definition: Vec2.h:385
Vec2< float > Vec2_f
Definition: Vec2.h:384
Vec2< UInt_16 > Vec2_u16
Definition: Vec2.h:378
Vec2< Int_16 > Vec2_16
Definition: Vec2.h:380
Vec2< Int_8 > Vec2_8
Definition: Vec2.h:383
Vec2< SInt_8 > Vec2_s8
Definition: Vec2.h:382
Vec2< UInt_64 > Vec2_u64
Definition: Vec2.h:372
Vec2< SInt_64 > Vec2_s64
Definition: Vec2.h:373
Vec2< UInt_8 > Vec2_u8
Definition: Vec2.h:381
Vec2< SInt_16 > Vec2_s16
Definition: Vec2.h:379
Vec2< Int_32 > Vec2_32
Definition: Vec2.h:377
Vec2< SInt_32 > Vec2_s32
Definition: Vec2.h:376
Vec2< UInt_32 > Vec2_u32
Definition: Vec2.h:375