-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector.cpp
More file actions
122 lines (99 loc) · 2.32 KB
/
Vector.cpp
File metadata and controls
122 lines (99 loc) · 2.32 KB
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
114
115
116
117
118
119
120
121
122
#include "Vector.hpp"
Vector::Vector( void ) : _x(0), _y(0), _z(0)
{
return ;
}
Vector::Vector( Vertex const & dest ) : _x(dest.getX()), _y(dest.getY()), _z(dest.getZ())
{
return ;
}
Vector::Vector( Vertex const & ori, Vertex const & dest) : _x(dest.getX() - ori.getX()), _y(dest.getY() - ori.getY()), _z(dest.getZ() - ori.getZ())
{
return ;
}
Vector::Vector( double const x, double const y, double const z ) : _x(x), _y(y), _z(z)
{
return ;
}
Vector::Vector(Vector const & src)
{
*this = src;
return ;
}
Vector::~Vector( void )
{
return ;
}
Vector Vector::operator-() const
{
return Vector(-this->_x, -this->_y, -this->_z);
}
Vector Vector::operator-(Vector const & rhs) const
{
return Vector(this->_x - rhs._x, this->_y - rhs._y, this->_z - rhs._z);
}
Vector Vector::operator+(Vector const & rhs) const
{
return Vector(this->_x + rhs._x, this->_y + rhs._y, this->_z + rhs._z);
}
double Vector::operator*(Vector const & rhs) const
{
return (this->_x * rhs._x + this->_y * rhs._y + this->_z * rhs._z);
}
Vector Vector::operator*(double const k) const
{
return Vector(this->_x * k, this->_y * k, this->_z * k);
}
Vector Vector::operator^(Vector const & rhs) const
{
double x = (this->_y * rhs._z) - (this->_z * rhs._y);
double y = (this->_z * rhs._x) - (this->_x * rhs._z);
double z = (this->_x * rhs._y) - (this->_y * rhs._x);
return Vector(x, y, z);
}
Vector & Vector::operator=(Vector const & rhs)
{
this->_x = rhs._x;
this->_y = rhs._y;
this->_z = rhs._z;
return *this;
}
double Vector::getX() const
{
return this->_x;
}
double Vector::getY() const
{
return this->_y;
}
double Vector::getZ() const
{
return this->_z;
}
double Vector::magnitude() const
{
return std::sqrt(std::pow(this->_x, 2) + std::pow(this->_y, 2) + std::pow(this->_z, 2));
}
Vector Vector::normalize() const
{
double norm = this->magnitude();
double x = this->_x / norm;
double y = this->_y / norm;
double z = this->_z / norm;
return Vector(x, y, z);
}
double Vector::cos(Vector const & rhs) const
{
return (*this * rhs / (this->magnitude() * rhs.magnitude()));
}
std::string Vector::toString() const
{
std::stringstream sstr;
sstr << "[Vector : {" << this->_x << ", " << this->_y << ", " << this->_z << "}]";
return sstr.str();
}
std::ostream & operator<<(std::ostream & o, Vector const & rhs)
{
o << rhs.toString();
return o;
}