forked from tharvik/COG
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVvector.cpp
153 lines (126 loc) · 2.33 KB
/
Vvector.cpp
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
143
144
145
146
147
148
149
150
151
152
153
#include "Vvector.h"
#include "config.h"
#include <assert.h>
#define vx scalar[0]
#define vy scalar[1]
#define vz scalar[2]
// Constructors
Vvector::Vvector() : scalar()
{
setNull();
}
Vvector::Vvector(const float _x, const float _y, const float _z) : scalar()
{
vx = _x;
vy = _y;
vz = _z;
}
void Vvector::set(const float _x, const float _y, const float _z)
{
vx = _x;
vy = _y;
vz = _z;
}
void Vvector::setNull()
{
vx = 0;
vy = 0;
vz = 0;
}
float Vvector::length() const
{
return float(sqrt(vx*vx + vy*vy + vz*vz));
}
void Vvector::normalize()
{
float len = this->length();
vx /= len;
vy /= len;
vz /= len;
}
void Vvector::print() const
{
std::cout << "Vvector (" << vx << ';' << vy << ';' << vz
<< ") length: " << this->length() << std::endl;
}
Vvector Vvector::operator+(const Vvector &a) const
{
return Vvector(vx + a.vx, vy + a.vy, vz + a.vz);
}
Vvector Vvector::operator-(const Vvector &a) const
{
return Vvector(vx - a.vx, vy - a.vy, vz - a.vz);
}
Vvector Vvector::operator^(const Vvector &a) const
{
return Vvector(vy*a.vz - vz*a.vy,
vz*a.vx - vx*a.vz,
vx*a.vy - vy*a.vx);
}
Vvector Vvector::operator*(const float a) const
{
return Vvector(vx * a, vy * a, vz * a);
}
void Vvector::operator+=(const Vvector &a)
{
this->vx += a.vx;
this->vy += a.vy;
this->vz += a.vz;
}
void Vvector::operator-=(const Vvector &a)
{
this->vx -= a.vx;
this->vy -= a.vy;
this->vz -= a.vz;
}
void Vvector::operator^=(const Vvector &a) // doesn't work?!
{
this->vx = vy*a.vz - vz*a.vy;
this->vy = vz*a.vx - vx*a.vz;
this->vz = vx*a.vy - vy*a.vx;
}
void Vvector::operator*=(const float a)
{
this->vx *= a;
this->vy *= a;
this->vz *= a;
}
double Vvector::operator*(const Vvector &a) const
{
return vx*a.vx + vy*a.vy + vz*a.vz;
}
bool Vvector::operator==(const Vvector &a) const
{
return fabsf(vx - a.vx) < DELTA &&
fabsf(vy - a.vy) < DELTA &&
fabsf(vz - a.vz) < DELTA;
}
bool Vvector::operator!=(const Vvector &a) const
{
return !(*this == a);
}
float& Vvector::operator[](const unsigned short a)
{
assert(a < this->scalar.size());
return this->scalar[a];
}
float Vvector::x() const
{
return this->vx;
}
float Vvector::y() const
{
return this->vy;
}
float Vvector::z() const
{
return this->vz;
}
std::array<float, 3> Vvector::scalars() const
{
return this->scalar;
}
#undef vx
#undef vy
#undef vz
#undef DELTA