-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVec2.h
102 lines (86 loc) · 2.01 KB
/
Vec2.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#pragma once
template <typename T>
class Vec2
{
public:
/*Special Member Functions | Vec2 */
Vec2() = default;
// constexpr to allow creating Vec2 in compile time. Not in run time as by default
constexpr Vec2(T x, T y) // Because of TEMPLATE we need to implement it in header. We cant use .cpp file
:
x(x),
y(y)
{}
/* Getters | Vec2 */
constexpr T GetX() const { return x; }
constexpr T GetY() const { return y; }
/* Setters | Vec2 */
constexpr T SetX(T x) const { this->x = x; }
constexpr T SetY(T y) const { this->y = y; }
public:
/* Operators Overload | Vec2 */
constexpr bool operator== (const Vec2& rhs)
{
return (x == rhs.x && y == rhs.y);
}
constexpr bool operator!= (const Vec2& rhs)
{
return !(*this == rhs);
}
/*
There is more optimized way to implement this. But its harder to read
Why faster? Because we are not using already implemented features.
We doing it through basics of language
Vec2 operator+ (const Vec2& rhs)
{
return { x + rhs.x, y + rhs.y };
}
Vec2& operator+= (const Vec2& rhs)
{
x += rhs.x;
y += rhs.y;
return *this; // We use return here to allow us to A += B += C
}
*/
constexpr Vec2 operator+ (const Vec2& rhs) const
{
return { x + rhs.x, y + rhs.y };
}
constexpr Vec2& operator+= (const Vec2& rhs)
{
// We use return here to allow us to A += B += C
return *this = *this + rhs;
}
constexpr Vec2 operator+ (const int rhs) const
{
return { x + rhs, y + rhs };
}
constexpr Vec2 operator- (const Vec2& rhs) const
{
return { x - rhs.x, y - rhs.y };
}
constexpr Vec2& operator-= (const Vec2& rhs)
{
return *this = *this - rhs;
}
constexpr Vec2 operator- (const int rhs) const
{
return { x - rhs, y - rhs };
}
constexpr Vec2 operator* (const Vec2& rhs) const
{
return { x * rhs.x, y * rhs.y };
}
constexpr Vec2& operator*= (const Vec2& rhs)
{
return *this = *this * rhs;
}
constexpr Vec2 operator* (const int rhs) const
{
return { x * rhs, y * rhs };
}
private:
/* Private Fields | Vec2 */
T x;
T y;
};