This repository has been archived by the owner on Feb 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmathn.h
321 lines (276 loc) · 4.36 KB
/
mathn.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
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
#pragma once
#include <math.h>
float Lerp(float value1, float value2, float amount)
{
return value1 + (value2 - value1) * amount;
}
struct Matrix
{
float _11, _12, _13, _14;
float _21, _22, _23, _24;
float _31, _32, _33, _34;
float _41, _42, _43, _44;
void Reset() {
_11 = 0;
_12 = 0;
_13 = 0;
_14 = 0;
_21 = 0;
_22 = 0;
_23 = 0;
_24 = 0;
_31 = 0;
_32 = 0;
_33 = 0;
_34 = 0;
_41 = 0;
_42 = 0;
_43 = 0;
_44 = 0;
}
};
struct quat {
float x, y, z, w;
void Reset() {
x = 0;
y = 0;
z = 0;
w = 0;
}
};
struct vec3 {
float X;
float Y;
float Z;
vec3() {
X = 0;
Y = 0;
Z = 0;
}
vec3(float x, float y, float z) {
X = x;
Y = y;
Z = z;
}
void Reset() {
X = 0;
Y = 0;
Z = 0;
}
float Magnitude() {
return (float)sqrt((double)(this->X * this->X + this->Y * this->Y + this->Z * this->Z));
}
vec3 Normalized() {
float magnitude = Magnitude();
bool flag = magnitude > 1E-05f;
vec3 result;
static vec3 _this;
_this.X = X;
_this.Y = Y;
_this.Z = Z;
if (flag)
result = _this / magnitude;
else
result = vec3(0,0,0);
return result;
}
bool operator != (vec3 other) {
bool result = X != other.X || Y != other.Y || Z != other.Z;
return result;
}
bool operator == (vec3 other) {
return X == other.X && Y == other.Y && Z == other.Z;
}
vec3 operator -= (vec3 other) {
static vec3 buf;
buf.X = X;
buf.Y = Y;
buf.Z = Z;
buf.X -= other.X;
buf.Y -= other.Y;
buf.Z -= other.Z;
return buf;
}
vec3 operator * (float other)
{
static vec3 buf;
buf.X = X;
buf.Y = Y;
buf.Z = Z;
buf.X = buf.X * other;
buf.Y = buf.Y * other;
buf.Z = buf.Z * other;
return buf;
}
vec3 operator + (float other)
{
static vec3 buf;
buf.X = X;
buf.Y = Y;
buf.Z = Z;
buf.X += other;
buf.Y += other;
buf.Z += other;
return buf;
}
vec3 operator - (vec3 other)
{
static vec3 buf;
buf.X = X;
buf.Y = Y;
buf.Z = Z;
buf.X = X - other.X;
buf.Y = Y - other.Y;
buf.Z = Z - other.Z;
return buf;
}
vec3 operator + (vec3 other)
{
static vec3 buf;
buf.X = X + other.X;
buf.Y = Y + other.Y;
buf.Z = Z + other.Z;
return buf;
}
vec3 operator += (vec3 other)
{
static vec3 buf;
buf.X = X;
buf.Y = Y;
buf.Z = Z;
buf.X += other.X;
buf.Y += other.Y;
buf.Z += other.Z;
return buf;
}
vec3 operator / (float other)
{
static vec3 buf;
buf.X = X / other;
buf.Y = Y / other;
buf.Z = Z / other;
return buf;
}
vec3& operator *= (float other)
{
static vec3 buf;
buf.X = X;
buf.Y = Y;
buf.Z = Z;
buf.X *= other;
buf.Y *= other;
buf.Z *= other;
return *this;
}
float get_length_sqr(void) const
{
float buf;
buf += (X * X);
buf += (Y * Y);
buf += (Z * Z);
return buf;
}
};
struct vec2 {
float X;
float Y;
void Reset() {
X = 0;
Y = 0;
}
float Magnitude() {
return (float)sqrt((double)(this->X * this->X + this->Y * this->Y));
}
vec2(float x, float y) {
X = x;
Y = y;
}
vec2() {
X = 0;
Y = 0;
}
vec2 Normalized() {
float magnitude = Magnitude();
bool flag = magnitude > 1E-05f;
vec2 result;
vec2 _this = vec2(this->X, this->Y);
if (flag)
result = _this / magnitude;
else
result = vec2(0, 0);
return result;
}
bool operator != (vec2 other) {
bool result = X != other.X || Y != other.Y;
return result;
}
bool operator == (vec2 other) {
return X == other.X && Y == other.Y;
}
vec2 operator -= (vec2 other) {
vec2 buf = vec2(X,Y);
buf.X -= other.X;
buf.Y -= other.Y;
return buf;
}
vec2 operator * (float other)
{
vec2 buf;
buf.X *= other;
buf.Y *= other;
return buf;
}
vec2 operator + (float other)
{
vec2 buf;
buf.X += other;
buf.Y += other;
return buf;
}
vec2 operator - (vec2 other)
{
vec2 buf;
buf.X = X - other.X;
buf.Y = Y - other.Y;
return buf;
}
vec2 operator + (vec2 other)
{
vec2 buf;
buf.X = X + other.X;
buf.Y = Y + other.Y;
return buf;
}
vec2 operator += (vec2 other)
{
vec2 buf;
buf.X = X;
buf.Y = Y;
buf.X += other.X;
buf.Y += other.Y;
return buf;
}
vec2 operator / (float other)
{
vec2 buf;
buf.X /= other;
buf.Y /= other;
return buf;
}
vec2& operator *= (float other)
{
X *= other;
Y *= other;
return *this;
}
float get_length_sqr(void) const
{
float buf;
buf += (X * X);
buf += (Y * Y);
return buf;
}
};
typedef quat UQuaternion;
typedef vec3 UVector3;
typedef vec2 UVector2;