-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathVector3.cpp
106 lines (88 loc) · 2.15 KB
/
Vector3.cpp
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
#include "Util.h"
#include "Vector3.h"
namespace Fixie {
Vector3::Vector3() { }
Vector3::Vector3(Num x, Num y, Num z) {
components[0] = x;
components[1] = y;
components[2] = z;
}
Num& Vector3::operator[](const int index) {
return components[index];
}
const Num& Vector3::operator[](const int index) const {
return components[index];
}
Vector3 Vector3::operator+(Vector3 vector) {
Vector3 result = *this;
result += vector;
return result;
}
Vector3& Vector3::operator+=(Vector3 other) {
components[0] += other[0];
components[1] += other[1];
components[2] += other[2];
return *this;
}
Vector3 Vector3::operator-(Vector3 vector) {
Vector3 result = *this;
result -= vector;
return result;
}
Vector3& Vector3::operator-=(Vector3 other) {
components[0] -= other[0];
components[1] -= other[1];
components[2] -= other[2];
return *this;
}
Vector3 Vector3::operator*(Num divisor) {
Vector3 result = *this;
result *= divisor;
return result;
}
Vector3& Vector3::operator*=(Num divisor) {
components[0] *= divisor;
components[1] *= divisor;
components[2] *= divisor;
return *this;
}
Vector3 Vector3::operator/(Num divisor) {
Vector3 result = *this;
result /= divisor;
return result;
}
Vector3& Vector3::operator/=(Num divisor) {
components[0] /= divisor;
components[1] /= divisor;
components[2] /= divisor;
return *this;
}
Num Vector3::dot(Vector3 a, Vector3 b) {
return a[0]*b[0] + a[1]*b[1] + a[2]*b[2];
}
Vector3 Vector3::cross(Vector3 a, Vector3 b) {
return Vector3(
a[1]*b[2] - a[2]*b[1],
a[2]*b[0] - a[0]*b[2],
a[0]*b[1] - a[1]*b[0]
);
}
void Vector3::reset() {
components[0] = 0;
components[1] = 0;
components[2] = 0;
}
Vector3 Vector3::normalize(Vector3 v) {
return v/v.calcLength();
}
Num Vector3::calcLength() const {
return Util::sqrt(calcSquaredLength());
}
Num Vector3::calcSquaredLength() const {
return (
components[0] * components[0] +
components[1] * components[1] +
components[2] * components[2]
);
}
}