-
Notifications
You must be signed in to change notification settings - Fork 2
/
vector3d.cpp
161 lines (132 loc) · 2.83 KB
/
vector3d.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
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
#include "vector3d.h"
#include <math.h>
Vector3D::Vector3D(void) :
x(0),
y(0),
z(0) {
x = y = z = .0;
}
Vector3D::Vector3D(vec_t X, vec_t Y, vec_t Z) :
x(X),
y(Y),
z(Z) {
}
Vector3D::Vector3D(const vec_t *array) :
x(array[0]),
y(array[1]),
z(array[2]) {
}
Vector3D::Vector3D(const Vector3D &other) :
x(other.x),
y(other.y),
z(other.z) {
}
Vector3D& Vector3D::operator=(const Vector3D &other) {
x = other.x;
y = other.y;
z = other.z;
return *this;
}
void Vector3D::toArray(vec_t *dst) {
dst[0] = x;
dst[1] = y;
dst[2] = z;
}
vec_t& Vector3D::operator[](int index) {
switch(index){
case 0:
return x;
case 1:
return y;
}
return z;
}
bool Vector3D::operator==(Vector3D const &other) const {
return (other.x == x) && (other.y == y) && (other.z == z);
}
bool Vector3D::operator!=(Vector3D const &other) const {
return (other.x != x) || (other.y != y) || (other.z != z);
}
Vector3D& Vector3D::operator+=(const Vector3D &v) {
x += v.x;
y += v.y;
z += v.z;
return *this;
}
Vector3D& Vector3D::operator-=(const Vector3D &v) {
x -=v.x;
y -=v.y;
z -= v.z;
return *this;
}
Vector3D& Vector3D::operator*=(vec_t val) {
x *= val;
y *= val;
z *= val;
return *this;
}
Vector3D& Vector3D::operator*=(const Vector3D &v) {
x *= v.x;
y *= v.y;
z *= v.z;
return *this;
}
Vector3D& Vector3D::operator/=(vec_t val) {
//@NOTE: We shall do some checking for 0 here!
vec_t div = 1.0f / val;
x *= div;
y *= div;
z *= div;
return *this;
}
Vector3D& Vector3D::operator/=(const Vector3D &v) {
x /= v.x;
y /= v.y;
z /= v.z;
return *this;
}
Vector3D Vector3D::operator+(const Vector3D &v) const {
return Vector3D(x + v.x, y + v.y, z + v.z);
}
Vector3D Vector3D::operator-(const Vector3D &v) const {
return Vector3D(x - v.x, y - v.y, z - v.z);
}
Vector3D Vector3D::operator*(vec_t val) const {
return Vector3D(x * val, y * val, z * val);
}
Vector3D Vector3D::operator*(const Vector3D &v) const {
return Vector3D(x * v.x, y * v.y, z * v.z);
}
Vector3D Vector3D::operator/(vec_t val) const {
//@NOTE: We shall do some checking for 0 here!
vec_t div = 1.0 / val;
return Vector3D(x * div, y * div, z * div);
}
Vector3D Vector3D::operator/(const Vector3D &v) const {
return Vector3D(x / v.x, y / v.y, z / v.z);
}
vec_t Vector3D::length() const {
return sqrt(x*x + y*y + z*z);
}
void Vector3D::normalize() {
vec_t len = length();
if(len != 0.0) {
x = x / len;
y = y / len;
z = z / len;
}
}
Vector3D Vector3D::normalized() const {
Vector3D copy = *this;
copy.normalize();
return copy;
}
vec_t Vector3D::dot(const Vector3D &v) const {
return (x*v.x + y*v.y + z*v.z);
}
Vector3D Vector3D::cross(const Vector3D &v) const {
vec_t X = y * v.z - z * v.y;
vec_t Y = z * v.x - x * v.z;
vec_t Z = x * v.y - y * v.x;
return Vector3D(X, Y, Z);
}