-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector3.h
More file actions
88 lines (84 loc) · 1.77 KB
/
vector3.h
File metadata and controls
88 lines (84 loc) · 1.77 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
#pragma once
#include <cmath>
class Vector3
{
public:
float x, y, z;
Vector3() : x(0), y(0), z(0) {}
Vector3(float x, float y, float z) : x(x), y(y), z(z) {}
Vector3 operator+(const Vector3& other) const
{
return Vector3(x + other.x, y + other.y, z + other.z);
}
Vector3 operator-(const Vector3& other) const
{
return Vector3(x - other.x, y - other.y, z - other.z);
}
Vector3 operator*(float scalar) const
{
return Vector3(x * scalar, y * scalar, z * scalar);
}
Vector3 operator/(float scalar) const
{
return Vector3(x / scalar, y / scalar, z / scalar);
}
Vector3& operator+=(const Vector3& other)
{
x += other.x;
y += other.y;
z += other.z;
return *this;
}
Vector3& operator-=(const Vector3& other)
{
x -= other.x;
y -= other.y;
z -= other.z;
return *this;
}
Vector3& operator*=(float scalar)
{
x *= scalar;
y *= scalar;
z *= scalar;
return *this;
}
Vector3& operator/=(float scalar)
{
x /= scalar;
y /= scalar;
z /= scalar;
return *this;
}
bool operator==(const Vector3& other) const
{
return x == other.x && y == other.y && z == other.z;
}
bool operator!=(const Vector3& other) const
{
return x != other.x || y != other.y || z != other.z;
}
float Dot(const Vector3& other) const
{
return x * other.x + y * other.y + z * other.z;
}
Vector3 Cross(const Vector3& other) const
{
return Vector3(y * other.z - z * other.y, z * other.x - x * other.z, x * other.y - y * other.x);
}
float Length() const
{
return sqrt(x * x + y * y + z * z);
}
float Distance(Vector3& other) const {
return sqrt(pow(other.x - x, 2) + pow(other.y - y, 2) + pow(other.z - z, 2));
}
void Normalize() {
float length = std::sqrt(x * x + y * y + z * z);
if (length != 0.0f) {
x /= length;
y /= length;
z /= length;
}
}
};