-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVector2.hpp
224 lines (177 loc) · 3.88 KB
/
Vector2.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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#pragma once
#ifndef __MATHS_VECTOR2_HPP__
#define __MATHS_VECTOR2_HPP__
namespace Math
{
class Vector2 : public DirectX::XMFLOAT2
{
public:
//--------------------------------------------------------------------------
// Constructors
//
Vector2() : DirectX::XMFLOAT2()
{
}
Vector2(float x, float y) : DirectX::XMFLOAT2(x, y)
{
}
explicit Vector2(const float* v) : DirectX::XMFLOAT2(v)
{
}
explicit Vector2(DirectX::FXMVECTOR v)
{
DirectX::XMStoreFloat2(this, v);
}
Vector2(const Vector2& v) : DirectX::XMFLOAT2(v)
{
}
Vector2(int ix, int iy) : DirectX::XMFLOAT2(static_cast<float>(ix), static_cast<float>(iy))
{
}
//--------------------------------------------------------------------------
// Assignment
//
Vector2& operator = (DirectX::FXMVECTOR v)
{
DirectX::XMStoreFloat2(this, v);
return *this;
}
Vector2& operator = (const Vector2& v)
{
x = v.x;
y = v.y;
return *this;
}
//--------------------------------------------------------------------------
// Conversion
//
operator DirectX::XMVECTOR () const
{
return DirectX::XMLoadFloat2(this);
}
//--------------------------------------------------------------------------
// Comparison
//
bool operator == (const Vector2& v) const
{
return x == v.x && y == v.y;
}
bool operator != (const Vector2& v) const
{
return x != v.x || y != v.y;
}
bool operator < (const Vector2& v) const
{
return x < v.x && y < v.y;
}
bool operator <= (const Vector2& v) const
{
return x <= v.x && y <= v.y;
}
bool operator > (const Vector2& v) const
{
return x > v.x && y > v.y;
}
bool operator >= (const Vector2& v) const
{
return x >= v.x && y >= v.y;
}
//--------------------------------------------------------------------------
// Computation
//
float length() const
{
return sqrtf(x * x + y * y);
}
float length_squared() const
{
return x * x + y * y;
}
void normalise();
Vector2 normalise() const;
//--------------------------------------------------------------------------
// Arithmetic Operators
//
Vector2 operator + (const Vector2& v) const
{
return Vector2(x + v.x, y + v.y);
}
Vector2 operator - (const Vector2& v) const
{
return Vector2(x - v.x, y - v.y);
}
Vector2 operator * (const Vector2& v) const
{
return Vector2(x * v.x, y * v.y);
}
Vector2 operator / (const Vector2& v) const
{
return Vector2(x / v.x, y / v.y);
}
Vector2 operator - () const
{
return Vector2(-x, -y);
}
Vector2 operator * (const float n) const
{
return Vector2(x * n, y * n);
}
friend Vector2 operator * (const float n, const Vector2& v)
{
return Vector2(v.x * n, v.y * n);
}
Vector2 operator / (const float n) const
{
return Vector2(x / n, y / n);
}
Vector2& operator += (const Vector2& v)
{
x += v.x;
y += v.y;
return *this;
}
Vector2& operator -= (const Vector2& v)
{
x -= v.x;
y -= v.y;
return *this;
}
Vector2& operator *= (const Vector2& v)
{
x *= v.x;
y *= v.y;
return *this;
}
Vector2& operator /= (const Vector2& v)
{
x /= v.x;
y /= v.y;
return *this;
}
Vector2& operator *= (const float n)
{
x *= n;
y *= n;
return *this;
}
Vector2& operator /= (const float n)
{
x /= n;
y /= n;
return *this;
}
//--------------------------------------------------------------------------
// Auxilliary Functions
//
static Vector2 lerp(const Vector2& a, const Vector2& b, float t);
static Vector2 minimise(const Vector2& a, const Vector2& b);
static Vector2 maximise(const Vector2& a, const Vector2& b);
//--------------------------------------------------------------------------
// Constants
//
static const Vector2 ZERO;
static const Vector2 UNIT_X;
static const Vector2 UNIT_Y;
};
} // namespace Math
#endif // __MATHS_VECTOR2_HPP__