-
Notifications
You must be signed in to change notification settings - Fork 0
/
100vector.c
85 lines (75 loc) · 2.56 KB
/
100vector.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
// Isaac Haseley - Nathan Mannes - CS311 - Winter 2017
/*** In general dimensions ***/
/* Copies the dim-dimensional vector v to the dim-dimensional vector copy. */
void vecCopy(int dim, double v[], double copy[]) {
for (int i = 0; i < dim; i++) {
copy[i] = v[i];
}
}
/* Adds the dim-dimensional vectors v and w. */
void vecAdd(int dim, double v[], double w[], double vPlusW[]) {
for (int i = 0; i < dim; i++) {
vPlusW[i] = v[i] + w[i];
}
}
/* Subtracts the dim-dimensional vectors v and w. */
void vecSubtract(int dim, double v[], double w[], double vMinusW[]) {
for (int i = 0; i < dim; i++) {
vMinusW[i] = v[i] - w[i];
}
}
/* Scales the dim-dimensional vector w by the number c. */
void vecScale(int dim, double c, double w[], double cTimesW[]) {
for (int i = 0; i < dim; i++) {
cTimesW[i] = c * w[i];
}
}
/* Assumes that there are dim + 2 arguments, the last dim of which are doubles.
Sets the dim-dimensional vector v to those doubles. */
void vecSet(int dim, double v[], ...) {
va_list argumentPointer;
va_start(argumentPointer, v);
for (int i = 0; i < dim; i++) {
v[i] = va_arg(argumentPointer, double);
}
va_end(argumentPointer);
}
/* Returns the dot product of the dim-dimensional vectors v and w. */
double vecDot(int dim, double v[], double w[]) {
int i;
double answer = 0;
for(i = 0; i < dim; i++) {
answer += v[i] * w[i];
}
return answer;
}
/* Returns the length of the dim-dimensional vector v. */
double vecLength(int dim, double v[]) {
double x = vecDot(dim, v, v);
return sqrt(x);
}
/* Returns the length of the dim-dimensional vector v. If the length is
non-zero, then also places a scaled version of v into the dim-dimensional
vector unit, so that unit has length 1. */
double vecUnit(int dim, double v[], double unit[]) {
double x = vecLength(dim, v);
if (x != 0) {
vecScale(dim, 1/x, v, unit);
}
return x;
}
/* Computes the cross product of the 3-dimensional vectors v and w, and places
it into vCrossW. */
void vec3Cross(double v[3], double w[3], double vCrossW[3]) {
vCrossW[0] = v[1] * w[2] - w[1] * v[2];
vCrossW[1] = w[0] * v[2] - v[0] * w[2];
vCrossW[2] = v[0] * w[1] - w[0] * v[1];
}
/* Computes the 3-dimensional vector v from its spherical coordinates.
rho >= 0.0 is the radius. 0 <= phi <= pi is the co-latitude. -pi <= theta <= pi
is the longitude or azimuth. */
void vec3Spherical(double rho, double phi, double theta, double v[3]) {
v[0] = rho * sin(phi) * cos(theta);
v[1] = rho * sin(phi) * sin(theta);
v[2] = rho * cos(phi);
}