-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector.c
95 lines (74 loc) · 1.37 KB
/
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
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
#include "vector.h"
/*
Gives the cross product between two vectors.
*/
vector cross(vector a, vector b)
{
vector res;
res.x = (a.y * b.z) - (b.y * a.z);
res.y = (a.x * b.z) - (b.x * a.z);
res.z = (a.x * b.y) - (b.x * a.y);
return res;
}
/*
Gives the dot product between two vectors.
*/
double dot(vector a, vector b)
{
double res = (a.x * b.x) + (a.y * b.y) + (a.z * b.z);
return res;
}
/*
Gives the vector sum of two vectors.
*/
vector vector_add(vector a, vector b)
{
vector res;
res.x = a.x + b.x;
res.y = a.y + b.y;
res.z = a.z + b.z;
return res;
}
/*
Gives the vector subtraction of two vectors.
*/
vector vector_minus(vector a, vector b)
{
vector minus_b;
minus_b.x = -b.x;
minus_b.y = -b.y;
minus_b.z = -b.z;
vector res = vector_add(a, minus_b);
return res;
}
/*
Gives the resultant of the product between a scalar and a vector.
*/
vector scalar_prod(double scalar, vector a)
{
vector res;
res.x = scalar * a.x;
res.y = scalar * a.y;
res.z = scalar * a.z;
return res;
}
/*
Calculates the mod of the vector.
*/
double mod_vector(vector a)
{
double res = a.x * a.x + a.y * a.y + a.z * a.z;
res = sqrt(res);
return res;
}
/*
Give the unit vector r^ in the direction of the vector r.
*/
vector unit_vector(vector a)
{
vector res;
double mod = mod_vector(a);
mod = 1.0 / mod;
res = scalar_prod(mod, a);
return res;
}