forked from OneLoneCoder/olcUTIL_Geometry2D
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patholcUTIL_Geometry2D.h
3271 lines (2694 loc) · 95.2 KB
/
olcUTIL_Geometry2D.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
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
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
OneLoneCoder - Geometry 2D v2.0
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
A collection of 2D Geometric primitives and functions to work with
and between them.
License (OLC-3)
~~~~~~~~~~~~~~~
Copyright 2018 - 2024 OneLoneCoder.com
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions or derivations of source code must retain the above
copyright notice, this list of conditions and the following disclaimer.
2. Redistributions or derivative works in binary form must reproduce
the above copyright notice. This list of conditions and the following
disclaimer must be reproduced in the documentation and/or other
materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Links
~~~~~
YouTube: https://www.youtube.com/javidx9
Discord: https://discord.gg/WhwHUMV
Twitter: https://www.twitter.com/javidx9
Twitch: https://www.twitch.tv/javidx9
GitHub: https://www.github.com/onelonecoder
Homepage: https://www.onelonecoder.com
Authors
~~~~~~~
David Barr, aka javidx9, (c) OneLoneCoder 2019, 2020, 2021, 2022, 2023, 2024
With significant contributions from:
Piratux, Gusgo99, Gorbit99, MaGetzUb, Dandistine, Moros1138
cstdint, sigonasr, bixxy, Qwerasd, starfreakclone, fux
Changes:
v1.01: +Made constants inline
+Header guards (lol... sigh...)
v2.0: +Major file contribution and restructuring
*/
/*
What Is This?
~~~~~~~~~~~~~
I've worked with 2D stuff for decades and I'm tired of reapeatedly researching, deriving
and implementing geometric analysis routines, so wanted a "one-stop-shop" to collate all
these mathematics. You don't even need olc::PixelGameEngine, this file will run as a
standalone 2D vector/geometry utility.
As well as a robust 2D Vector implementation, this file offers definitions of the following
shapes:
point - A 2D (x,y) vector from (0,0)
line - A line segment defined by a start and end point
circle - A circle defined by a middle point and a radius
rectangle - An axis aligned quad defined by a top left point, and a size
triangle - A triangle defined by 3 points
ray - A special case "line" with an origin and a direction
Functions have been provided that yield useful analyses for almost every combination
of shapes. The function groups all have the same names, and are differentiated via
argument type:
point closest(a, b)
Returns closest point on Shape A to Shape B
bool overlaps(a, b)
Returns true if any part of Shape A overlaps any part of Shape B, including boundaries
bool contains(a, b)
Returns true if Shape A wholly contains Shape B within and including it's boundary
vector<point> intersects(a, b)
Returns a vector of points where Shape A boundary intersects with Shape B boundary
optional<point> project(a, b, ray)
Projects Shape A along a ray, until and if it contacts shape B. If it never contacts
then nothing is returned. If it does contact the closest position Shape A can be to
Shape B is returned without the shapes overlapping
rect envelope_r(a) / bounding_box(a)
Returns the minimum area rectangle that fully encompasses Shape A
rect envelope_c(a) / bounding_circle(a)
Returns the minimum area circle that fully encompasses Shape A
ray reflect(ray, a)
Returns a ray that is a reflection of supplied incident ray against Shape A
optional<point, normal> collision(ray, a)
Returns the point and normal where a ray collides with Shape A
*/
/*
Quick Navigation
~~~~~~~~~~~~~~~~
To jump to an implementation quickly, use your editor to search in the following way:
f(a, b)
where:
f = overlaps, intersects, contains, closest, envelope_r, envelope_b, reflects, collision
a = p, l, r, c, t, q, pol (point, line, rect, circ, triangle, ray, polygon)
example:
"contains(r,c)" - takes you to implementation for contains(rect, circ)
- Does the rectangle contain the circle?
*/
/*
Function Matrix - Function(A, B)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
A B>| POINT | LINE | RECT | CIRCLE | TRIANGLE | RAY |
---------+--------------+--------------+--------------+--------------+--------------+--------------+
POINT | contains | contains | contains | contains | contains | |
| closest | closest | closest | closest | closest | closest |
| overlaps | overlaps | overlaps | overlaps | overlaps | |
| intersects | intersects | intersects | intersects | intersects | |
| | | | | | |
---------+--------------+--------------+--------------+--------------+--------------+--------------+
LINE | contains | contains | contains | contains | contains | |
| closest | | | closest | | |
| overlaps | overlaps | overlaps | overlaps | overlaps | |
| intersects | intersects | intersects | intersects | intersects | |
| | | | | | |
---------+--------------+--------------+--------------+--------------+--------------+--------------+
RECT | contains | contains | contains | contains | contains | |
| closest | | | | | |
| overlaps | overlaps | overlaps | overlaps | overlaps | |
| intersects | intersects | intersects | intersects | intersects | |
| | | | | | |
---------+--------------+--------------+--------------+--------------+--------------+--------------+
CIRCLE | contains | contains | contains | contains | contains | |
| closest | closest | | | | |
| overlaps | overlaps | overlaps | overlaps | overlaps | |
| intersects | intersects | intersects | intersects | intersects | |
| project | project | | project | | |
---------+--------------+--------------+--------------+--------------+--------------+--------------+
TRIANGLE | contains | contains | contains | contains | contains | |
| closest | | | | | |
| overlaps | overlaps | overlaps | overlaps | overlaps | |
| intersects | intersects | intersects | intersects | intersects | |
| | | | | | |
---------+--------------+--------------+--------------+--------------+--------------+--------------+
RAY | contains | | | | | |
| | | | | | |
| | collision | collision | collision | collision | collision* |
| | intersects | intersects | intersects | intersects | intersects |
| | reflect | reflect | reflect | reflect | reflect* |
---------+--------------+--------------+--------------+--------------+--------------+--------------+
* Exists but always fails
*/
#pragma once
#include <string>
#include <cmath>
#include <vector>
#include <algorithm>
#include <cstdint>
#include <optional>
#include <cassert>
#include <array>
#ifdef PGE_VER
#error "olcUTIL_Geometry2D.h must be included BEFORE olcPixelGameEngine.h"
#else
#if !defined(OLC_VECTOR2D_DEFINED)
namespace olc
{
/*
A complete 2D geometric vector structure, with a variety
of useful utility functions and operator overloads
*/
template<class T>
struct v_2d
{
static_assert(std::is_arithmetic<T>::value, "olc::v_2d<type> must be numeric");
// x-axis component
T x = 0;
// y-axis component
T y = 0;
// Default constructor
inline constexpr v_2d() = default;
// Specific constructor
inline constexpr v_2d(T _x, T _y) : x(_x), y(_y)
{}
// Copy constructor
inline constexpr v_2d(const v_2d& v) = default;
// Assignment operator
inline constexpr v_2d& operator=(const v_2d& v) = default;
// Returns rectangular area of vector
inline constexpr auto area() const
{
return x * y;
}
// Returns magnitude of vector
inline auto mag() const
{
return std::sqrt(x * x + y * y);
}
// Returns magnitude squared of vector (useful for fast comparisons)
inline constexpr T mag2() const
{
return x * x + y * y;
}
// Returns normalised version of vector
inline v_2d norm() const
{
auto r = 1 / mag();
return v_2d(x * r, y * r);
}
// Returns vector at 90 degrees to this one
inline constexpr v_2d perp() const
{
return v_2d(-y, x);
}
// Rounds both components down
inline constexpr v_2d floor() const
{
return v_2d(std::floor(x), std::floor(y));
}
// Rounds both components up
inline constexpr v_2d ceil() const
{
return v_2d(std::ceil(x), std::ceil(y));
}
// Returns 'element-wise' max of this and another vector
inline constexpr v_2d max(const v_2d& v) const
{
return v_2d(std::max(x, v.x), std::max(y, v.y));
}
// Returns 'element-wise' min of this and another vector
inline constexpr v_2d min(const v_2d& v) const
{
return v_2d(std::min(x, v.x), std::min(y, v.y));
}
// Calculates scalar dot product between this and another vector
inline constexpr auto dot(const v_2d& rhs) const
{
return this->x * rhs.x + this->y * rhs.y;
}
// Calculates 'scalar' cross product between this and another vector (useful for winding orders)
inline constexpr auto cross(const v_2d& rhs) const
{
return this->x * rhs.y - this->y * rhs.x;
}
// Treat this as polar coordinate (R, Theta), return cartesian equivalent (X, Y)
inline constexpr v_2d cart() const
{
return v_2d(std::cos(y) * x, std::sin(y) * x);
}
// Treat this as cartesian coordinate (X, Y), return polar equivalent (R, Theta)
inline constexpr v_2d polar() const
{
return v_2d(mag(), std::atan2(y, x));
}
// Clamp the components of this vector in between the 'element-wise' minimum and maximum of 2 other vectors
inline constexpr v_2d clamp(const v_2d& v1, const v_2d& v2) const
{
return this->max(v1).min(v2);
}
// Linearly interpolate between this vector, and another vector, given normalised parameter 't'
inline constexpr v_2d lerp(const v_2d& v1, const double t) const
{
return (*this) * (T(1.0 - t)) + (v1 * T(t));
}
// Compare if this vector is numerically equal to another
inline constexpr bool operator == (const v_2d& rhs) const
{
return (this->x == rhs.x && this->y == rhs.y);
}
// Compare if this vector is not numerically equal to another
inline constexpr bool operator != (const v_2d& rhs) const
{
return (this->x != rhs.x || this->y != rhs.y);
}
// Return this vector as a std::string, of the form "(x,y)"
inline std::string str() const
{
return std::string("(") + std::to_string(this->x) + "," + std::to_string(this->y) + ")";
}
// Assuming this vector is incident, given a normal, return the reflection
inline constexpr v_2d reflect(const v_2d& n) const
{
return (*this) - 2.0 * (this->dot(n) * n);
}
// Allow 'casting' from other v_2d types
template<class F>
inline constexpr operator v_2d<F>() const
{
return { static_cast<F>(this->x), static_cast<F>(this->y) };
}
};
// Multiplication operator overloads between vectors and scalars, and vectors and vectors
template<class TL, class TR>
inline constexpr auto operator * (const TL& lhs, const v_2d<TR>& rhs)
{
return v_2d(lhs * rhs.x, lhs * rhs.y);
}
template<class TL, class TR>
inline constexpr auto operator * (const v_2d<TL>& lhs, const TR& rhs)
{
return v_2d(lhs.x * rhs, lhs.y * rhs);
}
template<class TL, class TR>
inline constexpr auto operator * (const v_2d<TL>& lhs, const v_2d<TR>& rhs)
{
return v_2d(lhs.x * rhs.x, lhs.y * rhs.y);
}
template<class TL, class TR>
inline constexpr auto operator *= (v_2d<TL>& lhs, const TR& rhs)
{
lhs = lhs * rhs;
return lhs;
}
// Division operator overloads between vectors and scalars, and vectors and vectors
template<class TL, class TR>
inline constexpr auto operator / (const TL& lhs, const v_2d<TR>& rhs)
{
return v_2d(lhs / rhs.x, lhs / rhs.y);
}
template<class TL, class TR>
inline constexpr auto operator / (const v_2d<TL>& lhs, const TR& rhs)
{
return v_2d(lhs.x / rhs, lhs.y / rhs);
}
template<class TL, class TR>
inline constexpr auto operator / (const v_2d<TL>& lhs, const v_2d<TR>& rhs)
{
return v_2d(lhs.x / rhs.x, lhs.y / rhs.y);
}
template<class TL, class TR>
inline constexpr auto operator /= (v_2d<TL>& lhs, const TR& rhs)
{
lhs = lhs / rhs;
return lhs;
}
// Unary Addition operator (pointless but i like the platinum trophies)
template<class T>
inline constexpr auto operator + (const v_2d<T>& lhs)
{
return v_2d(+lhs.x, +lhs.y);
}
// Addition operator overloads between vectors and scalars, and vectors and vectors
template<class TL, class TR>
inline constexpr auto operator + (const TL& lhs, const v_2d<TR>& rhs)
{
return v_2d(lhs + rhs.x, lhs + rhs.y);
}
template<class TL, class TR>
inline constexpr auto operator + (const v_2d<TL>& lhs, const TR& rhs)
{
return v_2d(lhs.x + rhs, lhs.y + rhs);
}
template<class TL, class TR>
inline constexpr auto operator + (const v_2d<TL>& lhs, const v_2d<TR>& rhs)
{
return v_2d(lhs.x + rhs.x, lhs.y + rhs.y);
}
template<class TL, class TR>
inline constexpr auto operator += (v_2d<TL>& lhs, const TR& rhs)
{
lhs = lhs + rhs;
return lhs;
}
template<class TL, class TR>
inline constexpr auto operator += (v_2d<TL>& lhs, const v_2d<TR>& rhs)
{
lhs = lhs + rhs;
return lhs;
}
// Unary negation operator overoad for inverting a vector
template<class T>
inline constexpr auto operator - (const v_2d<T>& lhs)
{
return v_2d(-lhs.x, -lhs.y);
}
// Subtraction operator overloads between vectors and scalars, and vectors and vectors
template<class TL, class TR>
inline constexpr auto operator - (const TL& lhs, const v_2d<TR>& rhs)
{
return v_2d(lhs - rhs.x, lhs - rhs.y);
}
template<class TL, class TR>
inline constexpr auto operator - (const v_2d<TL>& lhs, const TR& rhs)
{
return v_2d(lhs.x - rhs, lhs.y - rhs);
}
template<class TL, class TR>
inline constexpr auto operator - (const v_2d<TL>& lhs, const v_2d<TR>& rhs)
{
return v_2d(lhs.x - rhs.x, lhs.y - rhs.y);
}
template<class TL, class TR>
inline constexpr auto operator -= (v_2d<TL>& lhs, const TR& rhs)
{
lhs = lhs - rhs;
return lhs;
}
// Greater/Less-Than Operator overloads - mathematically useless, but handy for "sorted" container storage
template<class TL, class TR>
inline constexpr bool operator < (const v_2d<TL>& lhs, const v_2d<TR>& rhs)
{
return (lhs.y < rhs.y) || (lhs.y == rhs.y && lhs.x < rhs.x);
}
template<class TL, class TR>
inline constexpr bool operator > (const v_2d<TL>& lhs, const v_2d<TR>& rhs)
{
return (lhs.y > rhs.y) || (lhs.y == rhs.y && lhs.x > rhs.x);
}
// Allow olc::v_2d to play nicely with std::cout
template<class T>
inline constexpr std::ostream& operator << (std::ostream& os, const v_2d<T>& rhs)
{
os << rhs.str();
return os;
}
// Convenient types ready-to-go
typedef v_2d<int32_t> vi2d;
typedef v_2d<uint32_t> vu2d;
typedef v_2d<float> vf2d;
typedef v_2d<double> vd2d;
}
#define OLC_VECTOR2D_DEFINED 1
#endif
namespace olc::utils::geom2d
{
// Lemon Meringue
inline const double pi = 3.141592653589793238462643383279502884;
// Floating point error margin
inline const double epsilon = 0.001;
namespace internal
{
template<typename T>
inline std::vector<olc::v_2d<T>> filter_duplicate_points(const std::vector<olc::v_2d<T>>& points) {
std::vector<olc::v_2d<T>> filtered_points;
for (const auto& point : points)
{
bool is_duplicate = false;
for (const auto& filtered_point : filtered_points)
{
if (std::abs(point.x - filtered_point.x) < epsilon && std::abs(point.y - filtered_point.y) < epsilon)
{
is_duplicate = true;
break;
}
}
if (!is_duplicate)
{
filtered_points.push_back(point);
}
}
return filtered_points;
}
};
//https://stackoverflow.com/questions/1903954/is-there-a-standard-sign-function-signum-sgn-in-c-c
template <typename T>
constexpr int sgn(T val) { return (T(0) < val) - (val < T(0)); }
// Defines a line segment
template<typename T>
struct line
{
olc::v_2d<T> start;
olc::v_2d<T> end;
inline line(const olc::v_2d<T>& s = { T(0), T(0) },
const olc::v_2d<T>& e = { T(0), T(0) })
: start(s), end(e)
{ }
// Get vector pointing from start to end
inline constexpr olc::v_2d<T> vector() const
{
return (end - start);
}
// Get length of line
inline constexpr T length()
{
return vector().mag();
}
// Get length of line^2
inline constexpr T length2()
{
return vector().mag2();
}
// Given a real distance, get point along line
inline constexpr olc::v_2d<T> rpoint(const T& distance) const
{
return start + vector().norm() * distance;
}
// Given a unit distance, get point along line
inline constexpr olc::v_2d<T> upoint(const T& distance) const
{
return start + vector() * distance;
}
// Return which side of the line does a point lie
inline constexpr int32_t side(const olc::v_2d<T>& point) const
{
double d = vector().cross(point - start);
if (d < 0)
return -1;
else
if (d > 0)
return 1;
else
return 0;
}
// Returns line equation "mx + a" coefficients where:
// x: m
// y: a
// NOTE: Returns {inf, inf} if std::abs(end.x - start.x) < epsilon:
inline constexpr olc::vd2d coefficients() const
{
double x1 = start.x;
double x2 = end.x;
double y1 = start.y;
double y2 = end.y;
// check if line is vertical or close to vertical
if (std::abs(x2 - x1) < epsilon) {
return olc::vd2d{ std::numeric_limits<double>::infinity(), std::numeric_limits<double>::infinity() };
}
double m = (y2 - y1) / (x2 - x1);
return olc::vd2d {m, -m * x1 + y1};
}
};
template<typename T>
struct ray
{
olc::v_2d<T> origin;
olc::v_2d<T> direction;
inline ray(const olc::v_2d<T>& o = { T(0), T(0) },
const olc::v_2d<T>& d = { T(0), T(0) })
: origin(o), direction(d)
{ }
};
template<typename T>
struct rect
{
olc::v_2d<T> pos;
olc::v_2d<T> size;
inline rect(const olc::v_2d<T>& p = { T(0), T(0) },
const olc::v_2d<T>& s = { T(1), T(1) })
: pos(p), size(s)
{ }
inline olc::v_2d<T> middle() const
{
return pos + (size * double(0.5));
}
// Get line segment from top side of rectangle
inline line<T> top() const
{
return { pos, {pos.x + size.x, pos.y } };
}
// Get line segment from bottom side of rectangle
inline line<T> bottom() const
{
return { {pos.x, pos.y + size.y}, pos + size };
}
// Get line segment from left side of rectangle
inline line<T> left() const
{
return { pos, {pos.x, pos.y + size.y} };
}
// Get line segment from right side of rectangle
inline line<T> right() const
{
return { {pos.x + size.x, pos.y }, pos + size };
}
// Get a line from an indexed side, starting top, going clockwise
inline line<T> side(const size_t i) const
{
if ((i & 0b11) == 0) return top();
if ((i & 0b11) == 1) return right();
if ((i & 0b11) == 2) return bottom();
//if ((i & 0b11) == 3) return left(); // Dumb compilers cant fathom this
return left();
}
// Get area of rectangle
inline constexpr T area() const
{
return size.x * size.y;
}
// Get perimeter of rectangle
inline constexpr T perimeter() const
{
return T(2) * (size.x + size.y);
}
// Returns side count: 4
inline constexpr size_t side_count() const {
return 4;
}
};
template<typename T>
struct circle
{
olc::v_2d<T> pos;
T radius = T(0);
inline circle(const olc::v_2d<T>& p = { T(0), T(0) }, const T r = T(0))
: pos(p), radius(r)
{ }
// Get area of circle
inline constexpr T area() const
{
return T(pi) * radius * radius;
}
// Get circumference of circle
inline constexpr T perimeter() const
{
return T(2.0 * pi) * radius;
}
// Get circumference of circle
inline constexpr T circumference() const
{
return perimeter();
}
};
template<typename T>
struct triangle
{
std::array<olc::v_2d<T>, 3> pos;
inline triangle(
const olc::v_2d<T>& p0 = { T(0), T(0) },
const olc::v_2d<T>& p1 = { T(0), T(0) },
const olc::v_2d<T>& p2 = { T(0), T(0) })
: pos{ p0,p1,p2 }
{ }
// Get a line from an indexed side, starting top, going clockwise
inline line<T> side(const size_t i) const
{
return line(pos[i % 3], pos[(i + 1) % 3]);
}
// Get area of triangle
inline constexpr T area() const
{
return double(0.5) * std::abs(
(pos[0].x * (pos[1].y - pos[2].y)) +
(pos[1].x * (pos[2].y - pos[0].y)) +
(pos[2].x * (pos[0].y - pos[1].y)));
}
// Get perimeter of triangle
inline constexpr T perimeter() const
{
return line(pos[0], pos[1]).length()
+ line(pos[1], pos[2]).length()
+ line(pos[2], pos[0]).length();
}
// Returns side count: 3
inline constexpr size_t side_count() const {
return 3;
}
};
template<typename T>
struct polygon
{
std::vector<olc::v_2d<T>> pos;
// triangles that make up the polygon
std::vector<triangle<T>> triangles;
std::vector<line<T>> edges;
};
// =========================================================================================================================
// Closest(shape, point) ===================================================================================================
// Closest location on [SHAPE] to Point
// closest(p,p)
// Returns closest point on point to any shape (aka the original point) :P
template<typename T1, typename T2>
inline olc::v_2d<T1> closest(const olc::v_2d<T1>& p, [[maybe_unused]] const T2& anything_who_cares)
{
return p;
}
// closest(l,p)
// Returns closest point on line to point
template<typename T1, typename T2>
inline olc::v_2d<T1> closest(const line<T1>& l, const olc::v_2d<T2>& p)
{
auto d = l.vector();
double u = std::clamp(double(d.dot(p - l.start)) / d.mag2(), 0.0, 1.0);
return l.start + u * d;
}
// closest(c,p)
// Returns closest point on circle to point
template<typename T1, typename T2>
inline olc::v_2d<T1> closest(const circle<T1>& c, const olc::v_2d<T2>& p)
{
return c.pos + olc::vd2d(p - c.pos).norm() * c.radius;
}
// closest(r,p)
// Returns closest point on rectangle to point
template<typename T1, typename T2>
inline olc::v_2d<T1> closest(const rect<T1>& r, const olc::v_2d<T2>& p)
{
// Note: this algorithm can be reused for polygon
auto c1 = closest(r.top(), p);
auto c2 = closest(r.bottom(), p);
auto c3 = closest(r.left(), p);
auto c4 = closest(r.right(), p);
auto d1 = (c1 - p).mag2();
auto d2 = (c2 - p).mag2();
auto d3 = (c3 - p).mag2();
auto d4 = (c4 - p).mag2();
auto dmin = d1;
auto cmin = c1;
if (d2 < dmin) {
dmin = d2;
cmin = c2;
}
if (d3 < dmin) {
dmin = d3;
cmin = c3;
}
if (d4 < dmin) {
dmin = d4;
cmin = c4;
}
return cmin;
}
// closest(t,p)
// Returns closest point on triangle to point
template<typename T1, typename T2>
inline olc::v_2d<T1> closest(const triangle<T1>& t, const olc::v_2d<T2>& p)
{
olc::utils::geom2d::line<T1> l{ t.pos[0], t.pos[1] };
auto p0 = closest(l, p);
auto d0 = (p0 - p).mag2();
l.end = t.pos[2];
auto p1 = closest(l, p);
auto d1 = (p1 - p).mag2();
l.start = t.pos[1];
auto p2 = closest(l, p);
auto d2 = (p2 - p).mag2();
if ((d0 <= d1) && (d0 <= d2)) {
return p0;
}
else if ((d1 <= d0) && (d1 <= d2)) {
return p1;
}
else {
return p2;
}
}
// closest(ray,p)
// Returns closest point on ray to point
template<typename T1, typename T2>
inline olc::v_2d<T1> closest(const ray<T1>& r, const olc::v_2d<T2>& p)
{
// TODO: implement
return p;
}
// closest(p,p)
// Returns closest point on polygon to point
template<typename T1, typename T2>
inline olc::v_2d<T1> closest(const polygon<T1>& p, const olc::v_2d<T2>& point)
{
// TODO:
return {};
}
// Closest location on [SHAPE] to Line
// closest(l,l)
// Returns closest point on line to line
template<typename T1, typename T2>
inline olc::v_2d<T1> closest(const line<T1>& l1, const line<T2>& l2)
{
// TODO:
return {};
}
// closest(r,l)
// Returns closest point on rectangle to line
template<typename T1, typename T2>
inline olc::v_2d<T1> closest(const rect<T1>& r, const line<T2>& l)
{
// TODO:
return {};
}
// closest(c,l)
// Returns closest point on circle to line
template<typename T1, typename T2>
inline olc::v_2d<T1> closest(const circle<T1>& c, const line<T2>& l)
{
const auto p1 = closest(l, c.pos);
return c.pos + olc::vd2d(p1 - c.pos).norm() * c.radius;
}
// closest(t,l)
// Returns closest point on triangle to line
template<typename T1, typename T2>
inline olc::v_2d<T1> closest(const triangle<T1>& t, const line<T2>& l)
{
// TODO:
return {};
}
// closest(p,l)
// Returns closest point on polygon to line
template<typename T1, typename T2>
inline olc::v_2d<T1> closest(const polygon<T1>& p, const line<T2>& l)
{
// TODO:
return {};
}
// Closest location on [SHAPE] to Circle
// closest(l,c)
// Returns closest point on line to circle
template<typename T1, typename T2>
inline olc::v_2d<T1> closest(const line<T1>& l, const circle<T2>& c)
{
const auto p1 = closest(c, l); // Closest point on circle to line
return closest(l, p1);
}
// closest(r,c)
// Returns closest point on rectangle to circle
template<typename T1, typename T2>
inline olc::v_2d<T1> closest(const rect<T1>& r, const circle<T2>& l)
{
// TODO:
return {};
}
// closest(c,c)
// Returns closest point on circle to circle
template<typename T1, typename T2>
inline olc::v_2d<T1> closest(const circle<T1>& c1, const circle<T2>& c2)
{
return closest(c1, c2.pos);
}
// closest(t,c)
// Returns closest point on triangle to circle
template<typename T1, typename T2>
inline olc::v_2d<T1> closest(const triangle<T1>& r, const circle<T2>& l)
{
// TODO:
return {};
}
// closest(p,c)
// Returns closest point on polygon to circle
template<typename T1, typename T2>
inline olc::v_2d<T1> closest(const polygon<T1>& p, const circle<T2>& c)
{
// TODO:
return {};