Faro Engine 0.0.0.b519570 (main)
Loading...
Searching...
No Matches
Vector2.hpp
Go to the documentation of this file.
1#pragma once
2#include <cmath>
3#include "../Containers/String.hpp"
4
5namespace Faro
6{
7 template <class T>
8 struct Vector2
9 {
10 union
11 {
12 T f[2];
13#pragma warning(suppress: 4201)
14 struct { T x, y; };
15 };
16
17 Vector2() { Clear(); }
18
19 Vector2(T x, T y) : x(x), y(y) {}
20
21 Vector2 operator+(const Vector2<T>& v) const { return Vector2(x + v.x, y + v.y); }
22
23 Vector2 operator-(const Vector2<T>& v) const { return Vector2(x - v.x, y - v.y); }
24
25 Vector2 operator*(const T value) const { return Vector2(x * value, y * value); }
26
27 Vector2 operator/(const T value) const { return Vector2(x / value, y / value); }
28
29 Vector2 operator-() const { return Vector2(-x, -y); }
30
31 Vector2 operator *(const Vector2<T>& vector) const { return Vector2(x * vector.x, y * vector.y); }
32
33 void operator+=(const Vector2<T>& v) { x += v.x; y += v.y; }
34
35 void operator-=(const Vector2<T>& v) { x -= v.x; y -= v.y; }
36
37 void operator*=(const T value) { x *= value; y *= value; }
38
39 float Dot(const Vector2<T>& vector) const
40 {
41 return x * vector.x + y * vector.y;
42 }
43
44 float Cross(const Vector2<T>& vector) const
45 {
46 return (x * vector.y) - (y * vector.x);
47 }
48
49 static float Dot(const Vector2<T>& a, const Vector2<T>& b)
50 {
51 return a.x * b.x + a.y * b.y;
52 }
53
54 static float Cross(const Vector2<T>& a, const Vector2<T>& b)
55 {
56 return (a.x * b.y) - (a.y * b.x);
57 }
58
59 T Magnitude() const { return std::sqrt(x*x + y * y); }
60
61 T SquareMagnitude() const { return x * x + y * y; }
62
63 void Normalize() { T l = Magnitude(); x /= l; y /= l; }
64
65 bool operator==(const Vector2<T>& other) const
66 {
67 for (int i = 0; i < 2; i++) {
68 if (f[i] != other.f[i]) return false;
69 }
70 return true;
71 }
72
73 bool operator!=(const Vector2<T>& other) const
74 {
75 for (int i = 0; i < 2; i++)
76 {
77 if (f[i] != other.f[i]) return true;
78 }
79 return false;
80 }
81
82 void Clear() { x = 0; y = 0; }
83
84 Vector2<T> Lerp(const Vector2<T>& r, float fact) const { return Vector2(x + ((r.x - x) * fact), y + ((r.y - y) * fact)); }
85
87 {
88 return "(x: " + Faro::ToString<T>(x) + ", y: " + Faro::ToString<T>(y) + ")";
89 }
90
91 template<class N> Vector2<N> ToType()
92 {
93 return Vector2<N>((N)x, (N)y);
94 }
95
96 static Vector2 zero;
97 static Vector2 one;
98 };
99
100 template<class T> Vector2<T> Vector2<T>::zero = { 0, 0 };
101 template<class T> Vector2<T> Vector2<T>::one = { 1, 1 };
102
105}
Definition String.hpp:12
Definition Array.hpp:8
Vector2< int32 > Int2D
Definition Vector2.hpp:104
Vector2< float > Float2D
Definition Vector2.hpp:103
Definition Vector2.hpp:9
Vector2(T x, T y)
Definition Vector2.hpp:19
void operator-=(const Vector2< T > &v)
Definition Vector2.hpp:35
T Magnitude() const
Definition Vector2.hpp:59
void operator+=(const Vector2< T > &v)
Definition Vector2.hpp:33
Vector2 operator-() const
Definition Vector2.hpp:29
static Vector2 one
Definition Vector2.hpp:97
void Normalize()
Definition Vector2.hpp:63
void Clear()
Definition Vector2.hpp:82
bool operator==(const Vector2< T > &other) const
Definition Vector2.hpp:65
Vector2 operator-(const Vector2< T > &v) const
Definition Vector2.hpp:23
Vector2< N > ToType()
Definition Vector2.hpp:91
Vector2()
Definition Vector2.hpp:17
T x
Definition Vector2.hpp:14
Vector2 operator*(const T value) const
Definition Vector2.hpp:25
static float Dot(const Vector2< T > &a, const Vector2< T > &b)
Definition Vector2.hpp:49
bool operator!=(const Vector2< T > &other) const
Definition Vector2.hpp:73
Vector2 operator+(const Vector2< T > &v) const
Definition Vector2.hpp:21
T f[2]
Definition Vector2.hpp:12
Vector2< T > Lerp(const Vector2< T > &r, float fact) const
Definition Vector2.hpp:84
static float Cross(const Vector2< T > &a, const Vector2< T > &b)
Definition Vector2.hpp:54
T y
Definition Vector2.hpp:14
void operator*=(const T value)
Definition Vector2.hpp:37
T SquareMagnitude() const
Definition Vector2.hpp:61
float Cross(const Vector2< T > &vector) const
Definition Vector2.hpp:44
Vector2 operator/(const T value) const
Definition Vector2.hpp:27
float Dot(const Vector2< T > &vector) const
Definition Vector2.hpp:39
String ToString()
Definition Vector2.hpp:86
static Vector2 zero
Definition Vector2.hpp:96