-
Notifications
You must be signed in to change notification settings - Fork 17
/
ol3d_vector.c
60 lines (52 loc) · 1.51 KB
/
ol3d_vector.c
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
#include "ol3d_core.h"
// dot product
ol3d_Vector3_t ol3d_vector_dot(const ol3d_Vector3_t *a, const ol3d_Vector3_t *b) {
return (ol3d_Vector3_t){
.x = a->x * b->x,
.y = a->y * b->y,
.z = a->z * b->z
};
}
double ol3d_vector_length(const ol3d_Vector3_t *a) {
ol3d_Vector3_t temp = ol3d_vector_dot(a, a);
return (double)sqrt(temp.x + temp.y + temp.z);
}
ol3d_Vector3_t ol3d_vector_normalize(const ol3d_Vector3_t *a) {
return ol3d_vector_divide(a, ol3d_vector_length(a));
}
ol3d_Vector3_t ol3d_vector_add(const ol3d_Vector3_t *a, const ol3d_Vector3_t *b) {
return (ol3d_Vector3_t) {
.x = a->x + b->x,
.y = a->y + b->y,
.z = a->z + b->z
};
}
ol3d_Vector3_t ol3d_vector_subtract(const ol3d_Vector3_t *a, const ol3d_Vector3_t *b) {
return (ol3d_Vector3_t) {
.x = a->x - b->x,
.y = a->y - b->y,
.z = a->z - b->z
};
}
ol3d_Vector3_t ol3d_vector_multiply(const ol3d_Vector3_t *a, double b) {
return (ol3d_Vector3_t) {
.x = a->x * b,
.y = a->y * b,
.z = a->z * b
};
}
ol3d_Vector3_t ol3d_vector_divide(const ol3d_Vector3_t *a, double b) {
return (ol3d_Vector3_t) {
.x = a->x / b,
.y = a->y / b,
.z = a->z / b
};
}
// cross product
ol3d_Vector3_t ol3d_vector_cross(const ol3d_Vector3_t *a, const ol3d_Vector3_t *b) {
return (ol3d_Vector3_t) {
.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
};
}