forked from avose/ChromoCraft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector.c
181 lines (148 loc) · 3.05 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#ifndef VECTOR_C
#define VECTOR_C
#include <math.h>
#include "vector.h"
////////////////////////////////////////////////////////////
int vector3_compare(vector3_t *v1, vector3_t *v2)
{
return (v1->s.x == v2->s.x) &&
(v1->s.y == v2->s.y) &&
(v1->s.z == v2->s.z) ;
}
//
// l = sqrt(pow(v[i],2));
//
double vector3_length(vector3_t *v)
{
return sqrt( v->a[0]*v->a[0] + v->a[1]*v->a[1] + v->a[2]*v->a[2] );
}
//
// v2[i] = v1[i]*s
//
vector3_t* vector3_mult_scalar(vector3_t *v1, vector3_t *v2, const double s)
{
v2->s.x = v1->s.x * s;
v2->s.y = v1->s.y * s;
v2->s.z = v1->s.z * s;
return v2;
}
//
// v2[i] = v1[i] + s
//
vector3_t* vector3_add_scalar(vector3_t *v1, vector3_t *v2, const double s)
{
v2->s.x = v1->s.x + s;
v2->s.y = v1->s.y + s;
v2->s.z = v1->s.z + s;
return v2;
}
//
// return dot(v1, v2);
//
double vector3_dotprod(vector3_t *v1, vector3_t *v2)
{
return v1->s.x*v2->s.x + v1->s.y*v2->s.y + v1->s.z*v2->s.z;
}
//
// v3[i] = v1[i] + v2[i]
//
vector3_t* vector3_add_vector(vector3_t *v1, vector3_t *v2, vector3_t *v3)
{
v3->s.x = v1->s.x + v2->s.x;
v3->s.y = v1->s.y + v2->s.y;
v3->s.z = v1->s.z + v2->s.z;
return v3;
}
//
// v3[i] = v1[i] - v2[i]
//
vector3_t* vector3_sub_vector(vector3_t *v1, vector3_t *v2, vector3_t *v3)
{
v3->s.x = v1->s.x - v2->s.x;
v3->s.y = v1->s.y - v2->s.y;
v3->s.z = v1->s.z - v2->s.z;
return v3;
}
//
// v3 = cross_product(v1,v2)
//
vector3_t* vector3_crossprod(vector3_t *v1, vector3_t *v2, vector3_t *v3)
{
v3->s.x = v1->s.y * v2->s.z - v1->s.z * v2->s.y;
v3->s.y = v1->s.z * v2->s.x - v1->s.x * v2->s.z;
v3->s.z = v1->s.x * v2->s.y - v1->s.y * v2->s.x;
return v3;
}
//
// v2 = normalize(v1)
//
vector3_t* vector3_normalize(vector3_t *v1, vector3_t *v2)
{
const double l = sqrt( v1->s.x*v1->s.x + v1->s.y*v1->s.y + v1->s.z*v1->s.z);
v2->s.x = v1->s.x / l;
v2->s.y = v1->s.y / l;
v2->s.z = v1->s.z / l;
return v2;
}
//
// v2[i] = v1[i]
//
vector3_t* vector3_copy(vector3_t *v1, vector3_t *v2)
{
v2->s.x = v1->s.x;
v2->s.y = v1->s.y;
v2->s.z = v1->s.z;
return v2;
}
//
// return dist( (point)x, (point)l_1, (point)l_2 );
//
double vector3_point_line_dist(vector3_t *x, vector3_t *l1, vector3_t *l2)
{
vector3_t l2ml1,l1mx;
double l2ml1l,l1mxl;
vector3_sub_vector(l2, l1, &l2ml1);
vector3_sub_vector(l1, x, &l1mx );
l2ml1l = vector3_length(&l2ml1);
l1mxl = vector3_length(&l1mx);
return (l1mxl*l1mxl*l2ml1l*l2ml1l - vector3_dotprod(&l1mx,&l2ml1))
/
(l2ml1l*l2ml1l);
}
////////////////////////////////////////////////////////////
//
//
//
vector4_t* vector4_mult_scalar(vector4_t *v1, vector4_t *v2, const double s)
{
return v2;
}
//
//
//
vector4_t* vector4_add_scalar(vector4_t *v1, vector4_t *v2, const double s)
{
return v2;
}
//
//
//
vector4_t* vector4_add_vector(vector4_t *v1, vector4_t *v2, vector4_t *v3)
{
return v3;
}
//
//
//
vector4_t* vector4_sub_vector(vector4_t *v1, vector4_t *v2, vector4_t *v3)
{
return v3;
}
//
//
//
vector4_t* vector4_copy(vector4_t *v1, vector4_t *v2)
{
return v2;
}
#endif