-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathatyp_Vector2.h
178 lines (141 loc) · 2.88 KB
/
atyp_Vector2.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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#pragma once
#include <cmath>
class Vector2{
public:
enum class position{
x,
y
};
union{
struct{
float x, y;
};
float data[2];
};
static Vector2 up() {
return Vector2(0.0f, 1.0f);
}
static Vector2 right() {
return Vector2(1.0f, 0.0f);
}
static Vector2 one() {
return Vector2(1.0f);
}
static Vector2 zero() {
return Vector2(0.0f);
}
static float dot(const Vector2& a, const Vector2& b) {
return (a.x * b.x + a.y * b.y);
}
static Vector2 clamp(const Vector2& value, const Vector2& min, const Vector2& max){
Vector2 result;
result.x = (value.x < min.x ? min.x : (value.x > max.x ? max.x : value.x));
result.y = (value.y < min.y ? min.y : (value.y > max.y ? max.y : value.y));
return result;
}
static Vector2 Abs(const Vector2& value){
return Vector2(abs(value.x), abs(value.y));
}
Vector2() {
data[0] = data[1] = 0;
}
Vector2(float v) {
data[0] = data[1] = v;
}
Vector2(float a_x, float a_y) {
x = a_x;
y = a_y;
}
#ifdef _CSTDIO_
void Print() const {
printf("%.2f, %.2f\n", x, y);
}
#endif // _CSTDIO_
Vector2 copy() const {
return Vector2(x, y);
}
Vector2 inverse() const {
return Vector2(-x, -y);
}
float magnitude() const {
return sqrtf(x * x + y * y);
}
float magnitudeSqr() const {
return x * x + y * y;
}
Vector2& normalise() {
float mag = magnitude();
if(mag == 0.0f)return *this;
x /= mag;
y /= mag;
return *this;
}
Vector2 normalised() const {
float mag = magnitude();
if(mag == 0.0f)return *this;
return Vector2(x / mag, y / mag);
}
float dot(const Vector2& rhs) const {
return x * rhs.x + y * rhs.y;
}
Vector2 operator+(const Vector2& rhs) const {
return Vector2(x + rhs.x, y + rhs.y);
}
Vector2 operator-(const Vector2& rhs) const {
return Vector2(x - rhs.x, y - rhs.y);
}
Vector2 operator*(float rhs) const {
return Vector2(x * rhs, y * rhs);
}
Vector2 operator/(float rhs) const {
return Vector2(x / rhs, y / rhs);
}
Vector2& operator+=(const Vector2& rhs) {
x += rhs.x;
y += rhs.y;
return *this;
}
Vector2& operator-=(const Vector2& rhs) {
x -= rhs.x;
y -= rhs.y;
return *this;
}
Vector2& operator*=(float rhs) {
x *= rhs;
y *= rhs;
return *this;
}
Vector2& operator*=(const Vector2& rhs) {
x *= rhs.x;
y *= rhs.y;
return *this;
}
Vector2& operator/=(float rhs) {
x /= rhs;
y /= rhs;
return *this;
}
Vector2& operator/=(const Vector2& rhs) {
x /= rhs.x;
y /= rhs.y;
return *this;
}
bool operator==(const Vector2& rhs) const {
return x == rhs.x && y == rhs.y;
}
bool operator!=(const Vector2& rhs) const {
return !(*this == rhs);
}
Vector2 operator-() const {
return Vector2(-x, -y);
}
float& operator[](int index) const {
return *((float*)this + index);
}
operator float* () const {
return (float*)this;
}
};
inline Vector2 operator*(float lhs, Vector2 rhs) {
return Vector2(rhs.x * lhs, rhs.y * lhs);
}