-
Notifications
You must be signed in to change notification settings - Fork 0
/
Vector3D.h
82 lines (56 loc) · 1.56 KB
/
Vector3D.h
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
#pragma once
class Vector3D
{
public:
Vector3D(): m_x(0), m_y(0), m_z(0)
{
}
Vector3D(float x, float y, float z) : m_x(x), m_y(y), m_z(z)
{
}
Vector3D(const Vector3D& vector) : m_x(vector.m_x), m_y(vector.m_y), m_z(vector.m_z)
{
}
static Vector3D lerp(const Vector3D& start, const Vector3D& end, float delta)
{
Vector3D v;
v.m_x = start.m_x * (1.0f - delta) + end.m_x * (delta);
v.m_y = start.m_y * (1.0f - delta) + end.m_y * (delta);
v.m_z = start.m_z * (1.0f - delta) + end.m_z * (delta);
return v;
}
Vector3D operator *(float num) const
{
return Vector3D(m_x*num,m_y*num,m_z*num);
}
Vector3D operator +(const Vector3D& vec) const
{
return Vector3D(m_x + vec.m_x, m_y + vec.m_y, m_z + vec.m_z);
}
Vector3D operator -(const Vector3D& vec) const
{
return Vector3D(m_x - vec.m_x, m_y - vec.m_y, m_z - vec.m_z);
}
static Vector3D normalize(const Vector3D& vec)
{
Vector3D res{};
double len = sqrt(float(float(vec.m_x) * float(vec.m_x) + float(vec.m_y) * float(vec.m_y) + float(vec.m_z) * float(vec.m_z)));
if (!len) return Vector3D();
res.m_x = float(vec.m_x / len);
res.m_y = float(vec.m_y / len);
res.m_z = float(vec.m_z / len);
return res;
}
static Vector3D cross(const Vector3D& v1, const Vector3D& v2)
{
Vector3D res;
res.m_x = (v1.m_y * v2.m_z) - (v1.m_z * v2.m_y);
res.m_y = (v1.m_z * v2.m_x) - (v1.m_x * v2.m_z);
res.m_z = (v1.m_x * v2.m_y) - (v1.m_y * v2.m_x);
return res;
}
~Vector3D()
{
}
float m_x, m_y, m_z;
};