-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector4.cpp
More file actions
88 lines (73 loc) · 1.62 KB
/
Vector4.cpp
File metadata and controls
88 lines (73 loc) · 1.62 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
#include "Vector4.h"
#include "Vector3.h"
#include <cassert>
#include <cmath>
namespace dae
{
Vector4::Vector4(float _x, float _y, float _z, float _w) : x(_x), y(_y), z(_z), w(_w) {}
Vector4::Vector4(const Vector3& v, float _w) : x(v.x), y(v.y), z(v.z), w(_w) {}
float Vector4::Magnitude() const
{
return sqrtf(x * x + y * y + z * z + w * w);
}
float Vector4::SqrMagnitude() const
{
return x * x + y * y + z * z + w * w;
}
float Vector4::Normalize()
{
const float m = Magnitude();
x /= m;
y /= m;
z /= m;
w /= m;
return m;
}
Vector4 Vector4::Normalized() const
{
const float m = Magnitude();
return { x / m, y / m, z / m, w / m };
}
float Vector4::Dot(const Vector4& v1, const Vector4& v2)
{
return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z + v1.w * v2.w;
}
#pragma region Operator Overloads
Vector4 Vector4::operator*(float scale) const
{
return { x * scale, y * scale, z * scale, w * scale };
}
Vector4 Vector4::operator+(const Vector4& v) const
{
return { x + v.x, y + v.y, z + v.z, w + v.w };
}
Vector4 Vector4::operator-(const Vector4& v) const
{
return { x - v.x, y - v.y, z - v.z, w - v.w };
}
Vector4& Vector4::operator+=(const Vector4& v)
{
x += v.x;
y += v.y;
z += v.z;
w += v.w;
return *this;
}
float& Vector4::operator[](int index)
{
assert(index <= 3 && index >= 0);
if (index == 0)return x;
if (index == 1)return y;
if (index == 2)return z;
return w;
}
float Vector4::operator[](int index) const
{
assert(index <= 3 && index >= 0);
if (index == 0)return x;
if (index == 1)return y;
if (index == 2)return z;
return w;
}
#pragma endregion
}