-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVector3.cpp
54 lines (42 loc) · 1.12 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
#include "Vector3.h"
Vector3::Vector3() : x(0), y(0), z(0) {}
Vector3 Vector3::normalize() const {
double mag = magnitude();
if (mag == 0) {
return *this;
} else {
return Vector3(x / mag, y / mag, z / mag);
}
}
double Vector3::magnitude() const {
return std::sqrt(x * x + y * y + z * z);
}
Vector3 Vector3::operator-(Vector3 v2) const {
return Vector3(x - v2.x, y - v2.y, z - v2.z);
}
double Vector3::dot(Vector3 v2) const {
return x * v2.x + y * v2.y + z * v2.z;
}
Vector3 Vector3::operator*(double s) const {
return Vector3(x * s, y * s, z * s);
}
Vector3 Vector3::operator/(double s) const {
return Vector3(x / s, y / s, z /s);
}
Vector3 Vector3::cross(Vector3 v2) const {
return Vector3(y * v2.z - z * v2.y, z * v2.x - x * v2.z, x * v2.y - y * v2.x);
}
Vector3 Vector3::operator-() const {
return Vector3(-x, -y, -z);
}
Vector3 Vector3::operator+(Vector3 v2) const {
return Vector3(x + v2.x, y + v2.y, z + v2.z);
}
void Vector3::operator+=(Vector3 v2) {
x += v2.x;
y += v2.y;
z += v2.z;
}
double Vector3::angleWith(Vector3 v2) const {
return acos(dot(v2) / (magnitude() * v2.magnitude()));
}