-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVector2D.h
399 lines (309 loc) · 9.66 KB
/
Vector2D.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
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#pragma once
//------------------------------------------------------------------------
// Vector2d - Author: Mat Buckland
//------------------------------------------------------------------------
#include <iostream>
#include <windows.h>
#include <limits>
const double MinDouble = (std::numeric_limits<double>::min)();
//compares two real numbers. Returns true if they are equal
inline bool isEqual(float a, float b)
{
if (fabs(a - b) < 1E-12)
return true;
return false;
}
inline bool isEqual(double a, double b)
{
if (fabs(a - b) < 1E-12)
return true;
return false;
}
struct Vector2D
{
double x;
double y;
Vector2D() :x(0.0), y(0.0) {}
Vector2D(double a, double b) :x(a), y(b) {}
//sets x and y to zero
void Zero() { x = 0.0; y = 0.0; }
//returns true if both x and y are zero
bool isZero()const { return (x * x + y * y) < MinDouble; }
//returns the length of the vector
inline double Length()const;
//returns the squared length of the vector (thereby avoiding the sqrt)
inline double LengthSq()const;
inline void Normalize();
inline double Dot(const Vector2D& v2)const;
//returns positive if v2 is clockwise of this vector,
//negative if anticlockwise (assuming the Y axis is pointing down,
//X axis to right like a Window app)
inline int Sign(const Vector2D& v2)const;
//returns the vector that is perpendicular to this one.
inline Vector2D Perp()const;
//adjusts x and y so that the length of the vector does not exceed max
inline void Truncate(double max);
//returns the distance between this vector and the one passed as a parameter
inline double Distance(const Vector2D& v2)const;
//squared version of above.
inline double DistanceSq(const Vector2D& v2)const;
inline void Reflect(const Vector2D& norm);
//returns the vector that is the reverse of this vector
inline Vector2D GetReverse()const;
//we need some overloaded operators
const Vector2D& operator+=(const Vector2D& rhs)
{
x += rhs.x;
y += rhs.y;
return *this;
}
const Vector2D& operator-=(const Vector2D& rhs)
{
x -= rhs.x;
y -= rhs.y;
return *this;
}
const Vector2D& operator*(const Vector2D& rhs)
{
x *= rhs.x;
y *= rhs.y;
return *this;
}
const Vector2D& operator*=(const Vector2D& rhs)
{
x *= rhs.x;
y *= rhs.y;
return *this;
}
const Vector2D& operator*=(const double& rhs)
{
x *= rhs;
y *= rhs;
return *this;
}
const Vector2D& operator/=(const double& rhs)
{
x /= rhs;
y /= rhs;
return *this;
}
bool operator==(const Vector2D& rhs)const
{
return (isEqual(x, rhs.x) && isEqual(y, rhs.y));
}
bool operator!=(const Vector2D& rhs)const
{
return (x != rhs.x) || (y != rhs.y);
}
};
//-----------------------------------------------------------------------some more operator overloads
inline Vector2D operator*(const Vector2D& lhs, double rhs);
inline Vector2D operator*(double lhs, const Vector2D& rhs);
inline Vector2D operator-(const Vector2D& lhs, const Vector2D& rhs);
inline Vector2D operator+(const Vector2D& lhs, const Vector2D& rhs);
inline Vector2D operator/(const Vector2D& lhs, double val);
std::ostream& operator<<(std::ostream& os, const Vector2D& rhs);
std::ifstream& operator>>(std::ifstream& is, Vector2D& lhs);
//------------------------------------------------------------------------member functions
//------------------------- Length ---------------------------------------
//
// returns the length of a 2D vector
//------------------------------------------------------------------------
inline double Vector2D::Length()const
{
return sqrt(x * x + y * y);
}
//------------------------- LengthSq -------------------------------------
//
// returns the squared length of a 2D vector
//------------------------------------------------------------------------
inline double Vector2D::LengthSq()const
{
return (x * x + y * y);
}
//------------------------- Vec2DDot -------------------------------------
//
// calculates the dot product
//------------------------------------------------------------------------
inline double Vector2D::Dot(const Vector2D& v2)const
{
return x * v2.x + y * v2.y;
}
//------------------------ Sign ------------------------------------------
//
// returns positive if v2 is clockwise of this vector,
// minus if anticlockwise (Y axis pointing down, X axis to right)
//------------------------------------------------------------------------
enum { clockwise = 1, anticlockwise = -1 };
inline int Vector2D::Sign(const Vector2D& v2)const
{
if (y * v2.x > x * v2.y)
{
return clockwise;
}
else
{
return anticlockwise;
}
}
//------------------------------ Perp ------------------------------------
//
// Returns a vector perpendicular to this vector
//------------------------------------------------------------------------
inline Vector2D Vector2D::Perp()const
{
return Vector2D(-y, x);
}
//------------------------------ Distance --------------------------------
//
// calculates the euclidean distance between two vectors
//------------------------------------------------------------------------
inline double Vector2D::Distance(const Vector2D& v2)const
{
double ySeparation = v2.y - y;
double xSeparation = v2.x - x;
return sqrt(ySeparation * ySeparation + xSeparation * xSeparation);
}
//------------------------------ DistanceSq ------------------------------
//
// calculates the euclidean distance squared between two vectors
//------------------------------------------------------------------------
inline double Vector2D::DistanceSq(const Vector2D& v2)const
{
double ySeparation = v2.y - y;
double xSeparation = v2.x - x;
return ySeparation * ySeparation + xSeparation * xSeparation;
}
//----------------------------- Truncate ---------------------------------
//
// truncates a vector so that its length does not exceed max
//------------------------------------------------------------------------
inline void Vector2D::Truncate(double max)
{
if (this->Length() > max)
{
this->Normalize();
*this *= max;
}
}
//--------------------------- Reflect ------------------------------------
//
// given a normalized vector this method reflects the vector it
// is operating upon. (like the path of a ball bouncing off a wall)
//------------------------------------------------------------------------
inline void Vector2D::Reflect(const Vector2D& norm)
{
*this += 2.0 * this->Dot(norm) * norm.GetReverse();
}
//----------------------- GetReverse ----------------------------------------
//
// returns the vector that is the reverse of this vector
//------------------------------------------------------------------------
inline Vector2D Vector2D::GetReverse()const
{
return Vector2D(-this->x, -this->y);
}
//------------------------- Normalize ------------------------------------
//
// normalizes a 2D Vector
//------------------------------------------------------------------------
inline void Vector2D::Normalize()
{
double vector_length = this->Length();
if (vector_length > std::numeric_limits<double>::epsilon())
{
this->x /= vector_length;
this->y /= vector_length;
}
}
//------------------------------------------------------------------------non member functions
inline Vector2D Vec2DNormalize(const Vector2D& v)
{
Vector2D vec = v;
double vector_length = vec.Length();
if (vector_length > std::numeric_limits<double>::epsilon())
{
vec.x /= vector_length;
vec.y /= vector_length;
}
return vec;
}
inline double Vec2DDistance(const Vector2D& v1, const Vector2D& v2)
{
double ySeparation = v2.y - v1.y;
double xSeparation = v2.x - v1.x;
return sqrt(ySeparation * ySeparation + xSeparation * xSeparation);
}
inline double Vec2DDistanceSq(const Vector2D& v1, const Vector2D& v2)
{
double ySeparation = v2.y - v1.y;
double xSeparation = v2.x - v1.x;
return ySeparation * ySeparation + xSeparation * xSeparation;
}
inline double Vec2DLength(const Vector2D& v)
{
return sqrt(v.x * v.x + v.y * v.y);
}
inline double Vec2DLengthSq(const Vector2D& v)
{
return (v.x * v.x + v.y * v.y);
}
inline Vector2D POINTStoVector(const POINTS& p)
{
return Vector2D(p.x, p.y);
}
inline Vector2D POINTtoVector(const POINT& p)
{
return Vector2D((double)p.x, (double)p.y);
}
inline POINTS VectorToPOINTS(const Vector2D& v)
{
POINTS p;
p.x = (short)v.x;
p.y = (short)v.y;
return p;
}
inline POINT VectorToPOINT(const Vector2D& v)
{
POINT p;
p.x = (long)v.x;
p.y = (long)v.y;
return p;
}
//------------------------------------------------------------------------operator overloads
inline Vector2D operator*(const Vector2D& lhs, double rhs)
{
Vector2D result(lhs);
result *= rhs;
return result;
}
inline Vector2D operator*(double lhs, const Vector2D& rhs)
{
Vector2D result(rhs);
result *= lhs;
return result;
}
//overload the - operator
inline Vector2D operator-(const Vector2D& lhs, const Vector2D& rhs)
{
Vector2D result(lhs);
result.x -= rhs.x;
result.y -= rhs.y;
return result;
}
//overload the + operator
inline Vector2D operator+(const Vector2D& lhs, const Vector2D& rhs)
{
Vector2D result(lhs);
result.x += rhs.x;
result.y += rhs.y;
return result;
}
//overload the / operator
inline Vector2D operator/(const Vector2D& lhs, double val)
{
Vector2D result(lhs);
result.x /= val;
result.y /= val;
return result;
}