-
Notifications
You must be signed in to change notification settings - Fork 56
/
Vector2D.cpp
338 lines (275 loc) · 7.35 KB
/
Vector2D.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#include <cmath>
#include "Vector2D.h"
#include <stdlib.h>
Vector2D::Vector2D(void)
{
}
Vector2D::Vector2D(vec_t X, vec_t Y)
{
x = X; y = Y;
}
Vector2D::Vector2D(vec_t* clr)
{
x = clr[0]; y = clr[1];
}
//-----------------------------------------------------------------------------
// initialization
//-----------------------------------------------------------------------------
void Vector2D::Init(vec_t ix, vec_t iy)
{
x = ix; y = iy;
}
void Vector2D::Random(float minVal, float maxVal)
{
x = minVal + ((float)rand() / RAND_MAX) * (maxVal - minVal);
y = minVal + ((float)rand() / RAND_MAX) * (maxVal - minVal);
}
void Vector2DClear(Vector2D& a)
{
a.x = a.y = 0.0f;
}
//-----------------------------------------------------------------------------
// assignment
//-----------------------------------------------------------------------------
Vector2D& Vector2D::operator=(const Vector2D& vOther)
{
x = vOther.x; y = vOther.y;
return *this;
}
//-----------------------------------------------------------------------------
// Array access
//-----------------------------------------------------------------------------
vec_t& Vector2D::operator[](int i)
{
return ((vec_t*)this)[i];
}
vec_t Vector2D::operator[](int i) const
{
return ((vec_t*)this)[i];
}
//-----------------------------------------------------------------------------
// Base address...
//-----------------------------------------------------------------------------
vec_t* Vector2D::Base()
{
return (vec_t*)this;
}
vec_t const* Vector2D::Base() const
{
return (vec_t const*)this;
}
//-----------------------------------------------------------------------------
// IsValid?
//-----------------------------------------------------------------------------
bool Vector2D::IsValid() const
{
return !isinf(x) && !isinf(y);
}
//-----------------------------------------------------------------------------
// comparison
//-----------------------------------------------------------------------------
bool Vector2D::operator==(const Vector2D& src) const
{
return (src.x == x) && (src.y == y);
}
bool Vector2D::operator!=(const Vector2D& src) const
{
return (src.x != x) || (src.y != y);
}
//-----------------------------------------------------------------------------
// Copy
//-----------------------------------------------------------------------------
void Vector2DCopy(const Vector2D& src, Vector2D& dst)
{
dst.x = src.x;
dst.y = src.y;
}
void Vector2D::CopyToArray(float* rgfl) const
{
rgfl[0] = x; rgfl[1] = y;
}
//-----------------------------------------------------------------------------
// standard Math operations
//-----------------------------------------------------------------------------
void Vector2D::Negate()
{
x = -x; y = -y;
}
void Vector2DAdd(const Vector2D& a, const Vector2D& b, Vector2D& c)
{
c.x = a.x + b.x;
c.y = a.y + b.y;
}
void Vector2DSubtract(const Vector2D& a, const Vector2D& b, Vector2D& c)
{
c.x = a.x - b.x;
c.y = a.y - b.y;
}
void Vector2DMultiply(const Vector2D& a, vec_t b, Vector2D& c)
{
c.x = a.x * b;
c.y = a.y * b;
}
void Vector2DMultiply(const Vector2D& a, const Vector2D& b, Vector2D& c)
{
c.x = a.x * b.x;
c.y = a.y * b.y;
}
void Vector2DDivide(const Vector2D& a, vec_t b, Vector2D& c)
{
vec_t oob = 1.0f / b;
c.x = a.x * oob;
c.y = a.y * oob;
}
void Vector2DDivide(const Vector2D& a, const Vector2D& b, Vector2D& c)
{
c.x = a.x / b.x;
c.y = a.y / b.y;
}
void Vector2DMA(const Vector2D& start, float s, const Vector2D& dir, Vector2D& result)
{
result.x = start.x + s * dir.x;
result.y = start.y + s * dir.y;
}
// FIXME: Remove
// For backwards compatability
void Vector2D::MulAdd(const Vector2D& a, const Vector2D& b, float scalar)
{
x = a.x + b.x * scalar;
y = a.y + b.y * scalar;
}
void Vector2DLerp(const Vector2D& src1, const Vector2D& src2, vec_t t, Vector2D& dest)
{
dest[0] = src1[0] + (src2[0] - src1[0]) * t;
dest[1] = src1[1] + (src2[1] - src1[1]) * t;
}
//-----------------------------------------------------------------------------
// dot, cross
//-----------------------------------------------------------------------------
vec_t DotProduct2D(const Vector2D& a, const Vector2D& b)
{
return(a.x * b.x + a.y * b.y);
}
// for backwards compatability
vec_t Vector2D::Dot(const Vector2D& vOther) const
{
return DotProduct2D(*this, vOther);
}
vec_t Vector2DNormalize(Vector2D& v)
{
vec_t l = v.Length();
if (l != 0.0f) {
v /= l;
}
else {
v.x = v.y = 0.0f;
}
return l;
}
//-----------------------------------------------------------------------------
// length
//-----------------------------------------------------------------------------
vec_t Vector2DLength(const Vector2D& v)
{
return (vec_t)sqrt(v.x * v.x + v.y * v.y);
}
vec_t Vector2D::NormalizeInPlace()
{
return Vector2DNormalize(*this);
}
bool Vector2D::IsLengthGreaterThan(float val) const
{
return LengthSqr() > val * val;
}
bool Vector2D::IsLengthLessThan(float val) const
{
return LengthSqr() < val * val;
}
vec_t Vector2D::Length(void) const
{
return Vector2DLength(*this);
}
void Vector2DMin(const Vector2D& a, const Vector2D& b, Vector2D& result)
{
result.x = (a.x < b.x) ? a.x : b.x;
result.y = (a.y < b.y) ? a.y : b.y;
}
void Vector2DMax(const Vector2D& a, const Vector2D& b, Vector2D& result)
{
result.x = (a.x > b.x) ? a.x : b.x;
result.y = (a.y > b.y) ? a.y : b.y;
}
//-----------------------------------------------------------------------------
// Computes the closest point to vecTarget no farther than flMaxDist from vecStart
//-----------------------------------------------------------------------------
void ComputeClosestPoint2D(const Vector2D& vecStart, float flMaxDist, const Vector2D& vecTarget, Vector2D* pResult)
{
Vector2D vecDelta;
Vector2DSubtract(vecTarget, vecStart, vecDelta);
float flDistSqr = vecDelta.LengthSqr();
if (flDistSqr <= flMaxDist * flMaxDist) {
*pResult = vecTarget;
}
else {
vecDelta /= sqrt(flDistSqr);
Vector2DMA(vecStart, flMaxDist, vecDelta, *pResult);
}
}
//-----------------------------------------------------------------------------
// Returns a Vector2D with the min or max in X, Y, and Z.
//-----------------------------------------------------------------------------
Vector2D Vector2D::Min(const Vector2D& vOther) const
{
return Vector2D(x < vOther.x ? x : vOther.x, y < vOther.y ? y : vOther.y);
}
Vector2D Vector2D::Max(const Vector2D& vOther) const
{
return Vector2D(x > vOther.x ? x : vOther.x, y > vOther.y ? y : vOther.y);
}
//-----------------------------------------------------------------------------
// arithmetic operations
//-----------------------------------------------------------------------------
Vector2D Vector2D::operator-(void) const
{
return Vector2D(-x, -y);
}
Vector2D Vector2D::operator+(const Vector2D& v) const
{
Vector2D res;
Vector2DAdd(*this, v, res);
return res;
}
Vector2D Vector2D::operator-(const Vector2D& v) const
{
Vector2D res;
Vector2DSubtract(*this, v, res);
return res;
}
Vector2D Vector2D::operator*(float fl) const
{
Vector2D res;
Vector2DMultiply(*this, fl, res);
return res;
}
Vector2D Vector2D::operator*(const Vector2D& v) const
{
Vector2D res;
Vector2DMultiply(*this, v, res);
return res;
}
Vector2D Vector2D::operator/(float fl) const
{
Vector2D res;
Vector2DDivide(*this, fl, res);
return res;
}
Vector2D Vector2D::operator/(const Vector2D& v) const
{
Vector2D res;
Vector2DDivide(*this, v, res);
return res;
}
Vector2D operator*(float fl, const Vector2D& v)
{
return v * fl;
}