-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVector4.cpp
More file actions
66 lines (57 loc) · 1.17 KB
/
Vector4.cpp
File metadata and controls
66 lines (57 loc) · 1.17 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
#include <iostream>
#include "Vector4.h"
#include "Matrix4.h"
Vector4::Vector4(double x, double y, double z, double w) : x(x), y(y), z(z), w(w)
{
return;
}
Vector4::Vector4() : x(0), y(0), z(0), w(0)
{
return;
}
Vector4 Vector4::operator+(const Vector4 &rh)
{
Vector4 result(
x + rh.x,
y + rh.y,
z + rh.z,
w + rh.w);
return result;
}
Vector4 Vector4::operator-(const Vector4 &rh)
{
Vector4 result(
x - rh.x,
y - rh.y,
z - rh.z,
w - rh.w);
return result;
}
void Vector4::dehomogenize()
{
if (w == 0) return;
x /= w;
y /= w;
z /= w;
w = 1;
}
void Vector4::print(std::string prefix) const
{
std::cout << prefix << " <" << x << ", " << y << ", " << z << ", " << w << ">" << std::endl;
}
double Vector4::length()
{
return sqrt(x*x + y*y + z*z);
}
Vector4 Vector4::operator*(const Matrix4 &m)
{
return Vector4(
m.m[0][0] * x + m.m[0][1] * y + m.m[0][2] * z + m.m[0][3] * w,
m.m[1][0] * x + m.m[1][1] * y + m.m[1][2] * z + m.m[1][3] * w,
m.m[2][0] * x + m.m[2][1] * y + m.m[2][2] * z + m.m[2][3] * w,
m.m[3][0] * x + m.m[3][1] * y + m.m[3][2] * z + m.m[3][3] * w);
}
double Vector4::operator*(const Vector4 &v)
{
return x * v.x + y * v.y + z * v.z + w * v.w;
}