-
Notifications
You must be signed in to change notification settings - Fork 0
/
MathStructs.cpp
150 lines (122 loc) · 2.53 KB
/
MathStructs.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <math.h>
#include "MathStructs.h"
Point::Point(double x, double y, double z)
: itsX(x), itsY(y), itsZ(z)
{}
Point::Point()
{
Point(0.0, 0.0, 0.0);
}
void Point::setX(double x)
{
itsX = x;
}
void Point::setY(double y)
{
itsY = y;
}
void Point::setZ(double z)
{
itsZ = z;
}
double Point::getX() const
{
return itsX;
}
double Point::getY() const
{
return itsY;
}
double Point::getZ() const
{
return itsZ;
}
Vector::Vector(double x, double y, double z)
: Point(x, y, z)
{}
Vector::Vector(Point lhs)
: Point(lhs)
{}
Vector::Vector()
: Point()
{}
double Vector::length() const
{
return sqrt(pow(itsX, 2) + pow(itsY, 2) + pow(itsZ, 2));
}
double Vector::inclination2D() const
{
return atan(getY() / getX()) + ((itsX < 0) ? PI : 0);
}
Vector Vector::add(Vector other) const
{
double x = this->getX() + other.getX();
double y = this->getY() + other.getY();
double z = this->getZ() + other.getZ();
return (Vector(x, y, z));
}
Vector Vector::sub(Vector other) const
{
double x = this->getX() - other.getX();
double y = this->getY() - other.getY();
double z = this->getZ() - other.getZ();
return (Vector(x, y, z));
}
Vector Vector::cross(Vector other) const
{
double x = this->getY() * other.getZ() - other.getY() * this->getZ();
double y = this->getZ() * other.getX() - other.getZ() * this->getX();
double z = this->getX() * other.getY() - other.getX() * this->getY();
return (Vector(x, y, z));
}
double Vector::dot(Vector other) const
{
return (getX() * other.getX()) + (getY() * other.getY()) + (getZ() * other.getZ());
}
Vector Vector::operator+(Vector other) const
{
return this->add(other);
}
Vector Vector::operator-(Vector other) const
{
return this->sub(other);
}
Vector Vector::operator*(double f) const
{
return Vector(itsX * f, itsY * f, itsZ * f);
}
Vector Vector::operator/(double f) const
{
return Vector(itsX / f, itsY / f, itsZ / f);
}
Vector Vector::operator+=(Vector other)
{
*this = *this + other;
return *this;
}
Vector Vector::operator-=(Vector other)
{
*this = *this - other;
return *this;
}
Vector &Vector::nullify()
{
if (fabs(getX()) < POS_ZERO)
itsX = 0;
if (fabs(getY()) < POS_ZERO)
itsY = 0;
if (fabs(getZ()) < POS_ZERO)
itsZ = 0;
return *this;
}
Vector Vector::unit() const
{
return (*this / this->length());
}
bool Vector::isNull() const
{
return (
(fabs(itsX) < POS_ZERO) &&
(fabs(itsY) < POS_ZERO) &&
(fabs(itsZ) < POS_ZERO));
}