-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector3.h
More file actions
69 lines (53 loc) · 1.24 KB
/
vector3.h
File metadata and controls
69 lines (53 loc) · 1.24 KB
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
#ifndef __VECTOR3_H__
#define __VECTOR3_H__
#include <math.h>
class Vector3 {
public:
float f[3];
Vector3(float x, float y, float z) {
f[0] =x;
f[1] =y;
f[2] =z;
}
Vector3() {}
float length() const {
return sqrt(f[0]*f[0]+f[1]*f[1]+f[2]*f[2]);
}
Vector3 normalized() const {
float l = length();
return Vector3(f[0]/l,f[1]/l,f[2]/l);
}
void operator+= (const Vector3& v) {
f[0]+=v.f[0];
f[1]+=v.f[1];
f[2]+=v.f[2];
}
Vector3 operator/ (float a) const {
return Vector3(f[0]/a,f[1]/a,f[2]/a);
}
Vector3 operator- (const Vector3& v) const {
return Vector3(f[0]-v.f[0],f[1]-v.f[1],f[2]-v.f[2]);
}
Vector3 operator+ (const Vector3& v) const {
return Vector3(f[0]+v.f[0],f[1]+v.f[1],f[2]+v.f[2]);
}
Vector3 operator* (const float &a) const {
return Vector3(f[0]*a,f[1]*a,f[2]*a);
}
Vector3 operator *= (const float &a) {
f[0] *= a;
f[1] *= a;
f[2] *= a;
return *this;
}
Vector3 operator-() const {
return Vector3(-f[0],-f[1],-f[2]);
}
Vector3 cross(const Vector3& v) const {
return Vector3(f[1]*v.f[2] - f[2]*v.f[1], f[2]*v.f[0] - f[0]*v.f[2], f[0]*v.f[1] - f[1]*v.f[0]);
}
float dot(const Vector3& v) const {
return f[0]*v.f[0] + f[1]*v.f[1] + f[2]*v.f[2];
}
};
#endif // __VECTOR3_H__