forked from jcbritobr/sray
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector3.cpp
executable file
·103 lines (82 loc) · 2.6 KB
/
vector3.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
#include <cmath>
#include "vector3.hpp"
float Vector3::x() const
{
return std::get<0>(data);
}
float Vector3::y() const
{
return std::get<1>(data);
}
float Vector3::z() const
{
return std::get<2>(data);
}
float Vector3::length() const
{
float dx = std::get<0>(data) * std::get<0>(data);
float dy = std::get<1>(data) * std::get<1>(data);
float dz = std::get<2>(data) * std::get<2>(data);
return sqrt(dx + dy + dz);
}
float Vector3::squaredLenght() const
{
float dx = std::get<0>(data) * std::get<0>(data);
float dy = std::get<1>(data) * std::get<1>(data);
float dz = std::get<2>(data) * std::get<2>(data);
return dx + dy + dz;
}
Vector3 Vector3::cross(const Vector3 &vectorA, const Vector3 &vectorB)
{
return Vector3((vectorA.y() * vectorB.z()) - (vectorA.z() * vectorB.y()),
(-(vectorA.x() * vectorB.z()) - (vectorA.z() * vectorB.x())),
(vectorA.x() * vectorB.y()) - (vectorA.y() * vectorB.x()));
}
Vector3 Vector3::makeUnitVector(const Vector3 &vector)
{
return vector / vector.length();
}
float Vector3::dot(const Vector3 &vectorA, const Vector3 &vectorB)
{
return vectorA.x() * vectorB.x() + vectorA.y() * vectorB.y() + vectorA.z() * vectorB.z();
}
Vector3 Vector3::operator-() const
{
return Vector3(-std::get<0>(data), -std::get<1>(data), -std::get<2>(data));
}
Vector3::Vector3()
{}
Vector3::Vector3(const float x, const float y, const float z)
{
std::get<0>(data) = x;
std::get<1>(data) = y;
std::get<2>(data) = z;
}
Vector3 operator +(const Vector3 &vector, const Vector3 &another)
{
return Vector3(vector.x() + another.x(), vector.y() + another.y(), vector.z() + another.z());
}
Vector3 operator -(const Vector3 &vector, const Vector3 &another)
{
return Vector3(vector.x() - another.x(), vector.y() - another.y(), vector.z() - another.z());
}
Vector3 operator *(const Vector3 &vector, const Vector3 &another)
{
return Vector3(vector.x() * another.x(), vector.y() * another.y(), vector.z() * another.z());
}
Vector3 operator /(const Vector3 &vector, const Vector3 &another)
{
return Vector3(vector.x() / another.x(), vector.y() / another.y(), vector.z() / another.z());
}
Vector3 operator /(const Vector3 &vector, float scalar)
{
return Vector3(vector.x() / scalar, vector.y() / scalar, vector.z() / scalar);
}
Vector3 operator *(const Vector3 &vector, float scalar)
{
return Vector3(vector.x() * scalar, vector.y() * scalar, vector.z() * scalar);
}
Vector3 operator *(float scalar, const Vector3 &vector)
{
return Vector3(scalar * vector.x(), scalar * vector.y(), scalar * vector.z());
}