-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvectors.c
102 lines (89 loc) · 1.5 KB
/
vectors.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
97
98
99
100
101
102
#include <math.h>
#include <stdlib.h>
struct Vectorf
{
float x;
float y;
float z;
float w;
};
Vectorf vecf(float x, float y, float z = 0.f, float w = 0.f)
{
Vectorf v;
v.x = x;
v.y = y;
v.z = z;
v.w = w;
return v;
}
// lengths
inline float veclen(Vectorf v)
{
return sqrt(v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w);
}
inline float veclensqr(Vectorf v)
{
return (v.x * v.x + v.y * v.y + v.z * v.z + v.w * v.w);
}
// arithmetic functions
inline Vectorf vecmult(Vectorf v1, Vectorf v2)
{
Vectorf v;
v.x = v1.x * v2.x;
v.y = v1.y * v2.y;
v.z = v1.z * v2.z;
v.w = v1.w * v2.w;
return v;
}
inline Vectorf vecadd(Vectorf v1, Vectorf v2)
{
Vectorf v;
v.x = v1.x + v2.x;
v.y = v1.y + v2.y;
v.z = v1.z + v2.z;
v.w = v1.w + v2.w;
return v;
}
inline Vectorf vecsub(Vectorf v1, Vectorf v2)
{
Vectorf v;
v.x = v1.x - v2.x;
v.y = v1.y - v2.y;
v.z = v1.z - v2.z;
v.w = v1.w - v2.w;
return v;
}
inline Vectorf vecscale(Vectorf v1, float f)
{
Vectorf v;
v.x = v1.x * f;
v.y = v1.y * f;
v.z = v1.z * f;
v.w = v1.w * f;
return v;
}
inline Vectorf normalize(Vectorf v1)
{
float f = veclen(v1);
if (f == 0.f)
{
return v1;
}
else
{
return vecscale(v1, 1.f/f);
}
}
inline float randomFloat()
{
return (float)rand()/(float)(RAND_MAX);
}
inline Vectorf randomBetween(Vectorf v1, Vectorf v2)
{
Vectorf v;
v.x = v1.x + (v2.x - v1.x) * randomFloat();
v.y = v1.y + (v2.y - v1.y) * randomFloat();
v.z = v1.z + (v2.z - v1.z) * randomFloat();
v.w = v1.w + (v2.w - v1.w) * randomFloat();
return v;
}