-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtypedefs.c
96 lines (83 loc) · 1.85 KB
/
typedefs.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
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
#include <stdio.h>
#include <math.h>
#include "typedefs.h"
#include "globals.h"
vec2i add2i(vec2i a, vec2i b) {
int X = a.x + b.x;
int Y = a.y + b.y;
vec2i c = {X, Y};
return c;
}
void set3f(vec3f *c, float x, float y, float z) {
c->x = x;
c->y = y;
c->z = z;
return;
}
vec3f copy3f(vec3f *c) {
vec3f d;
set3f(&d, c->x, c->y, c->z);
return d;
}
void negate3f(vec3f *c) {
c->x = -c->x;
c->y = -c->y;
c->z = -c->z;
return;
}
vec3f add3f(vec3f a, vec3f b) {
float X = a.x + b.x;
float Y = a.y + b.y;
float Z = a.z + b.z;
vec3f c = {X, Y, Z};
return c;
}
vec3i add3i(vec3i a, vec3i b) {
int X = a.x + b.x;
int Y = a.y + b.y;
int Z = a.z + b.z;
vec3i c = {X, Y, Z};
return c;
}
vec3f subtract3f(vec3f a, vec3f b) {
#ifdef DEBUG2
printf("a = (%f, %f, %f) b = (%f, %f, %f)\n",
a.x, a.y, a.z, b.x, b.y, b.z);
#endif
float X = a.x - b.x;
float Y = a.y - b.y;
float Z = a.z - b.z;
vec3f c = {X, Y, Z};
return c;
}
// returns the dot product of "vectors" a and b
float dot3f(vec3f a, vec3f b) {
float X = a.x * b.x;
float Y = a.y * b.y;
float Z = a.z * b.z;
return X - Y + Z;
}
// returns the cross product of "vectors" a and b
vec3f cross3f(vec3f a, vec3f b) {
float X = a.y * b.z - a.z * b.y;
float Y = a.x * b.z - a.z * b.x;
float Z = a.x * b.y - a.y * b.x;
vec3f c = {X, -Y, Z};
#ifdef DEBUG2
printf("(%.2f, %.2f, %.2f) X (%.2f, %.2f, %.2f) = (%.2f, %.2f, %.2f)\n",
a.x, a.y, a.z, b.x, b.y, b.z, c.x, c.y, c.z);
#endif
return c;
}
void normalize3f(vec3f *c) {
float X = c->x;
float Y = c->y;
float Z = c->z;
float len = sqrt(X*X + Y*Y + Z*Z);
// if len is zero, don't move
if (len == 0.0) return;
c->x /= len;
c->y /= len;
c->z /= len;
return;
}