-
Notifications
You must be signed in to change notification settings - Fork 2
/
vector.h
72 lines (59 loc) · 1.4 KB
/
vector.h
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
#ifndef __VECTOR_H__
#define __VECTOR_H__
#include <math.h>
typedef struct{
float x, y, z;
}vector;
__inline vector vector_make( float x, float y, float z ){
vector temp;
temp.x = x;
temp.y = y;
temp.z = z;
return temp;
}
__inline vector vector_add( vector v1, vector v2 ){
vector temp;
temp.x = v1.x + v2.x;
temp.y = v1.y + v2.y;
temp.z = v1.z + v2.z;
return temp;
}
__inline vector vector_sub( vector v1, vector v2 ){
vector temp;
temp.x = v1.x - v2.x;
temp.y = v1.y - v2.y;
temp.z = v1.z - v2.z;
return temp;
}
__inline vector vector_scale( vector v, float scalar ){
vector temp;
temp.x = v.x * scalar;
temp.y = v.y * scalar;
temp.z = v.z * scalar;
return temp;
}
__inline vector vector_lerp( vector v1, vector v2, float t){
return vector_add(v1, vector_scale(vector_sub(v2,v1),t) );
}
__inline float vector_magnitude( vector v ){
return (float)sqrt(v.x*v.x+v.y*v.y+v.z*v.z);
}
__inline vector vector_normalize(vector v){
vector temp;
float scale = 1.f/vector_magnitude(v);
temp.x = v.x*scale;
temp.y = v.y*scale;
temp.z = v.z*scale;
return temp;
}
__inline float vector_dotproduct( vector v1, vector v2 ){
return v1.x*v2.x + v1.y*v2.y + v1.z*v2.z;
}
__inline vector vector_crossproduct( const vector v1, const vector v2 ){
vector temp;
temp.x = (v1.y*v2.z)-(v1.z*v2.y);
temp.y = (v1.z*v2.x)-(v1.x*v2.z);
temp.z = (v1.x*v2.y)-(v1.y*v2.x);
return temp;
}
#endif /* __VECTOR_H__ */