-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector.h
70 lines (53 loc) · 1.75 KB
/
vector.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
#ifndef VECTOR_H
#define VECTOR_H
#include <math.h>
// 3D Vector
struct Vector3 {
float x;
float y;
float z;
};
struct Vector3 vector3_create(float x, float y, float z) {
struct Vector3 toRet;
toRet.x = x;
toRet.y = y;
toRet.z = z;
return toRet;
}
float vector3_length(struct Vector3 vector) {
return sqrtf(powf(vector.x, 2) + powf(vector.y, 2) + powf(vector.z, 2));
}
struct Vector3 vector3_normalize(struct Vector3 vector) {
float len = vector3_length(vector);
return vector3_create(vector.x / len, vector.y / len, vector.z / len);
}
struct Vector3 vector3_negate(struct Vector3 vector) {
return vector3_create(vector.x * -1, vector.y * -1, vector.z * -1);
}
struct Vector3 vector3_add(struct Vector3 one, struct Vector3 two) {
return vector3_create(one.x + two.x, one.y + two.y, one.z + two.z);
}
struct Vector3 vector3_subtract(struct Vector3 one, struct Vector3 two) {
return vector3_add(one, vector3_negate(two));
}
struct Vector3 vector3_multiply_scalar(struct Vector3 vector, float mult) {
return vector3_create(vector.x * mult, vector.y * mult, vector.z * mult);
}
struct Vector3 vector3_to_length(struct Vector3 vector, float len) {
struct Vector3 normalized = vector3_normalize(vector);
return vector3_multiply_scalar(normalized, len);
}
float vector3_dot_product(struct Vector3 one, struct Vector3 two) {
return one.x * two.x + one.y * two.y + one.z * two.z;
}
struct Vector3 vector3_cross_product(struct Vector3 one, struct Vector3 two) {
return vector3_create(
one.y * two.z - one.z * two.y,
one.z * two.x - one.x * two.z,
one.x * two.y - one.y * two.x
);
}
struct Vector3 vector3_facing(struct Vector3 one, struct Vector3 two) {
return vector3_normalize(vector3_subtract(two, one));
}
#endif //VECTOR_H