-
Notifications
You must be signed in to change notification settings - Fork 32
/
vec.cpp
230 lines (191 loc) · 4.1 KB
/
vec.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include "vec.h"
#include <cmath>
#include <sstream>
// V V EEEEEEE CCC 222 FFFFFFF
// V V E CC CC 2 22 F
// V V E CC C 2 F
// V V E C 2 F
// V V EEEE C 2 FFFF
// V V E C 2 F
// V V E CC C 2 F
// VVV E CC CC 2 F
// V EEEEEEE CCC 2222222 F
// Quake <3
inline float fast_inv_sqrt(float x)
{
union {float f; unsigned long ul;} y;
y.f = x;
y.ul = (0xBE6EB50CUL - y.ul) >> 1;
y.f = 0.5f * y.f * (3.0f - x * y.f * y.f);
return y.f;
}
inline float fast_sqrt(float x)
{
union {float f; unsigned long ul;} y;
y.f = x;
y.ul = (0xBE6EB50CUL - y.ul) >> 1;
y.f = 0.5f * y.f * (3.0f - x * y.f * y.f);
return x * y.f;
}
vec2f vec2f::operator+(const vec2f &rhs) const
{
return vec2f(x + rhs.x,
y + rhs.y);
}
vec2f vec2f::operator-(const vec2f &rhs) const
{
return vec2f(x - rhs.x,
y - rhs.y);
}
vec2f vec2f::operator*(float rhs) const
{
return vec2f(x * rhs,
y * rhs);
}
vec2f vec2f::operator/(float rhs) const
{
return vec2f(x / rhs,
y / rhs);
}
vec2f& vec2f::operator+=(const vec2f &rhs)
{
x += rhs.x;
y += rhs.y;
return *this;
}
vec2f& vec2f::operator-=(const vec2f &rhs)
{
x -= rhs.x;
y -= rhs.y;
return *this;
}
vec2f& vec2f::operator*=(float rhs)
{
x *= rhs;
y *= rhs;
return *this;
}
vec2f& vec2f::operator/=(float rhs)
{
x /= rhs;
y /= rhs;
return *this;
}
bool vec2f::operator==(const vec2f &rhs) const
{
return x == rhs.x && y == rhs.y;
}
bool vec2f::operator<(const vec2f &rhs) const
{
return x < rhs.x || (x == rhs.x && y < rhs.y);
}
float vec2f::dot(const vec2f &rhs) const
{
return x * rhs.x + y * rhs.y;
}
float vec2f::length() const
{
return sqrtf(x * x + y * y);
}
vec2f vec2f::normalise() const
{
return *this * fast_inv_sqrt(x * x + y * y);
}
std::string vec2f::toString()
{
std::stringstream ss;
ss << "(" << x << ", " << y << ")";
return ss.str();
}
/*vec2f::vec2f(float _x, float _y)
{
x = _x;
y = _y;
}*/
// V V EEEEEEE CCC 333 FFFFFFF
// V V E CC CC 33 33 F
// V V E CC C 3 3 F
// V V E C 33 F
// V V EEEE C 33 FFFF
// V V E C 33 F
// V V E CC C 3 3 F
// VVV E CC CC 33 33 F
// V EEEEEEE CCC 333 F
vec3f vec3f::operator+(const vec3f &rhs) const
{
return vec3f(x + rhs.x,
y + rhs.y,
z + rhs.z);
}
vec3f vec3f::operator-(const vec3f &rhs) const
{
return vec3f(x - rhs.x,
y - rhs.y,
z - rhs.z);
}
vec3f vec3f::operator*(float rhs) const
{
return vec3f(x * rhs,
y * rhs,
z * rhs);
}
vec3f vec3f::operator/(float rhs) const
{
return vec3f(x / rhs,
y / rhs,
z / rhs);
}
vec3f& vec3f::operator+=(const vec3f &rhs)
{
x += rhs.x;
y += rhs.y;
z += rhs.z;
return *this;
}
vec3f& vec3f::operator-=(const vec3f &rhs)
{
x -= rhs.x;
y -= rhs.y;
z -= rhs.z;
return *this;
}
vec3f& vec3f::operator*=(float rhs)
{
x *= rhs;
y *= rhs;
z *= rhs;
return *this;
}
vec3f& vec3f::operator/=(float rhs)
{
x /= rhs;
y /= rhs;
y /= rhs;
return *this;
}
bool vec3f::operator==(const vec3f &rhs) const
{
return x == rhs.x && y == rhs.y && z == rhs.z;
}
bool vec3f::operator<(const vec3f &rhs) const
{
return x < rhs.x || (x == rhs.x && (y < rhs.y || (y == rhs.y && z < rhs.z)));
}
float vec3f::dot(const vec3f &rhs) const
{
return x * rhs.x + y * rhs.y + z * rhs.z;
}
float vec3f::length() const
{
return sqrtf(x * x + y * y + z * z);
}
vec3f vec3f::normalise() const
{
return *this * fast_inv_sqrt(x * x + y * y + z * z);
}
std::string vec3f::toString()
{
std::stringstream ss;
ss << "(" << x << ", " << y << ", " << z << ")";
return ss.str();
}