-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathVector3.cpp
137 lines (113 loc) · 2.92 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
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
#include "Vector3.h"
#include "Vector2.h"
#include <math.h>
namespace Engine
{
Vector3::Vector3(void)
: X(0.0f), Y(0.0f), Z(0.0f)
{ }
Vector3::Vector3(float x, float y, float z)
: X(x), Y(y), Z(z)
{ }
Vector3::Vector3(const Vector2 &vector2)
: X(vector2.X), Y(vector2.Y), Z(0.0f)
{ }
float Vector3::GetLength(void) const
{
return sqrt(X * X + Y * Y + Z * Z);
}
float Vector3::GetLengthSquared(void) const
{
return (X * X + Y * Y + Z * Z);
}
void Vector3::Normalize(void)
{
float length = this->GetLength();
this->X /= length;
this->Y /= length;
this->Z /= length;
}
Vector3 Vector3::operator+(const Vector3 &other) const
{
return Vector3(this->X + other.X,
this->Y + other.Y,
this->Z + other.Z);
}
Vector3 Vector3::operator+=(const Vector3 &other)
{
this->X += other.X;
this->Y += other.Y;
this->Z += other.Z;
return Vector3(*this);
}
Vector3 Vector3::operator-(const Vector3 &other) const
{
return Vector3(this->X - other.X,
this->Y - other.Y,
this->Z - other.Z);
}
Vector3 Vector3::operator-=(const Vector3 &other)
{
this->X -= other.X;
this->Y -= other.Y;
this->Z -= other.Z;
return Vector3(*this);
}
Vector3 Vector3::operator*(float scalar) const
{
return Vector3(this->X * scalar,
this->Y * scalar,
this->Z * scalar);
}
Vector3 Vector3::operator*=(float scalar)
{
this->X *= scalar;
this->Y *= scalar;
this->Z *= scalar;
return Vector3(*this);
}
float Vector3::operator*(const Vector3 &other) const
{
return (this->X * other.X + this->Y * other.Y + this->Z * other.Z);
}
bool Vector3::operator<(const Vector3 &other) const
{
return (this->X < other.X) && (this->Y < other.Y) && (this->Z < other.Z);
}
bool Vector3::operator<=(const Vector3 &other) const
{
return (this->X <= other.X) && (this->Y <= other.Y) && (this->Z <= other.Z);
}
bool Vector3::operator>(const Vector3 &other) const
{
return (this->X > other.X) && (this->Y > other.Y) && (this->Z > other.Z);
}
bool Vector3::operator>=(const Vector3 &other) const
{
return (this->X >= other.X) && (this->Y >= other.Y) && (this->Z >= other.Z);
}
bool Vector3::operator==(const Vector3 &other) const
{
return (this->X == other.X) && (this->Y == other.Y) && (this->Z == other.Z);
}
bool Vector3::operator!=(const Vector3 &other) const
{
return (this->X != other.X) || (this->Y != other.Y) || (this->Z != other.Z);
}
Vector3 Vector3::Cross(const Vector3 &left, const Vector3 &right)
{
return Vector3(left.Y * right.Z - left.Z * right.Y,
left.Z * right.X - left.X * right.Z,
left.X * right.Y - left.Y * right.X);
}
float Vector3::Dot(const Vector3 &left, const Vector3 &right)
{
return left * right;
}
Vector3 Vector3::Normalize(const Vector3 &vector)
{
Vector3 result(vector);
result.Normalize();
return result;
}
}