-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vec2.hpp
149 lines (123 loc) · 2.56 KB
/
Vec2.hpp
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <iostream>
#include <string>
#include <cmath>
struct Vec2
{
float x, y;
Vec2()
{
x = 0.0f;
y = 0.0f;
}
Vec2(float x, float y)
{
this->x = x;
this->y = y;
}
#pragma region Operators
Vec2 operator-()
{
return Vec2(-x, -y);
}
Vec2 operator+(Vec2 otherVec)
{
return Vec2(x + otherVec.x, y + otherVec.y);
}
Vec2 operator-(Vec2 otherVec)
{
return Vec2(x - otherVec.x, y - otherVec.y);
}
Vec2 operator*(Vec2 otherVec)
{
return Vec2(x * otherVec.x, y * otherVec.y);
}
Vec2 operator/(Vec2 otherVec)
{
return Vec2(x / otherVec.x, y / otherVec.y);
}
Vec2 operator+(float otherFlo)
{
return Vec2(x + otherFlo, y + otherFlo);
}
Vec2 operator-(float otherFlo)
{
return Vec2(x - otherFlo, y - otherFlo);
}
Vec2 operator*(float otherFlo)
{
return Vec2(x * otherFlo, y * otherFlo);
}
Vec2 operator/(float otherFlo)
{
return Vec2(x / otherFlo, y / otherFlo);
}
void operator+=(Vec2 otherVec)
{
x += otherVec.x;
y += otherVec.y;
}
void operator-=(Vec2 otherVec)
{
x -= otherVec.x;
y -= otherVec.y;
}
void operator*=(Vec2 otherVec)
{
x *= otherVec.x;
y *= otherVec.y;
}
void operator/=(Vec2 otherVec)
{
x /= otherVec.x;
y /= otherVec.y;
}
void operator+=(float otherFlo)
{
x += otherFlo;
y += otherFlo;
}
void operator-=(float otherFlo)
{
x -= otherFlo;
y -= otherFlo;
}
void operator*=(float otherFlo)
{
x *= otherFlo;
y *= otherFlo;
}
void operator/=(float otherFlo)
{
x /= otherFlo;
y /= otherFlo;
}
friend std::ostream &operator<<(std::ostream &stream, Vec2 &v)
{
stream << "(X: " << v.x << ", Y: " << v.y << ")";
return stream;
}
#pragma endregion Operators
float mySqrt(float x)
{
unsigned int i = *(unsigned int *)&x;
i += 127 << 23;
i >>= 1;
return *(float *)&i;
}
float euclideanDistance()
{
return mySqrt(x * x + y * y);
}
void print(std::string name = "")
{
if (name != "")
{
std::cout << name << ": "
<< "(X: " << x << ", Y: " << y << ")" << std::endl;
}
else
{
std::cout << "(X: " << x << ", Y: " << y << ")" << std::endl;
}
}
};