CSEngine
Loading...
Searching...
No Matches
Vector.h
1#pragma once
2
3#include <cmath>
4
5namespace CSE {
6
7 const float Pi = 4 * std::atan(1.0f);
8 const float TwoPi = 2 * Pi;
9
10 template <typename T>
11 class Vector2 {
12
13 public:
14 Vector2() {}
15
16 Vector2(T x, T y) : x(x), y(y) {}
17
18 ~Vector2() {}
19
20 void Set(T x, T y) {
21 this->x = x;
22 this->y = y;
23 }
24
25 T Dot(const Vector2& v) const {
26 return x * v.x + y * v.y;
27 }
28
29 Vector2 operator+(const Vector2& v) const {
30 return Vector2(x + v.x, y + v.y);
31 }
32
33 Vector2 operator-(const Vector2& v) const {
34 return Vector2(x - v.x, y - v.y);
35 }
36
37 Vector2 operator-() const {
38 return Vector2(-x, -y);
39 }
40
41 void operator+=(const Vector2& v) {
42 *this = Vector2(x + v.x, y + v.y);
43 }
44
45 void operator-=(const Vector2& v) {
46 *this = Vector2(x - v.x, y - v.y);
47 }
48
49 Vector2 operator/(T s) const {
50 return Vector2(x / s, y / s);
51 }
52
53 Vector2 operator*(float s) const {
54 return Vector2(x * s, y * s);
55 }
56
57 void operator/=(float s) {
58 *this = Vector2(x / s, y / s);
59 }
60
61 void operator*=(float s) {
62 *this = Vector2(x * s, y * s);
63 }
64
65 void Normalize() {
66 float s = 1.0f / Length();
67 x *= s;
68 y *= s;
69 }
70
71 Vector2 Normalized() const {
72 Vector2 v = *this;
73 v.Normalize();
74 return v;
75 }
76
77 T LengthSquared() const {
78 return x * x + y * y;
79 }
80
81 T Length() const {
82 return sqrt(LengthSquared());
83 }
84
85 explicit operator Vector2<float>() const {
86 return Vector2<float>(x, y);
87 }
88
89 bool operator==(const Vector2& v) const {
90 return x == v.x && y == v.y;
91 }
92
93 Vector2 Lerp(float t, const Vector2& v) const {
94 return Vector2(x * (1 - t) + v.x * t,
95 y * (1 - t) + v.y * t);
96 }
97
98 static float Distance(const Vector2& a, const Vector2& b) {
99 return sqrtf(powf(a.x - b.x, 2) + powf(a.y - b.y, 2));
100 }
101
102 const T* Pointer() const {
103 return &x;
104 }
105
106 template <typename P>
107 P* Write(P* pData) {
108 Vector2* pVector = (Vector2*) pData;
109 *pVector++ = *this;
110 return (P*) pVector;
111 }
112
113 T x;
114 T y;
115 };
116
117 template <typename T>
118 class Vector3 {
119
120 public:
121 Vector3() : x(0.0f), y(0.0f), z(0.0f) {}
122
123 Vector3(T x, T y, T z) : x(x), y(y), z(z) {}
124
125 ~Vector3() {}
126
127 void Set(T x, T y, T z) {
128 this->x = x;
129 this->y = y;
130 this->z = z;
131 }
132
133 void Normalize() {
134 float s = 1.0f / std::sqrt(x * x + y * y + z * z);
135 x *= s;
136 y *= s;
137 z *= s;
138 }
139
140 Vector3 Normalized() const {
141 Vector3 v = *this;
142 v.Normalize();
143 return v;
144 }
145
146 Vector3 Cross(const Vector3& v) const {
147 return Vector3(y * v.z - z * v.y,
148 z * v.x - x * v.z,
149 x * v.y - y * v.x);
150 }
151
152 T Dot(const Vector3& v) const {
153 return x * v.x + y * v.y + z * v.z;
154 }
155
156 Vector3 operator+(const Vector3& v) const {
157 return Vector3(x + v.x, y + v.y, z + v.z);
158 }
159
160 void operator+=(const Vector3& v) {
161 x += v.x;
162 y += v.y;
163 z += v.z;
164 }
165
166 void operator-=(const Vector3& v) {
167 x -= v.x;
168 y -= v.y;
169 z -= v.z;
170 }
171
172 void operator/=(T s) {
173 x /= s;
174 y /= s;
175 z /= s;
176 }
177
178 Vector3 operator-(const Vector3& v) const {
179 return Vector3(x - v.x, y - v.y, z - v.z);
180 }
181
182 Vector3 operator-() const {
183 return Vector3(-x, -y, -z);
184 }
185
186 Vector3 operator*(T s) const {
187 return Vector3(x * s, y * s, z * s);
188 }
189
190 Vector3 operator/(T s) const {
191 return Vector3(x / s, y / s, z / s);
192 }
193
194 bool operator==(const Vector3& v) const {
195 return x == v.x && y == v.y && z == v.z;
196 }
197
198 Vector3 Lerp(float t, const Vector3& v) const {
199 return Vector3(x * (1 - t) + v.x * t,
200 y * (1 - t) + v.y * t,
201 z * (1 - t) + v.z * t);
202 }
203
204 static Vector3 Lerp(float t, const Vector3& a, Vector3& b) {
205 return Vector3(a.x * (1 - t) + b.x * t,
206 a.y * (1 - t) + b.y * t,
207 a.z * (1 - t) + b.z * t);
208 }
209
210 static float Distance(const Vector3& a, const Vector3& b) {
211 return sqrtf(powf(a.x - b.x, 2) + powf(a.y - b.y, 2) + powf(a.z - b.z, 2));
212 }
213
214 const T* Pointer() const {
215 return &x;
216 }
217
218 template <typename P>
219 P* Write(P* pData) {
220 Vector3<T>* pVector = (Vector3<T>*) pData;
221 *pVector++ = *this;
222 return (P*) pVector;
223 }
224
225 T x;
226 T y;
227 T z;
228 };
229
230 template <typename T>
231 class Vector4 {
232
233 public:
234 Vector4() {}
235
236 Vector4(T x, T y, T z, T w) : x(x), y(y), z(z), w(w) {}
237
238 Vector4(const Vector3<T>& v, T w) : x(v.x), y(v.y), z(v.z), w(w) {}
239
240 ~Vector4() {}
241
242 void Set(T x, T y, T z, T w) {
243 this->x = x;
244 this->y = y;
245 this->z = z;
246 this->w = w;
247
248 }
249
250 T Dot(const Vector4& v) const {
251 return x * v.x + y * v.y + z * v.z + w * v.w;
252 }
253
254 Vector4 operator-() const {
255 return Vector4(-x, -y, -z, -w);
256 }
257
258 Vector4 Lerp(float t, const Vector4& v) const {
259 return Vector4(x * (1 - t) + v.x * t,
260 y * (1 - t) + v.y * t,
261 z * (1 - t) + v.z * t,
262 w * (1 - t) + v.w * t);
263 }
264
265 Vector4 Lerp(float t, const Vector4& a, const Vector4& b) const {
266 return Vector4(a.x * (1 - t) + b.x * t,
267 a.y * (1 - t) + b.y * t,
268 a.z * (1 - t) + b.z * t,
269 a.w * (1 - t) + b.w * t);
270 }
271
272 static float Distance(const Vector4& a, const Vector4& b) {
273 return sqrtf(powf(a.x - b.x, 2) + powf(a.y - b.y, 2) + powf(a.z - b.z, 2) + powf(a.w - b.w, 2));
274 }
275
276 Vector4 operator+(const Vector4& v) const {
277 return Vector4(x + v.x, y + v.y, z + v.z, w + v.w);
278 }
279
280 Vector4 operator*(T s) const {
281 return Vector4(x * s, y * s, z * s, w * s);
282 }
283
284 const T* Pointer() const {
285 return &x;
286 }
287
288 T x;
289 T y;
290 T z;
291 T w;
292 };
293
294 typedef Vector2<bool> bvec2;
295
296 typedef Vector2<int> ivec2;
297 typedef Vector3<int> ivec3;
298 typedef Vector4<int> ivec4;
299
300 typedef Vector2<float> vec2;
301 typedef Vector3<float> vec3;
302 typedef Vector4<float> vec4;
303}