-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVector3D.cpp
54 lines (44 loc) · 1.17 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
#include "Vector3D.hpp"
Vector3D::Vector3D(double x, double y, double z) //: x(x), y(y), z(z)
{
this->x = x;
this->y = y;
this->z = z;
}
void Vector3D::printVector() const
{
cout << '[' << x << ',' << y << ',' << z << ']' << endl;
}
double Vector3D::magnitude() const
{
//return sqrt(pow(x,2) + pow(y,2) + pow(z,2));
return sqrt(this->dot(*this));
}
Vector3D Vector3D::normalize() const
{
return *this / this->magnitude();
}
double Vector3D::dot(Vector3D other) const
{
return this->x * other.x + this->y * other.y + this->z * other.z;
}
Vector3D Vector3D::operator+(Vector3D other) const
{
return Vector3D(this->x + other.x, this->y + other.y, this->z + other.z);
}
Vector3D Vector3D::operator-(Vector3D other) const
{
return Vector3D(this->x - other.x, this->y - other.y, this->z - other.z);
}
Vector3D Vector3D::operator*(double number) const
{
return Vector3D(this->x * number, this->y * number, this->z * number);
}
Vector3D operator*(double number, const Vector3D& vector)
{
return vector * number;
}
Vector3D Vector3D::operator/(double number) const
{
return Vector3D(this->x / number, this->y / number, this->z / number);
}