-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmath.cpp
More file actions
155 lines (126 loc) · 3.73 KB
/
Copy pathmath.cpp
File metadata and controls
155 lines (126 loc) · 3.73 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
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
151
152
153
154
155
#include "math.h"
#include <cmath>
#include <ostream>
Math::Vector3::Vector3()
{
this->x = 0;
this->y = 0;
this->z = 0;
};
Math::Vector3::Vector3(Vector3 const& v)
{
this->x = v.x;
this->y = v.y;
this->z = v.z;
}
Math::Vector3::Vector3(float x, float y, float z)
{
this->x = x;
this->y = y;
this->z = z;
};
float Math::Vector3::getX() const
{
return this->x;
};
float Math::Vector3::getY() const
{
return this->y;
};
float Math::Vector3::getZ() const
{
return this->z;
};
void Math::Vector3::setX(float f)
{
this->x = f;
};
void Math::Vector3::setY(float f)
{
this->y = f;
};
void Math::Vector3::setZ(float f)
{
this->z = f;
};
Math::Vector3& Math::Vector3::operator+=(Math::Vector3 const& rhs)
{
this->setX(this->getX() + rhs.getX());
this->setY(this->getY() + rhs.getY());
this->setZ(this->getZ() + rhs.getZ());
return *this;
};
Math::Vector3& Math::Vector3::operator-=(Math::Vector3 const& rhs)
{
this->setX(this->getX() - rhs.getX());
this->setY(this->getY() - rhs.getY());
this->setZ(this->getZ() - rhs.getZ());
return *this;
};
Math::Vector3 Math::Vector3::operator-() const
{
return { -this->getX(), -this->getY(), -this->getZ() };
};
float Math::Vector3::norm() const
{
return std::sqrt(this->x * this->x + this->y * this->y + this->z * this->z);
};
Math::Vector3 Math::operator+(Math::Vector3 const& lhs, Math::Vector3 const& rhs)
{
return { lhs.getX() + rhs.getX(), lhs.getY() + rhs.getY(), lhs.getZ() + rhs.getZ() };
};
Math::Vector3 Math::operator-(Math::Vector3 const& lhs, Math::Vector3 const& rhs)
{
return { lhs.getX() - rhs.getX(), lhs.getY() - rhs.getY(), lhs.getZ() - rhs.getZ() };
};
bool Math::operator==(Math::Vector3 const& lhs, Math::Vector3 const& rhs)
{
return (lhs.getX() == rhs.getX() && lhs.getY() == rhs.getY() && lhs.getZ() == rhs.getZ());
};
bool Math::operator!=(Math::Vector3 const& lhs, Math::Vector3 const& rhs)
{
return (lhs.getX() != rhs.getX() || lhs.getY() != rhs.getY() || lhs.getZ() != rhs.getZ());
};
Math::Vector3 Math::operator*(float const& lhs, Math::Vector3 const& rhs)
{
return { lhs * rhs.getX(), lhs * rhs.getY(), lhs * rhs.getZ() };
};
Math::Vector3 Math::operator*(Math::Vector3 const& lhs, float const& rhs)
{
return { lhs.getX() * rhs, lhs.getY() * rhs, lhs.getZ() * rhs };
};
Math::Vector3 Math::operator/(Math::Vector3 const& lhs, float const& rhs)
{
return { lhs.getX() / rhs, lhs.getY() / rhs, lhs.getZ() / rhs };
};
std::ostream& Math::operator<<(std::ostream &strm, const Math::Vector3 &v)
{
return strm << "x: " << v.getX() << " y: " << v.getY() << " z: " << v.getZ();
}
float Math::dot(Math::Vector3 const& lhs, Math::Vector3 const& rhs)
{
return lhs.getX() * rhs.getX() + lhs.getY() * rhs.getY() + lhs.getZ() * rhs.getZ();
}
Math::Vector3 Math::cross(Math::Vector3 const& lhs, Math::Vector3 const& rhs)
{
float x = lhs.getY() * rhs.getZ() - lhs.getZ() * rhs.getY();
float y = -(lhs.getX() * rhs.getZ() - lhs.getZ() * rhs.getX());
float z = lhs.getX() * rhs.getY() - lhs.getY() * rhs.getX();
return { x, y, z };
}
Math::Box::Box()
{
this->min = { 0, 0, 0 };
this->max = { 0, 0, 0 };
}
Math::Box::Box(Math::Vector3 u, Math::Vector3 v)
{
this->min = { std::min(u.getX(), v.getX()), std::min(u.getY(), v.getY()), std::min(u.getZ(), v.getZ()) };
this->max = { std::max(u.getX(), v.getX()), std::max(u.getY(), v.getY()), std::max(u.getZ(), v.getZ()) };
}
bool Math::Box::isInside(Math::Vector3 p) const
{
if (p.getX() <= this->min.getX() || p.getX() >= this->max.getX()) { return false; };
if (p.getY() <= this->min.getY() || p.getY() >= this->max.getY()) { return false; };
return p.getZ() > this->min.getZ() && p.getZ() < this->max.getZ();
}