-
Notifications
You must be signed in to change notification settings - Fork 0
/
tvector4d.h
142 lines (122 loc) · 3.65 KB
/
tvector4d.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
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
138
139
140
141
142
#ifndef TVECTOR4D_H
#define TVECTOR4D_H
#include "tpoint.h"
#include "tvector3d.h"
#include <cmath>
class TVector4D {
public:
float x, y, z, w;
// Constructores
TVector4D();
TVector4D(float x, float y, float z);
TVector4D(float x, float y, float z, float w);
explicit TVector4D(const TVector3D &vector);
explicit TVector4D(const TVector3D &vector, float w);
explicit TVector4D(const TPoint &p);
// Métodos
float length() const;
float lengthSquared() const;
float dotProduct(const TVector4D &v1, const TVector4D &v2) const;
bool isCeroVector() const;
TVector4D normalize() const;
TPoint toPoint() const;
TVector3D toVector3D() const;
TVector3D toVector3DAffine() const;
// Operadores:
float &operator[](std::size_t i);
float operator[](std::size_t i) const;
TVector4D &operator+=(const TVector4D &vector);
TVector4D &operator-=(const TVector4D &vector);
TVector4D &operator*=(float m);
TVector4D &operator/=(float m);
friend inline bool operator==(const TVector4D &v1, const TVector4D &v2);
friend inline bool operator!=(const TVector4D &v1, const TVector4D &v2);
friend inline const TVector4D operator+(const TVector4D &v1,
const TVector4D &v2);
friend inline const TVector4D operator-(const TVector4D &v1,
const TVector4D &v2);
friend inline const TVector4D operator*(float m, const TVector4D &vector);
friend inline const TVector4D operator*(const TVector4D &vector, float m);
friend inline const TVector4D operator-(const TVector4D &vector);
friend inline const TVector4D operator/(const TVector4D &vector, float m);
private:
friend class TVector3D;
friend class TPoint;
};
inline float &TVector4D::operator[](std::size_t i) {
switch (i) {
case 0:
return x;
case 1:
return y;
case 2:
return z;
case 3:
return w;
}
}
inline float TVector4D::operator[](std::size_t i) const {
switch (i) {
case 0:
return x;
case 1:
return y;
case 2:
return z;
case 3:
return w;
}
}
inline TVector4D &TVector4D::operator+=(const TVector4D &vector) {
x += vector.x;
y += vector.y;
z += vector.z;
w += vector.w;
return *this;
}
inline TVector4D &TVector4D::operator-=(const TVector4D &vector) {
x -= vector.x;
y -= vector.y;
z -= vector.z;
w -= vector.w;
return *this;
}
inline TVector4D &TVector4D::operator*=(float m) {
x *= m;
y *= m;
z *= m;
w *= m;
return *this;
}
inline TVector4D &TVector4D::operator/=(float m) {
x /= m;
y /= m;
z /= m;
w /= m;
return *this;
}
inline bool operator==(const TVector4D &v1, const TVector4D &v2) {
return v1.x == v2.x && v1.y == v2.y && v1.z == v2.z && v1.w == v2.w;
}
inline bool operator!=(const TVector4D &v1, const TVector4D &v2) {
return !(v1 == v2);
}
inline const TVector4D operator+(const TVector4D &v1, const TVector4D &v2) {
return TVector4D(v1.x + v2.x, v1.y + v2.y, v1.z + v2.z, v1.w + v2.w);
}
inline const TVector4D operator-(const TVector4D &v1, const TVector4D &v2) {
return TVector4D(v1.x - v2.x, v1.y - v2.y, v1.z - v2.z, v1.w - v2.w);
}
inline const TVector4D operator*(float m, const TVector4D &vector) {
return TVector4D(vector.x * m, vector.y * m, vector.z * m, vector.w * m);
}
inline const TVector4D operator*(const TVector4D &vector, float m) {
return TVector4D(vector.x * m, vector.y * m, vector.z * m, vector.w * m);
}
inline const TVector4D operator-(const TVector4D &vector) {
return TVector4D(-vector.x, -vector.y, -vector.z, -vector.w);
}
inline const TVector4D operator/(const TVector4D &vector, float m) {
return TVector4D(vector.x / m, vector.y / m, vector.z / m, vector.w / m);
}
#endif // TVECTOR4D_H