-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathvec2d.hpp
697 lines (545 loc) · 15.6 KB
/
vec2d.hpp
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
// ================================================================================================
// -*- C++ -*-
// File: vectormath/vec2d.hpp
// Author: Guilherme R. Lampert
// Created on: 30/12/16
// Brief: 2D vector and point extensions to the original Vectormath library.
// ================================================================================================
#ifndef VECTORMATH_VEC2D_HPP
#define VECTORMATH_VEC2D_HPP
namespace Vectormath
{
class Vector2;
class Point2;
// ========================================================
// A 2-D unpadded vector (sizeof = 8 bytes)
// ========================================================
class Vector2
{
float mX;
float mY;
public:
// Default constructor; does no initialization
//
inline Vector2() { }
// Construct a 2-D vector from x and y elements
//
inline Vector2(float x, float y);
// Copy elements from a 2-D point into a 2-D vector
//
explicit inline Vector2(const Point2 & pnt);
// Set all elements of a 2-D vector to the same scalar value
//
explicit inline Vector2(float scalar);
// Set the x element of a 2-D vector
//
inline Vector2 & setX(float x);
// Set the y element of a 2-D vector
//
inline Vector2 & setY(float y);
// Get the x element of a 2-D vector
//
inline float getX() const;
// Get the y element of a 2-D vector
//
inline float getY() const;
// Set an x or y element of a 2-D vector by index
//
inline Vector2 & setElem(int idx, float value);
// Get an x or y element of a 2-D vector by index
//
inline float getElem(int idx) const;
// Subscripting operator to set or get an element
//
inline float & operator[](int idx);
// Subscripting operator to get an element
//
inline float operator[](int idx) const;
// Add two 2-D vectors
//
inline const Vector2 operator + (const Vector2 & vec) const;
// Subtract a 2-D vector from another 2-D vector
//
inline const Vector2 operator - (const Vector2 & vec) const;
// Add a 2-D vector to a 2-D point
//
inline const Point2 operator + (const Point2 & pnt) const;
// Multiply a 2-D vector by a scalar
//
inline const Vector2 operator * (float scalar) const;
// Divide a 2-D vector by a scalar
//
inline const Vector2 operator / (float scalar) const;
// Perform compound assignment and addition with a 2-D vector
//
inline Vector2 & operator += (const Vector2 & vec);
// Perform compound assignment and subtraction by a 2-D vector
//
inline Vector2 & operator -= (const Vector2 & vec);
// Perform compound assignment and multiplication by a scalar
//
inline Vector2 & operator *= (float scalar);
// Perform compound assignment and division by a scalar
//
inline Vector2 & operator /= (float scalar);
// Negate all elements of a 2-D vector
//
inline const Vector2 operator - () const;
// Construct x axis
//
static inline const Vector2 xAxis();
// Construct y axis
//
static inline const Vector2 yAxis();
};
// Multiply a 2-D vector by a scalar
//
inline const Vector2 operator * (float scalar, const Vector2 & vec);
// Compute the absolute value of a 2-D vector per element
//
inline const Vector2 absPerElem(const Vector2 & vec);
// Maximum of two 2-D vectors per element
//
inline const Vector2 maxPerElem(const Vector2 & vec0, const Vector2 & vec1);
// Minimum of two 2-D vectors per element
//
inline const Vector2 minPerElem(const Vector2 & vec0, const Vector2 & vec1);
// Maximum element of a 2-D vector
//
inline float maxElem(const Vector2 & vec);
// Minimum element of a 2-D vector
//
inline float minElem(const Vector2 & vec);
// Compute the dot product of two 2-D vectors
//
inline float dot(const Vector2 & vec0, const Vector2 & vec1);
// Compute the square of the length of a 2-D vector
//
inline float lengthSqr(const Vector2 & vec);
// Compute the length of a 2-D vector
//
inline float length(const Vector2 & vec);
// Normalize a 2-D vector
// NOTE:
// The result is unpredictable when all elements of vec are at or near zero.
//
inline const Vector2 normalize(const Vector2 & vec);
// Linear interpolation between two 2-D vectors
// NOTE:
// Does not clamp t between 0 and 1.
//
inline const Vector2 lerp(float t, const Vector2 & vec0, const Vector2 & vec1);
#ifdef VECTORMATH_DEBUG
// Print a 2-D vector
// NOTE:
// Function is only defined when VECTORMATH_DEBUG is defined.
//
inline void print(const Vector2 & vec);
// Print a 2-D vector and an associated string identifier
// NOTE:
// Function is only defined when VECTORMATH_DEBUG is defined.
//
inline void print(const Vector2 & vec, const char * name);
#endif // VECTORMATH_DEBUG
// ========================================================
// A 2-D unpadded point (sizeof = 8 bytes)
// ========================================================
class Point2
{
float mX;
float mY;
public:
// Default constructor; does no initialization
//
inline Point2() { }
// Construct a 2-D point from x and y elements
//
inline Point2(float x, float y);
// Copy elements from a 2-D vector into a 2-D point
//
explicit inline Point2(const Vector2 & vec);
// Set all elements of a 2-D point to the same scalar value
//
explicit inline Point2(float scalar);
// Set the x element of a 2-D point
//
inline Point2 & setX(float x);
// Set the y element of a 2-D point
//
inline Point2 & setY(float y);
// Get the x element of a 2-D point
//
inline float getX() const;
// Get the y element of a 2-D point
//
inline float getY() const;
// Set an x or y element of a 2-D point by index
//
inline Point2 & setElem(int idx, float value);
// Get an x or y element of a 2-D point by index
//
inline float getElem(int idx) const;
// Subscripting operator to set or get an element
//
inline float & operator[](int idx);
// Subscripting operator to get an element
//
inline float operator[](int idx) const;
// Subtract a 2-D point from another 2-D point
//
inline const Vector2 operator - (const Point2 & pnt) const;
// Add a 2-D point to a 2-D vector
//
inline const Point2 operator + (const Vector2 & vec) const;
// Subtract a 2-D vector from a 2-D point
//
inline const Point2 operator - (const Vector2 & vec) const;
// Perform compound assignment and addition with a 2-D vector
//
inline Point2 & operator += (const Vector2 & vec);
// Perform compound assignment and subtraction by a 2-D vector
//
inline Point2 & operator -= (const Vector2 & vec);
};
// Compute the absolute value of a 2-D point per element
//
inline const Point2 absPerElem(const Point2 & pnt);
// Maximum of two 2-D points per element
//
inline const Point2 maxPerElem(const Point2 & pnt0, const Point2 & pnt1);
// Minimum of two 2-D points per element
//
inline const Point2 minPerElem(const Point2 & pnt0, const Point2 & pnt1);
// Maximum element of a 2-D point
//
inline float maxElem(const Point2 & pnt);
// Minimum element of a 2-D point
//
inline float minElem(const Point2 & pnt);
// Compute the square of the distance of a 2-D point from the coordinate-system origin
//
inline float distSqrFromOrigin(const Point2 & pnt);
// Compute the distance of a 2-D point from the coordinate-system origin
//
inline float distFromOrigin(const Point2 & pnt);
// Compute the square of the distance between two 2-D points
//
inline float distSqr(const Point2 & pnt0, const Point2 & pnt1);
// Compute the distance between two 2-D points
//
inline float dist(const Point2 & pnt0, const Point2 & pnt1);
// Linear interpolation between two 2-D points
// NOTE:
// Does not clamp t between 0 and 1.
//
inline const Point2 lerp(float t, const Point2 & pnt0, const Point2 & pnt1);
#ifdef VECTORMATH_DEBUG
// Print a 2-D point
// NOTE:
// Function is only defined when VECTORMATH_DEBUG is defined.
//
inline void print(const Point2 & pnt);
// Print a 2-D point and an associated string identifier
// NOTE:
// Function is only defined when VECTORMATH_DEBUG is defined.
//
inline void print(const Point2 & pnt, const char * name);
#endif // VECTORMATH_DEBUG
// ================================================================================================
// Vector2 implementation
// ================================================================================================
inline Vector2::Vector2(float _x, float _y)
: mX(_x), mY(_y)
{
}
inline Vector2::Vector2(const Point2 & pnt)
: mX(pnt.getX()), mY(pnt.getY())
{
}
inline Vector2::Vector2(float scalar)
: mX(scalar), mY(scalar)
{
}
inline Vector2 & Vector2::setX(float _x)
{
mX = _x;
return *this;
}
inline Vector2 & Vector2::setY(float _y)
{
mY = _y;
return *this;
}
inline float Vector2::getX() const
{
return mX;
}
inline float Vector2::getY() const
{
return mY;
}
inline Vector2 & Vector2::setElem(int idx, float value)
{
*(&mX + idx) = value;
return *this;
}
inline float Vector2::getElem(int idx) const
{
return *(&mX + idx);
}
inline float & Vector2::operator[](int idx)
{
return *(&mX + idx);
}
inline float Vector2::operator[](int idx) const
{
return *(&mX + idx);
}
inline const Vector2 Vector2::operator + (const Vector2 & vec) const
{
return Vector2((mX + vec.mX), (mY + vec.mY));
}
inline const Vector2 Vector2::operator - (const Vector2 & vec) const
{
return Vector2((mX - vec.mX), (mY - vec.mY));
}
inline const Point2 Vector2::operator + (const Point2 & pnt) const
{
return Point2((mX + pnt.getX()), (mY + pnt.getY()));
}
inline const Vector2 Vector2::operator * (float scalar) const
{
return Vector2((mX * scalar), (mY * scalar));
}
inline const Vector2 Vector2::operator / (float scalar) const
{
return Vector2((mX / scalar), (mY / scalar));
}
inline Vector2 & Vector2::operator += (const Vector2 & vec)
{
mX += vec.mX;
mY += vec.mY;
return *this;
}
inline Vector2 & Vector2::operator -= (const Vector2 & vec)
{
mX -= vec.mX;
mY -= vec.mY;
return *this;
}
inline Vector2 & Vector2::operator *= (float scalar)
{
mX *= scalar;
mY *= scalar;
return *this;
}
inline Vector2 & Vector2::operator /= (float scalar)
{
mX /= scalar;
mY /= scalar;
return *this;
}
inline const Vector2 Vector2::operator - () const
{
return Vector2(-mX, -mY);
}
inline const Vector2 Vector2::xAxis()
{
return Vector2(1.0f, 0.0f);
}
inline const Vector2 Vector2::yAxis()
{
return Vector2(0.0f, 1.0f);
}
inline const Vector2 operator * (float scalar, const Vector2 & vec)
{
return vec * scalar;
}
inline const Vector2 absPerElem(const Vector2 & vec)
{
return Vector2(fabsf(vec.getX()), fabsf(vec.getY()));
}
inline const Vector2 maxPerElem(const Vector2 & vec0, const Vector2 & vec1)
{
return Vector2((vec0.getX() > vec1.getX()) ? vec0.getX() : vec1.getX(),
(vec0.getY() > vec1.getY()) ? vec0.getY() : vec1.getY());
}
inline const Vector2 minPerElem(const Vector2 & vec0, const Vector2 & vec1)
{
return Vector2((vec0.getX() < vec1.getX()) ? vec0.getX() : vec1.getX(),
(vec0.getY() < vec1.getY()) ? vec0.getY() : vec1.getY());
}
inline float maxElem(const Vector2 & vec)
{
return (vec.getX() > vec.getY()) ? vec.getX() : vec.getY();
}
inline float minElem(const Vector2 & vec)
{
return (vec.getX() < vec.getY()) ? vec.getX() : vec.getY();
}
inline float dot(const Vector2 & vec0, const Vector2 & vec1)
{
float result;
result = (vec0.getX() * vec1.getX());
result = (result + (vec0.getY() * vec1.getY()));
return result;
}
inline float lengthSqr(const Vector2 & vec)
{
float result;
result = (vec.getX() * vec.getX());
result = (result + (vec.getY() * vec.getY()));
return result;
}
inline float length(const Vector2 & vec)
{
return sqrtf(lengthSqr(vec));
}
inline const Vector2 normalize(const Vector2 & vec)
{
const float lenSqr = lengthSqr(vec);
const float lenInv = (1.0f / sqrtf(lenSqr));
return Vector2((vec.getX() * lenInv), (vec.getY() * lenInv));
}
inline const Vector2 lerp(float t, const Vector2 & vec0, const Vector2 & vec1)
{
return (vec0 + ((vec1 - vec0) * t));
}
#ifdef VECTORMATH_DEBUG
inline void print(const Vector2 & vec)
{
std::printf("( %f %f )\n", vec.getX(), vec.getY());
}
inline void print(const Vector2 & vec, const char * name)
{
std::printf("%s: ( %f %f )\n", name, vec.getX(), vec.getY());
}
#endif // VECTORMATH_DEBUG
// ================================================================================================
// Point2 implementation
// ================================================================================================
inline Point2::Point2(float _x, float _y)
: mX(_x), mY(_y)
{
}
inline Point2::Point2(const Vector2 & vec)
: mX(vec.getX()), mY(vec.getY())
{
}
inline Point2::Point2(float scalar)
: mX(scalar), mY(scalar)
{
}
inline Point2 & Point2::setX(float _x)
{
mX = _x;
return *this;
}
inline Point2 & Point2::setY(float _y)
{
mY = _y;
return *this;
}
inline float Point2::getX() const
{
return mX;
}
inline float Point2::getY() const
{
return mY;
}
inline Point2 & Point2::setElem(int idx, float value)
{
*(&mX + idx) = value;
return *this;
}
inline float Point2::getElem(int idx) const
{
return *(&mX + idx);
}
inline float & Point2::operator[](int idx)
{
return *(&mX + idx);
}
inline float Point2::operator[](int idx) const
{
return *(&mX + idx);
}
inline const Vector2 Point2::operator - (const Point2 & pnt) const
{
return Vector2((mX - pnt.mX), (mY - pnt.mY));
}
inline const Point2 Point2::operator + (const Vector2 & vec) const
{
return Point2((mX + vec.getX()), (mY + vec.getY()));
}
inline const Point2 Point2::operator - (const Vector2 & vec) const
{
return Point2((mX - vec.getX()), (mY - vec.getY()));
}
inline Point2 & Point2::operator += (const Vector2 & vec)
{
mX += vec.getX();
mY += vec.getY();
return *this;
}
inline Point2 & Point2::operator -= (const Vector2 & vec)
{
mX -= vec.getX();
mY -= vec.getY();
return *this;
}
inline const Point2 absPerElem(const Point2 & pnt)
{
return Point2(fabsf(pnt.getX()), fabsf(pnt.getY()));
}
inline const Point2 maxPerElem(const Point2 & pnt0, const Point2 & pnt1)
{
return Point2((pnt0.getX() > pnt1.getX()) ? pnt0.getX() : pnt1.getX(),
(pnt0.getY() > pnt1.getY()) ? pnt0.getY() : pnt1.getY());
}
inline const Point2 minPerElem(const Point2 & pnt0, const Point2 & pnt1)
{
return Point2((pnt0.getX() < pnt1.getX()) ? pnt0.getX() : pnt1.getX(),
(pnt0.getY() < pnt1.getY()) ? pnt0.getY() : pnt1.getY());
}
inline float maxElem(const Point2 & pnt)
{
return (pnt.getX() > pnt.getY()) ? pnt.getX() : pnt.getY();
}
inline float minElem(const Point2 & pnt)
{
return (pnt.getX() < pnt.getY()) ? pnt.getX() : pnt.getY();
}
inline float distSqrFromOrigin(const Point2 & pnt)
{
return lengthSqr(Vector2(pnt));
}
inline float distFromOrigin(const Point2 & pnt)
{
return length(Vector2(pnt));
}
inline float distSqr(const Point2 & pnt0, const Point2 & pnt1)
{
return lengthSqr(pnt1 - pnt0);
}
inline float dist(const Point2 & pnt0, const Point2 & pnt1)
{
return length(pnt1 - pnt0);
}
inline const Point2 lerp(float t, const Point2 & pnt0, const Point2 & pnt1)
{
return (pnt0 + ((pnt1 - pnt0) * t));
}
#ifdef VECTORMATH_DEBUG
inline void print(const Point2 & pnt)
{
std::printf("( %f %f )\n", pnt.getX(), pnt.getY());
}
inline void print(const Point2 & pnt, const char * name)
{
std::printf("%s: ( %f %f )\n", name, pnt.getX(), pnt.getY());
}
#endif // VECTORMATH_DEBUG
} // namespace Vectormath
#endif // VECTORMATH_VEC2D_HPP