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