-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vector3D.cpp
85 lines (71 loc) · 1.98 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
#include "Vector3D.h"
#include <math.h>
Vector3D::Vector3D() : x(0), y(0), z(1) {}
Vector3D::Vector3D(const Vector3D &v) : x(v.x), y(v.y), z(v.z) {}
Vector3D::Vector3D(double n) : x(n), y(n), z(n) {}
Vector3D::Vector3D(double v_x, double v_y, double v_z) : x(v_x), y(v_y), z(v_z) {}
Vector3D &Vector3D::operator=(const Vector3D &v)
{
x = v.x;
y = v.y;
z = v.z;
return *this;
}
Vector3D Vector3D::operator+(const Vector3D &v) const
{
double newX = this->x + v.x;
double newY = this->y + v.y;
double newZ = this->z + v.z;
return Vector3D(newX, newY, newZ);
}
Vector3D Vector3D::operator-(const Vector3D &v) const
{
double newX = this->x - v.x;
double newY = this->y - v.y;
double newZ = this->z - v.z;
return Vector3D(newX, newY, newZ);
}
Vector3D Vector3D::operator/(double number) const
{
double newX = this->x / number;
double newY = this->y / number;
double newZ = this->z / number;
return Vector3D(newX, newY, newZ);
}
Vector3D Vector3D::operator*(double number) const
{
double newX = this->x * number;
double newY = this->y * number;
double newZ = this->z * number;
return Vector3D(newX, newY, newZ);
}
Vector3D operator*(double number, const Vector3D &v)
{
double newX = v.x * number;
double newY = v.y * number;
double newZ = v.z * number;
return Vector3D(newX, newY, newZ);
}
double Vector3D::operator*(const Vector3D &v) const
{
return (this->x * v.x) + (this->y * v.y) + (this->z * v.z);
}
Vector3D Vector3D::operator^(const Vector3D &v) const
{
double newX = (this->y * v.z) - (v.y * this->z);
double newY = -(this->x * v.z) + (v.x * this->z);
double newZ = (this->x * v.y) - (v.x * this->y);
return Vector3D(newX, newY, newZ);
}
void Vector3D::show()
{
cout << "<" << x << ", " << y << ", " << z << ">" << endl;
}
Vector3D Vector3D::hat()
{
double module = sqrt(x * x + y * y + z * z);
this->x /= module;
this->y /= module;
this->z /= module;
return (*this);
}