-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVector3.cpp
113 lines (85 loc) · 1.98 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
107
108
109
110
111
112
113
#include "Vector3.h"
Vector3::Vector3(double a, double b, double c) {
this->x = a;
this->y = b;
this->z = c;
}
double Vector3::magnitude() const {
return sqrt(pow(x, 2) + pow(y, 2) + pow(z, 2));
}
Vector3 Vector3::normalize() const {
return (*this) / this->magnitude();
}
double Vector3::dot(const Vector3& v) {
return (this->x * v.x) + (this->y * v.y) + (this->z * v.z);
}
// Getters
double Vector3::getX() const {
return this->x;
}
double Vector3::getY() const {
return this->y;
}
double Vector3::getZ() const {
return this->z;
}
// Operators
// TODO: Overloading depends on operands position, should add all cases
// Vector3 * double ---> works
// double * Vector3 ---> nope!
Vector3 Vector3::operator/(const double v) const {
double _x = this->x / v;
double _y = this->y / v;
double _z = this->z / v;
Vector3 res(_x, _y, _z);
return res;
}
Vector3& Vector3::operator/=(const double v) {
this->x /= v;
this->y /= v;
this->z /= v;
return (*this);
}
Vector3 Vector3::operator+(const Vector3& v) const {
double _x = this->x + v.x;
double _y = this->y + v.y;
double _z = this->z + v.z;
Vector3 res(_x, _y, _z);
return res;
}
Vector3& Vector3::operator+=(const Vector3& v) {
this->x += v.x;
this->x += v.y;
this->x += v.z;
return (*this);
}
Vector3 Vector3::operator-(const Vector3& v) const {
double _x = this->x - v.x;
double _y = this->y - v.y;
double _z = this->z - v.z;
Vector3 res(_x, _y, _z);
return res;
}
Vector3& Vector3::operator-=(const Vector3& v) {
this->x -= v.x;
this->x -= v.y;
this->x -= v.z;
return (*this);
}
Vector3 Vector3::operator*(const double v) const {
double _x = this->x * v;
double _y = this->y * v;
double _z = this->z * v;
Vector3 res(_x, _y, _z);
return res;
}
Vector3& Vector3::operator*=(const double v) {
this->x *= v;
this->y *= v;
this->z *= v;
return (*this);
}
std::ostream& operator<<(std::ostream& out, const Vector3& v) {
out << "(x: " << v.getX() << ", y: " << v.getY() << ", z: " << v.getZ() << ")";
return out;
}