-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmath.h
More file actions
233 lines (183 loc) · 3.62 KB
/
math.h
File metadata and controls
233 lines (183 loc) · 3.62 KB
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
#pragma once
#include <math.h>
#define PI 3.14159265359
namespace Math
{
typedef struct Vector3
{
float x, y, z;
Vector3() = default;
Vector3(float X, float Y, float Z)
{
x = X;
y = Y;
z = Z;
}
Vector3& operator*=(float n)
{
x *= n;
y *= n;
z *= n;
return(*this);
}
Vector3& operator+=(const Vector3& v)
{
x += v.x;
y += v.y;
z += v.z;
return(*this);
}
Vector3& operator-=(const Vector3& v)
{
x -= v.x;
y -= v.y;
z -= v.z;
return(*this);
}
Vector3& operator/=(float n)
{
x /= n;
y /= n;
z /= n;
return(*this);
}
} Vector3;
typedef struct Vector2
{
float x, y;
Vector2() = default;
Vector2(float X, float Y)
{
x = X;
y = Y;
}
Vector2& operator*=(float n)
{
x *= n;
y *= n;
return(*this);
}
Vector2& operator+=(float n)
{
x += n;
y += n;
return(*this);
}
Vector2& operator-=(float n)
{
x -= n;
y -= n;
return(*this);
}
Vector2& operator/=(float n)
{
x /= n;
y /= n;
return(*this);
}
} Vector2;
typedef struct Vector4
{
float x, y, z, n;
Vector4() = default;
Vector4(float X, float Y, float Z, float N)
{
x = X;
y = Y;
z = Z;
n = N;
}
Vector4& operator*=(float n)
{
x *= n;
y *= n;
z *= n;
this->n *= n;
return(*this);
}
Vector4& operator+=(float n)
{
x += n;
y += n;
z += n;
this->n += n;
return(*this);
}
Vector4& operator-=(float n)
{
x -= n;
y -= n;
z -= n;
this->n -= n;
return(*this);
}
Vector4& operator/=(float n)
{
x /= n;
y /= n;
z /= n;
this->n /= n;
return(*this);
}
}Vector4;
typedef struct Matrix4
{
float m[4][4];
Matrix4() = default;
Matrix4
(
float m00, float m10, float m20, float m30,
float m01, float m11, float m21, float m31,
float m02, float m12, float m22, float m32,
float m03, float m13, float m23, float m33
)
{
m[0][0] = m00; m[1][0] = m10; m[2][0] = m20; m[3][0] = m30;
m[0][1] = m01; m[1][1] = m11; m[2][1] = m21; m[3][1] = m31;
m[0][2] = m02; m[1][2] = m12; m[2][2] = m22; m[3][2] = m32;
m[0][3] = m03; m[1][3] = m13; m[2][3] = m23; m[3][3] = m33;
}
} Matrix4;
typedef struct Matrix3
{
float m[3][3];
Matrix3() = default;
Matrix3
(
float m00, float m10, float m20,
float m01, float m11, float m21,
float m02, float m12, float m22
)
{
m[0][0] = m00; m[1][0] = m10; m[2][0] = m20;
m[0][1] = m01; m[1][1] = m11; m[2][1] = m21;
m[0][2] = m02; m[1][2] = m12; m[2][2] = m22;
}
} Matrix3;
//Prototypes
Vector3 Normalize(const Vector3 a);
Vector3 Crossproduct(const Vector3 a, const Vector3 b);
inline Vector3 operator *(const Vector3& v1, const Vector3& v2);
inline Vector3 operator +(const Vector3& v1, const Vector3& v2);
inline Vector3 operator -(const Vector3& v1, const Vector3& v2);
inline Vector3 operator /(const Vector3& v1, const Vector3& v2);
float Dotproduct(const Vector3 a, const Vector3 b);
float ToRadians(float deg);
float Cotangens(float deg);
float Magnitude(Vector3 a);
Vector3 V4toV3(const Vector4& v1);
Vector4 MultByScalar(Vector4 v, float s);
Vector3 MultByScalar(Vector3 v, float s);
Vector2 MultByScalar(Vector2 v, float s);
Matrix4 Transpose(Matrix4 m);
Matrix4 operator *(Matrix4 a, Matrix4 b);
Matrix4 RotateX(float t);
Matrix4 RotateY(float t);
Matrix4 RotateZ(float t);
Matrix4 Translate(Vector3 t);
Matrix4 Scale(Vector3 s);
Matrix4 Perspective(float fov, float aspect_ratio, float near, float far);
Matrix4 To3x3(Matrix4 i);
Matrix4 lookAt(Vector3 from, Vector3 to, Vector3 up);
Matrix4 Inverse(const Matrix4& i);
}