forked from radforddc/icpc_siggen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoint.c
121 lines (92 loc) · 2.16 KB
/
point.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
/* point.c
* Karin Lagergren
*
* Cartesian points and vectors for signal calculation code
*
* November 2007, added dot product, vector subtraction, cross product
*/
#include <stdio.h>
#include <math.h>
#include "point.h"
#define SQ(x) ((x)*(x))
/* norm
* returns the absolute value of vector v
* e is unit vector in v's direction
*/
float vector_norm(vector v, vector *e){
float length;
length = vector_length(v);
e->x = v.x / length;
e->y = v.y / length;
e->z = v.z / length;
return length;
}
/* vector_length
* returns the length of vector v
*/
float vector_length(vector v){
return sqrt(SQ(v.x) + SQ(v.y) + SQ(v.z));
}
/* distance
* returns distance between points
*/
float distance(point pt1, point pt2){
float d;
d = sqrt(SQ(pt1.x - pt2.x) + SQ(pt1.y - pt2.y) + SQ(pt1.z - pt2.z));
return d;
}
/* vector_add */
vector vector_add(vector v1, vector v2){
vector v;
v.x = v1.x + v2.x;
v.y = v1.y + v2.y;
v.z = v1.z + v2.z;
return v;
}
/*vector subtraction, v1-v2*/
vector vector_sub(vector v1, vector v2){
vector v;
v.x = v1.x - v2.x;
v.y = v1.y - v2.y;
v.z = v1.z - v2.z;
return v;
}
/*vector dot product, v1*v1*/
float dot_prod(vector v1, vector v2){
return v1.x*v2.x + v1.y*v2.y + v1.z*v2.z;
}
/*vector cross product, v1 X v2*/
vector cross_prod(vector v1, vector v2){
vector v;
v.x = v1.y*v2.z - v1.z*v2.y;
v.y = v1.z*v2.x - v1.x*v2.z;
v.z = v1.x*v2.y - v1.y*v2.x;
return v;
}
/*scale vector by factor -- better name?*/
vector vector_scale(vector v, float factor){
vector res;
res.x = v.x*factor;
res.y = v.y*factor;
res.z = v.z*factor;
return res;
}
/*rotate vector by angle_deg degrees*/
vector vector_rotate_z(vector v, float angle_deg){
float angle_rad;
vector res;
angle_rad = angle_deg/180.0*M_PI;
res.x = cos(angle_rad)*v.x - sin(angle_rad)*v.y;
res.y = sin(angle_rad)*v.x + cos(angle_rad)*v.y;
res.z = v.z;
return res;
}
/* pt_to_str
* string representation of point pt
* written to string str, which has guaranteed length len
*/
char *pt_to_str(char *str, int len, point pt){
snprintf(str, len, "(%.1f %.1f %.1f)", pt.x, pt.y, pt.z);
return str;
}
#undef SQ