-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVector3D.cpp
84 lines (69 loc) · 1.61 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
#include <math.h>
#include "Vector3D.h"
bool Vector3D::operator==(const Vector3D& vec) {
return (x == vec.x && y == vec.y && z == vec.z);
}
bool Vector3D::operator!=(const Vector3D& vec) {
return !(x == vec.x && y == vec.y && z == vec.z);
}
// arithmetic
Vector3D Vector3D::operator+(const Vector3D& vec) {
return Vector3D(x + vec.x, y + vec.y, z + vec.z);
}
Vector3D Vector3D::operator-(const Vector3D& vec) {
return Vector3D(x - vec.x, y - vec.y, z - vec.z);
}
Vector3D Vector3D::operator*(double val) {
return Vector3D(x * val, y * val, z * val);
}
Vector3D Vector3D::operator/(double val) {
assert(val != 0);
return Vector3D(x / val, y / val, z / val);
}
Vector3D &Vector3D::operator=(const Vector3D& vec) {
x = vec.x;
y = vec.y;
z = vec.z;
return *this;
}
Vector3D &Vector3D::operator+=(const Vector3D& vec) {
x += vec.x;
y += vec.y;
z += vec.z;
return *this;
}
Vector3D& Vector3D::operator-=(const Vector3D& vec) {
x -= vec.x;
y -= vec.y;
z -= vec.z;
return *this;
}
Vector3D& Vector3D::operator*=(double val) {
x *= val;
y *= val;
z *= val;
return *this;
}
Vector3D& Vector3D::operator/=(double val) {
assert(val != 0);
x /= val;
y /= val;
z /= val;
return *this;
}
double Vector3D::dot(const Vector3D &vec) {
return x * vec.x + vec.y * y + vec.z * z;
}
Vector3D Vector3D::cross(const Vector3D& vec) {
double i = y * vec.z - z * vec.y;
double j = z * vec.x - x * vec.z;
double k = x * vec.y - y * vec.x;
return Vector3D(i, j, k);
}
double Vector3D::mag() {
return sqrt(x * x + y * y + z * z);
}
Vector3D Vector3D::normalized() {
assert(mag() != 0);
return *this / mag();
}