-
Notifications
You must be signed in to change notification settings - Fork 17
/
Vector.hpp
412 lines (340 loc) · 8.26 KB
/
Vector.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#pragma once
#include <algorithm>
#include <cmath>
namespace SDK
{
namespace utils
{
namespace maths
{
class vector2
{
public:
vector2( )
{
x = y = 0.f;
}
vector2( float fx, float fy )
{
x = fx;
y = fy;
}
float x, y;
vector2 operator+( const vector2& input ) const
{
return vector2{ x + input.x, y + input.y };
}
vector2 operator-( const vector2& input ) const
{
return vector2{ x - input.x, y - input.y };
}
vector2 operator/( float input ) const
{
return vector2{ x / input, y / input };
}
vector2 operator*( float input ) const
{
return vector2{ x * input, y * input };
}
vector2& operator-=( const vector2& v )
{
x -= v.x;
y -= v.y;
return *this;
}
vector2& operator/=( float input )
{
x /= input;
y /= input;
return *this;
}
vector2& operator*=( float input )
{
x *= input;
y *= input;
return *this;
}
float length( ) const
{
return std::sqrt( ( x * x ) + ( y * y ) );
}
vector2 normalized( ) const
{
return { x / length( ), y / length( ) };
}
vector2 normalize( ) const
{
return { x * length( ), y * length( ) };
}
float dot_product( vector2 input ) const
{
return ( x * input.x ) + ( y * input.y );
}
float distance( vector2 input ) const
{
return ( *this - input ).length( );
}
bool empty( ) const
{
return x == 0.f && y == 0.f;
}
};
class vector3
{
public:
vector3( )
{
x = y = z = 0.f;
}
vector3( float fx, float fy, float fz )
{
x = fx;
y = fy;
z = fz;
}
float x, y, z;
vector3 operator+( const vector3& input ) const
{
return vector3{ x + input.x, y + input.y, z + input.z };
}
vector3 operator+=(vector3 input)
{
return { x += input.x, y += input.y, z += input.z };
}
vector3 operator-( const vector3& input ) const
{
return vector3{ x - input.x, y - input.y, z - input.z };
}
vector3 operator/( float input ) const
{
return vector3{ x / input, y / input, z / input };
}
vector3 operator*( float input ) const
{
return vector3{ x * input, y * input, z * input };
}
vector3& operator-=( const vector3& v )
{
x -= v.x;
y -= v.y;
z -= v.z;
return *this;
}
vector3& operator/=( float input )
{
x /= input;
y /= input;
z /= input;
return *this;
}
vector3& operator*=( float input )
{
x *= input;
y *= input;
z *= input;
return *this;
}
bool operator==( const vector3& input ) const
{
return x == input.x && y == input.y && z == input.z;
}
void make_absolute( )
{
x = std::abs( x );
y = std::abs( y );
z = std::abs( z );
}
float length_sqr( ) const
{
return ( x * x ) + ( y * y ) + ( z * z );
}
float length( ) const
{
return std::sqrt( length_sqr( ) );
}
float length_2d( ) const
{
return std::sqrt( ( x * x ) + ( y * y ) );
}
vector3 normalized( ) const
{
return { x / length( ), y / length( ), z / length( ) };
}
void normalize_angle()
{
while (x > 89.0f)
x -= 180.f;
while (x < -89.0f)
x += 180.f;
//while (y > 180.f)
// y -= 360.f;
//while (y < -180.f)
// y += 360.f;
}
float dot_product( vector3 input ) const
{
return ( x * input.x ) + ( y * input.y ) + ( z * input.z );
}
float distance( vector3 input ) const
{
return ( *this - input ).length( );
}
float distance_2d( vector3 input ) const
{
return ( *this - input ).length_2d( );
}
void clamp( )
{
//std::clamp( x, -89.f, 89.f );
//std::clamp( y, -180.f, 180.f );
z = 0.f;
}
bool empty( ) const
{
return x == 0.f && y == 0.f && z == 0.f;
}
};
class vector4
{
public:
float x;
float y;
float z;
float w;
vector4( );
vector4( float x, float y, float z, float w );
vector4 operator+( const vector4& vector ) const;
vector4 operator-( const vector4& vector ) const;
vector4 operator-( ) const;
vector4 operator*( float number ) const;
vector4 operator/( float number ) const;
vector4& operator+=( const vector4& vector );
vector4& operator-=( const vector4& vector );
vector4& operator*=( float number );
vector4& operator/=( float number );
bool operator==( const vector4& vector ) const;
bool operator!=( const vector4& vector ) const;
inline float Dot( const vector4& vector )
{
return x * vector.x + y * vector.y + z * vector.z + w * vector.w;
}
inline float Distance( const vector4& vector )
{
return sqrt(
( x - vector.x ) * ( x - vector.x ) +
( y - vector.y ) * ( y - vector.y ) +
( z - vector.z ) * ( z - vector.z ) +
( w - vector.w ) * ( w - vector.w ) );
}
};
inline bool vector4::operator==( const vector4& vector ) const
{
return x == vector.x && y == vector.y && z == vector.z && w == vector.w;
}
inline bool vector4::operator!=( const vector4& vector ) const
{
return x != vector.x || y != vector.y || z != vector.z || w != vector.w;
}
inline vector4 vector4::operator+( const vector4& vector ) const
{
return vector4( x + vector.x, y + vector.y, z + vector.z, w + vector.w );
}
inline vector4 vector4::operator-( const vector4& vector ) const
{
return vector4( x - vector.x, y - vector.y, z - vector.z, w - vector.w );
}
inline vector4 vector4::operator-( ) const
{
return vector4( -x, -y, -z, -w );
}
inline vector4 vector4::operator*( float number ) const
{
return vector4( x * number, y * number, z * number, w * number );
}
inline vector4 vector4::operator/( float number ) const
{
return vector4( x / number, y / number, z / number, w / number );
}
class matrix
{
public:
inline float* operator[]( int i )
{
return m [ i ];
}
inline const float* operator[]( int i ) const
{
return m [ i ];
}
inline float* Base( )
{
return &m [ 0 ][ 0 ];
}
inline const float* Base( ) const
{
return &m [ 0 ][ 0 ];
}
public:
inline matrix( )
{
}
inline matrix(
float m00, float m01, float m02, float m03,
float m10, float m11, float m12, float m13,
float m20, float m21, float m22, float m23,
float m30, float m31, float m32, float m33 )
{
Init(
m00, m01, m02, m03,
m10, m11, m12, m13,
m20, m21, m22, m23,
m30, m31, m32, m33
);
}
inline void Init(
float m00, float m01, float m02, float m03,
float m10, float m11, float m12, float m13,
float m20, float m21, float m22, float m23,
float m30, float m31, float m32, float m33
)
{
m [ 0 ][ 0 ] = m00;
m [ 0 ][ 1 ] = m01;
m [ 0 ][ 2 ] = m02;
m [ 0 ][ 3 ] = m03;
m [ 1 ][ 0 ] = m10;
m [ 1 ][ 1 ] = m11;
m [ 1 ][ 2 ] = m12;
m [ 1 ][ 3 ] = m13;
m [ 2 ][ 0 ] = m20;
m [ 2 ][ 1 ] = m21;
m [ 2 ][ 2 ] = m22;
m [ 2 ][ 3 ] = m23;
m [ 3 ][ 0 ] = m30;
m [ 3 ][ 1 ] = m31;
m [ 3 ][ 2 ] = m32;
m [ 3 ][ 3 ] = m33;
}
matrix transpose( ) const
{
return matrix(
m [ 0 ][ 0 ], m [ 1 ][ 0 ], m [ 2 ][ 0 ], m [ 3 ][ 0 ],
m [ 0 ][ 1 ], m [ 1 ][ 1 ], m [ 2 ][ 1 ], m [ 3 ][ 1 ],
m [ 0 ][ 2 ], m [ 1 ][ 2 ], m [ 2 ][ 2 ], m [ 3 ][ 2 ],
m [ 0 ][ 3 ], m [ 1 ][ 3 ], m [ 2 ][ 3 ], m [ 3 ][ 3 ] );
}
float m [ 4 ][ 4 ];
};
using vec2_t = vector2;
using vec3_t = vector3;
using vec4_t = vector4;
using mat4x4_t = matrix;
struct Matrix34
{
vector4 vec0;
vector4 vec1;
vector4 vec2;
};
}
}
}