-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVector.cpp
168 lines (145 loc) · 3.82 KB
/
Vector.cpp
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
#include "Vector.h"
#include <math.h>
///////////////////////////////////////////////////////////////////////////////
// Implementations of Vector 2D functions
///////////////////////////////////////////////////////////////////////////////
vec2_t vec2_new(float x, float y) {
vec2_t result = { x, y };
return result;
}
float vec2_length(vec2_t v) {
return sqrt(v.x * v.x + v.y * v.y);
}
vec2_t vec2_add(vec2_t a, vec2_t b) {
vec2_t result = {
.x = a.x + b.x,
.y = a.y + b.y
};
return result;
}
vec2_t vec2_sub(vec2_t a, vec2_t b) {
vec2_t result = {
.x = a.x - b.x,
.y = a.y - b.y
};
return result;
}
vec2_t vec2_mul(vec2_t v, float factor) {
vec2_t result = {
.x = v.x * factor,
.y = v.y * factor
};
return result;
}
vec2_t vec2_div(vec2_t v, float factor) {
vec2_t result = {
.x = v.x / factor,
.y = v.y / factor
};
return result;
}
float vec2_dot(vec2_t a, vec2_t b) {
return (a.x * b.x) + (a.y * b.y);
}
///////////////////////////////////////////////////////////////////////////////
// Implementations of Vector 3D functions
///////////////////////////////////////////////////////////////////////////////
vec3_t vec3_new(float x, float y, float z) {
vec3_t result = { x, y, z };
return result;
}
vec3_t vec3_clone(vec3_t* v) {
vec3_t result = { v->x, v->y, v->z };
return result;
}
float vec3_length(vec3_t v) {
return sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
}
vec3_t vec3_add(vec3_t a, vec3_t b) {
vec3_t result = {
.x = a.x + b.x,
.y = a.y + b.y,
.z = a.z + b.z
};
return result;
}
vec3_t vec3_sub(vec3_t a, vec3_t b) {
vec3_t result = {
.x = a.x - b.x,
.y = a.y - b.y,
.z = a.z - b.z
};
return result;
}
vec3_t vec3_mul(vec3_t v, float factor) {
vec3_t result = {
.x = v.x * factor,
.y = v.y * factor,
.z = v.z * factor
};
return result;
}
vec3_t vec3_div(vec3_t v, float factor) {
vec3_t result = {
.x = v.x / factor,
.y = v.y / factor,
.z = v.z / factor
};
return result;
}
vec3_t vec3_cross(vec3_t a, vec3_t b) {
vec3_t result = {
.x = a.y * b.z - a.z * b.y,
.y = a.z * b.x - a.x * b.z,
.z = a.x * b.y - a.y * b.x
};
return result;
}
float vec3_dot(vec3_t a, vec3_t b) {
return (a.x * b.x) + (a.y * b.y) + (a.z * b.z);
}
vec3_t vec3_rotate_x(vec3_t v, float angle) {
vec3_t rotated_vector = {
.x = v.x,
.y = v.y * (float)cos(angle) - v.z * (float)sin(angle),
.z = v.y * (float)sin(angle) + v.z * (float)cos(angle)
};
return rotated_vector;
}
vec3_t vec3_rotate_y(vec3_t v, float angle) {
vec3_t rotated_vector = {
.x = v.x * (float)cos(angle) - v.z * (float)sin(angle),
.y = v.y,
.z = v.x * (float)sin(angle) + v.z * (float)cos(angle)
};
return rotated_vector;
}
vec3_t vec3_rotate_z(vec3_t v, float angle) {
vec3_t rotated_vector = {
.x = v.x * (float)cos(angle) - v.y * (float)sin(angle),
.y = v.x * (float)sin(angle) + v.y * (float)cos(angle),
.z = v.z
};
return rotated_vector;
}
void vec3_normalize(vec3_t* v) {
float length = sqrt(v->x * v->x + v->y * v->y + v->z * v->z);
v->x /= length;
v->y /= length;
v->z /= length;
}
///////////////////////////////////////////////////////////////////////////////
// Implementations of Vector conversion functions
///////////////////////////////////////////////////////////////////////////////
vec4_t vec4_from_vec3(vec3_t v) {
vec4_t result = { v.x, v.y, v.z, 1.0 };
return result;
}
vec3_t vec3_from_vec4(vec4_t v) {
vec3_t result = { v.x, v.y, v.z };
return result;
}
vec2_t vec2_from_vec4(vec4_t v) {
vec2_t result = { v.x, v.y };
return result;
}