-
Notifications
You must be signed in to change notification settings - Fork 0
/
apollonius.cpp
3479 lines (3033 loc) · 95.4 KB
/
apollonius.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
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
/**
* Copyright (c) 2023-present Merlot.Rain
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include "apollonius.h"
#include <algorithm>
#include <array>
#include <assert.h>
#include <cmath>
#include <iostream>
#include <limits>
#include <numeric>
#include <set>
#include <vector>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#define APO_TOLERANCE (1.0e-9)
#define APO_UNKOWN_TYPE (-1)
#define APO_POINT_TYPE (0)
#define APO_LINE_TYPE (1)
#define APO_CIRCLE_TYPE (2)
#define APO_IS_POINT(shp) ((shp).type == APO_POINT_TYPE)
#define APO_IS_LINE(shp) ((shp).type == APO_LINE_TYPE)
#define APO_IS_CIRCLE(shp) ((shp).type == APO_CIRCLE_TYPE)
#define APO_IS_NULL(shp) ((shp).type == APO_UNKOWN_TYPE)
#define APO_IS_NORMALE_NUMBER(x) (!std::isnan(x) && !std::isinf(x))
enum Side
{
Left,
Right,
BothSides,
};
struct APO_Point
{
double x;
double y;
bool valid;
APO_Point();
APO_Point(double x, double y, bool valid_in = true);
void set(double vx, double vy);
void setPolar(double radius, double angle);
bool isValid() const;
APO_Point operator+(const APO_Point& rhs) const;
APO_Point operator-(const APO_Point& rhs) const;
APO_Point operator*(double scale) const;
APO_Point operator/(double scale) const;
APO_Point& operator+=(const APO_Point& rhs);
APO_Point& operator-=(const APO_Point& rhs);
APO_Point& operator*=(double scale);
APO_Point& operator/=(double scale);
double getDistanceTo(const APO_Point& point, bool limited = true) const;
bool equalsFuzzy(const APO_Point& point) const;
void setAngle(double a);
double getAngle() const;
double getAngleTo(const APO_Point& point) const;
double getMagnitude() const;
void setMagnitude(double m);
static APO_Point getAverage(const APO_Point& v1, const APO_Point& v2);
static double getCrossProduct(const APO_Point& v1, const APO_Point& v2);
static double getDotProduct(const APO_Point& v1, const APO_Point& v2);
static APO_Point createPolar(double radius, double angle);
static std::vector<APO_Point>
getUnique(const std::vector<APO_Point>& vectors);
APO_Point& rotate(double rotation);
APO_Point& rotate(double rotation, const APO_Point& center);
static APO_Point invalid;
static APO_Point undefined;
friend bool
operator==(const APO_Point& lhs, const APO_Point& rhs)
{
if (lhs.valid == true && rhs.valid == true)
{
return lhs.x == rhs.x && lhs.y == rhs.y;
}
else if (lhs.valid == false && rhs.valid == false)
{
return true;
}
return false;
}
friend bool
operator!=(const APO_Point& lhs, const APO_Point& rhs)
{
return !(lhs == rhs);
}
};
struct APO_Line
{
APO_Point begin_point;
APO_Point end_point;
APO_Line();
APO_Line(const APO_Point& begin, const APO_Point& end);
APO_Line(double x1, double y1, double x2, double y2);
APO_Line(const APO_Point& begin, double angle, double distance);
double getAngle() const;
void setLength(double l, bool fromStart = true);
double getLength() const;
APO_Point getClosestPoint(const APO_Point& p, bool limited) const;
APO_Point getMiddlePoint() const;
bool rotate(double rotation, const APO_Point& center);
bool move(const APO_Point& offset);
double getDistanceTo(const APO_Point& point, bool limited = true) const;
void reverse();
bool moveTo(const APO_Point& dest);
std::vector<APO_Line> getOffset(double distance, double num,
Side side) const;
APO_Point getVectorTo(const APO_Point& point, bool limited) const;
Side getSideOfPoint(const APO_Point& point) const;
static APO_Line undefined;
};
struct APO_Circle
{
APO_Point center;
double radius;
APO_Circle();
APO_Circle(double center_x, double center_y, double radius);
APO_Circle(const APO_Point& center, double radius);
static APO_Circle createFrom3Points(const APO_Point& p1, const APO_Point& p2,
const APO_Point& p3);
static APO_Circle createFrom2Points(const APO_Point& p1,
const APO_Point& p2);
bool containsPoint(const APO_Point& p) const;
};
struct APO_Shape
{
int type;
union
{
APO_Point point;
APO_Line line;
APO_Circle circle;
};
APO_Shape();
APO_Shape(const APO_Point& p);
APO_Shape(const APO_Line& l);
APO_Shape(const APO_Circle& c);
};
template <class T>
bool
isUndefined(const T& t)
{
return false;
}
template <>
bool
isUndefined<APO_Point>(const APO_Point& t)
{
return std::isnan(t.x) && std::isnan(t.y) && !t.valid;
}
template <>
bool
isUndefined<APO_Line>(const APO_Line& t)
{
return isUndefined<APO_Point>(t.begin_point)
&& isUndefined<APO_Point>(t.end_point);
}
template <>
bool
isUndefined<APO_Circle>(const APO_Circle& t)
{
return isUndefined<APO_Point>(t.center) && std::isnan(t.radius);
}
template <class T>
T
undefined()
{
return T::undefined;
}
/* ------------------------- static solve functions ------------------------- */
static bool getInverseShape(const APO_Shape& shp,
const APO_Shape& inversionCircle,
APO_Shape& inversed);
template <class T>
static std::vector<APO_Shape>
getInverseShapes(const std::vector<T>& shapes,
const APO_Shape& inversionCircle);
static std::vector<APO_Line>
getCircleTangentsThroughPoint(const APO_Circle& circle, const APO_Point& p);
static std::vector<APO_Line> getAllTangents(const APO_Shape& shp1,
const APO_Shape& shp2);
static std::vector<APO_Circle>
getSolutions(const std::vector<APO_Shape>& shps);
static std::vector<APO_Circle> getSolution(const APO_Shape& shp1,
const APO_Shape& shp2,
const APO_Shape& shp3);
static std::vector<APO_Circle> getSolutionFromPPP(const APO_Point& point1,
const APO_Point& point2,
const APO_Point& point3);
static std::vector<APO_Circle> getSolutionFromPPC(const APO_Point& point1,
const APO_Point& point2,
const APO_Circle& circle);
static std::vector<APO_Circle> getSolutionFromPPL(const APO_Point& point1,
const APO_Point& point2,
const APO_Line& line);
static std::vector<APO_Circle> getSolutionFromPCC(const APO_Point& point,
const APO_Circle& circle1,
const APO_Circle& circle2);
static std::vector<APO_Circle> getSolutionFromPLL(const APO_Point& point,
const APO_Line& line1,
const APO_Line& line2);
static std::vector<APO_Circle> getSolutionFromPLC(const APO_Point& point,
const APO_Line& line,
const APO_Circle& circle);
static std::vector<APO_Circle> getSolutionFromLLL(const APO_Line& line1,
const APO_Line& line2,
const APO_Line& line3);
static std::vector<APO_Circle> getSolutionFromLLC(const APO_Line& line1,
const APO_Line& line2,
const APO_Circle& circle);
static std::vector<APO_Circle> getSolutionFromLCC(const APO_Line& line,
const APO_Circle& circle1,
const APO_Circle& circle2);
static std::vector<APO_Circle> getSolutionFromCCC(const APO_Circle& circle1,
const APO_Circle& circle2,
const APO_Circle& circle3);
template <class T, class E>
static std::vector<APO_Point> getIntersectionPoints(const T& t, const E& e,
bool limited);
template <>
std::vector<APO_Point> getIntersectionPoints<APO_Circle, APO_Circle>(
const APO_Circle& t, const APO_Circle& e, bool limited);
template <>
std::vector<APO_Point>
getIntersectionPoints<APO_Line, APO_Line>(const APO_Line& t, const APO_Line& e,
bool limited);
template <>
std::vector<APO_Point>
getIntersectionPoints<APO_Line, APO_Circle>(const APO_Line& t,
const APO_Circle& e, bool limited);
template <class T, class E>
static bool isIntersectWith(const T& t, const E& e, bool limited);
static bool pointIsOnLine(const APO_Point& point, const APO_Line& line,
bool limited);
static bool pointIsOnCircle(const APO_Point& point, const APO_Circle& circle);
static bool fuzzyCompare(double a, double b);
static double getAngleDifference(double a1, double a2);
static std::vector<APO_Line> getAngleBisectors(const APO_Line& line1,
const APO_Line& line2);
static APO_Point getCommonIntersectionPoint(const APO_Circle& c1,
const APO_Circle& c2,
const APO_Circle& c3);
static APO_Point getPowerCenter(const APO_Circle& c1, const APO_Circle& c2,
const APO_Circle& c3);
static std::vector<APO_Line> getSimilarityAxes(const APO_Circle& c1,
const APO_Circle& c2,
const APO_Circle& c3);
static APO_Point getPole(const APO_Circle& circle, const APO_Line& polarLine);
static std::vector<APO_Circle> getSolutionsCCCAlt(const APO_Circle& circle1,
const APO_Circle& circle2,
const APO_Circle& circle3);
template <class T>
static std::vector<T> verify(const std::vector<T>& candidates,
const APO_Shape& shape1, const APO_Shape& shape2,
const APO_Shape& shape3);
static std::vector<APO_Circle>
getCircles2TR(const APO_Line shp1, const APO_Line& shp2, double radius,
const APO_Point& pos = APO_Point());
template <class T>
static std::vector<T> removeDuplicates(const std::vector<T>& shaps);
/* ------------------------- template function impls ------------------------ */
template <class T>
std::vector<APO_Shape>
getInverseShapes(const std::vector<T>& shapes,
const APO_Shape& inversionCircle)
{
std::vector<APO_Shape> res;
for (auto&& shp : shapes)
{
APO_Shape inversed;
if (getInverseShape(shp, inversionCircle, inversed))
{
res.push_back(inversed);
}
}
return res;
}
template <class T, class E>
std::vector<APO_Point>
getIntersectionPoints(const T& t, const E& e, bool limited)
{
return std::vector<APO_Point>();
}
template <>
std::vector<APO_Point>
getIntersectionPoints<APO_Circle, APO_Circle>(const APO_Circle& circle1,
const APO_Circle& circle2,
bool limited)
{
std::vector<APO_Point> res;
double r1 = circle1.radius;
double r2 = circle2.radius;
if (r1 < r2)
{
return getIntersectionPoints<APO_Circle, APO_Circle>(circle2, circle1,
true);
}
APO_Point c1 = circle1.center;
APO_Point c2 = circle2.center;
APO_Point u = c2 - c1;
double u_mag = u.getMagnitude();
// concentric
if (u_mag < APO_TOLERANCE)
{
return res;
}
double tol = (r1 + r2) / 200000;
// the two circles (almost) touch externally / internally in one point
// (tangent):
if (fabs(u_mag - (r1 + r2)) < tol || fabs(u_mag - fabs(r1 - r2)) < tol)
{
u.setMagnitude(r1);
res.push_back(c1 + u);
return res;
}
APO_Point v(u.y, -u.x);
double s, t1, t2, term;
s = 1.0 / 2.0 * ((r1 * r1 - r2 * r2) / (pow(u_mag, 2.0)) + 1.0);
term = (r1 * r1) / (pow(u_mag, 2.0)) -s * s;
// no intersection:
if (term < 0.0)
{
return res;
}
// one or two intersections:
t1 = sqrt(term);
t2 = -sqrt(term);
APO_Point sol1 = c1 + u * s + v * t1;
APO_Point sol2 = c1 + u * s + v * t2;
if (fabs(sol1.x - sol2.x) < tol && fabs(sol1.y - sol2.y) < tol)
{
res.push_back(sol1);
}
else
{
res.push_back(sol1);
res.push_back(sol2);
}
return res;
}
template <>
std::vector<APO_Point>
getIntersectionPoints<APO_Line, APO_Line>(const APO_Line& line1,
const APO_Line& line2, bool limited)
{
std::vector<APO_Point> res;
double a1 = line1.end_point.y - line1.begin_point.y;
double b1 = line1.begin_point.x - line1.end_point.x;
double c1 = a1 * line1.begin_point.x + b1 * line1.begin_point.y;
double a2 = line2.end_point.y - line2.begin_point.y;
double b2 = line2.begin_point.x - line2.end_point.x;
double c2 = a2 * line2.begin_point.x + b2 * line2.begin_point.y;
double det = a1 * b2 - a2 * b1;
if (fabs(det) < 1.0e-6)
{
return res;
}
else
{
APO_Point v((b2 * c1 - b1 * c2) / det, (a1 * c2 - a2 * c1) / det);
if ((!limited || 0 == pointIsOnLine(v, line1, limited))
&& (!limited || 0 == pointIsOnLine(v, line2, limited)))
{
res.push_back(v);
}
}
return res;
}
template <>
std::vector<APO_Point>
getIntersectionPoints<APO_Line, APO_Circle>(const APO_Line& line,
const APO_Circle& circle,
bool limited)
{
std::vector<APO_Point> res;
APO_Point vLineCenter = line.getVectorTo(circle.center, false);
double dist = vLineCenter.getMagnitude();
// special case: arc almost touches line (tangent with tiny gap or tiny
// overlap):
if (fuzzyCompare(dist, circle.radius))
{
APO_Point sol = circle.center - vLineCenter;
if (!limited || pointIsOnLine(sol, line, true))
{
res.push_back(sol);
}
return res;
}
APO_Point p = line.begin_point;
APO_Point d = line.end_point - line.begin_point;
if (d.getMagnitude() < 1.0e-6)
{
return res;
}
APO_Point delta = p - circle.center;
// root term:
double term = std::pow(APO_Point::getDotProduct(d, delta), 2.0)
- std::pow(d.getMagnitude(), 2.0)
* (std::pow(delta.getMagnitude(), 2.0)
- std::pow(circle.radius, 2.0));
// no intersection:
if (term < 0.0)
{
return res;
}
// one or two intersections:
double t1 = (-APO_Point::getDotProduct(d, delta) + sqrt(term))
/ std::pow(d.getMagnitude(), 2.0);
double t2;
bool tangent = false;
// only one intersection:
if (fabs(term) < APO_TOLERANCE)
{
t2 = t1;
tangent = true;
}
// two intersections
else
{
t2 = (-APO_Point::getDotProduct(d, delta) - sqrt(term))
/ std::pow(d.getMagnitude(), 2.0);
}
APO_Point sol1;
APO_Point sol2(0, 0, false);
sol1 = p + d * t1;
if (!tangent)
{
sol2 = p + d * t2;
}
if (!limited || pointIsOnLine(sol1, line, true))
{
res.push_back(sol1);
}
if (sol2.isValid())
{
if (!limited || pointIsOnLine(sol2, line, true))
{
res.push_back(sol2);
}
}
// ret.setTangent(tangent);
// tangent with two intersections very close to each other:
if (res.size() == 2 && res[0].equalsFuzzy(res[1]))
{
res.pop_back();
}
return res;
}
template <class T, class E>
bool
isIntersectWith(const T& t, const E& e, bool limited)
{
auto&& ips = getIntersectionPoints<T, E>(t, e, limited);
return !ips.empty();
}
template <class T>
inline std::vector<T>
verify(const std::vector<T>& candidates, const APO_Shape& shape1,
const APO_Shape& shape2, const APO_Shape& shape3)
{
return std::vector<T>();
}
template <class T>
std::vector<T>
removeDuplicates(const std::vector<T>& shaps)
{
auto cmp = [&](const APO_Shape& a, const APO_Shape& b)
{
if (APO_IS_LINE(a) && APO_IS_LINE(b))
{
return a.line.begin_point.equalsFuzzy(b.line.begin_point)
&& b.line.end_point.equalsFuzzy(b.line.end_point);
}
if (APO_IS_CIRCLE(a) && APO_IS_CIRCLE(b))
{
return a.circle.center.equalsFuzzy(b.circle.center)
&& fuzzyCompare(a.circle.radius, b.circle.radius);
}
return false;
};
std::set<T, decltype(cmp)> sets(cmp);
for (auto&& s : shaps)
{
sets.insert(s);
}
std::vector<T> res;
for (auto it = sets.begin(); it != sets.end(); ++it)
{
res.push_back(*it);
}
return res;
}
/* --------------------------------- solves --------------------------------- */
/// http://www.geometer.org/mathcircles/inversion.pdf
bool
getInverseShape(const APO_Shape& shp, const APO_Shape& inversionCircle,
APO_Shape& inversed)
{
// inverse point
// https://mathworld.wolfram.com/InversePoints.html
//
// Points, also called polar reciprocals, which are transformed into each
// other through inversion about a given inversion circle C (or inversion
// sphere). The points P and P^' are inverse points with respect to the
// inversion circle if
// OP * OP' = r^2
// In this case, P^' is called the inversion pole and the line L through P
// and perpendicular to OP is called the polar. In the above figure, the
// quantity r^2 is called the circle power of the point P relative to the
// circle C.
if (APO_IS_POINT(shp))
{
double r = inversionCircle.circle.radius;
APO_Point center = inversionCircle.circle.center;
double d = shp.point.getDistanceTo(center);
if (fabs(d) < APO_TOLERANCE)
{
inversed = APO_Shape(shp.point);
return true;
}
double d_inverse = pow(r, 2) / d;
inversed = APO_Shape(
APO_Point(center.x + (shp.point.x - center.x) * d_inverse / d,
center.y + (shp.point.y - center.y) * d_inverse / d));
return true;
}
// line inverse
if (APO_IS_LINE(shp))
{
APO_Point center = inversionCircle.circle.center;
// A "line" that passes through O is inverted to itself. Note, of course
// that the individual points of the "line" are inverted to other points
// on the "line" except for the two points where it passes through k.
if (pointIsOnLine(center, shp.line, false))
{
inversed = APO_Shape(shp.line);
return true;
}
else
{
// Every "line" that does not pass through O is inverted to a circle
// (no quotes: a real circle) that passes through O.
APO_Line s;
s.begin_point = center;
s.end_point = shp.line.getClosestPoint(center, false);
// intersection_points from shp.line and s
std::vector<APO_Point> ips = getIntersectionPoints(shp.line, s, false);
if (ips.size() == 1)
{
APO_Point p = ips[0];
APO_Shape pinverse;
if (getInverseShape(p, inversionCircle, pinverse))
{
inversed = APO_Shape(
APO_Circle::createFrom2Points(center, pinverse.point));
return true;
}
}
}
}
if (APO_IS_CIRCLE(shp))
{
/// concentric circles
APO_Circle circle = shp.circle;
if (circle.center.equalsFuzzy(inversionCircle.circle.center))
{
APO_Shape inversed_point_shp;
getInverseShape(
APO_Point(circle.center.x + circle.radius, circle.center.y),
inversionCircle, inversed_point_shp);
double radius = circle.center.x - inversed_point_shp.point.x;
if (radius < 0)
{
radius = fabs(radius);
}
inversed = APO_Shape(APO_Circle(circle.center, radius));
return true;
}
else if (pointIsOnCircle(inversionCircle.circle.center, circle))
{
APO_Line s(inversionCircle.circle.center, circle.center);
std::vector<APO_Point> ips = getIntersectionPoints(s, circle, false);
if (ips.size() > 0)
{
return false;
}
APO_Point p = ips[0];
if (p.equalsFuzzy(inversionCircle.circle.center))
{
if (ips.size() < 2)
{
return false;
}
p = ips[1];
}
APO_Shape pinverse;
if (!getInverseShape(p, inversionCircle, pinverse))
{
return false;
}
inversed = APO_Shape(
APO_Line(pinverse.point, s.getAngle() + M_PI / 2.0, 1.0));
return true;
}
else
{
APO_Line l(inversionCircle.circle.center, circle.center);
std::vector<APO_Point> ips = getIntersectionPoints(l, circle, false);
if (ips.empty())
{
return false;
}
APO_Point p1 = ips[0];
APO_Point p2 = ips[1];
APO_Shape p1inverse, p2inverse;
if (!getInverseShape(p1, inversionCircle, p1inverse))
{
return false;
}
if (!getInverseShape(p2, inversionCircle, p2inverse))
{
return false;
}
inversed = APO_Shape(
APO_Circle::createFrom2Points(p1inverse.point, p2inverse.point));
return true;
}
}
return false;
}
std::vector<APO_Line>
getCircleTangentsThroughPoint(const APO_Circle& circle, const APO_Point& p)
{
std::vector<APO_Line> res;
// used when creating tangential circles to two parallel lines and point:
if (fabs(circle.radius) < APO_TOLERANCE)
{
res.push_back(APO_Line(p, circle.center));
res.push_back(APO_Line(p, circle.center));
return res;
}
// point on the circle line (produces error):
else if (pointIsOnCircle(p, circle))
{
APO_Line s(p, circle.center);
res.push_back(APO_Line(p, s.getAngle() + M_PI / 2.0, 1.0));
return res;
}
// pointis inside the circle:
else if (circle.containsPoint(p))
{
return res;
}
// point outside circle:
else
{
APO_Circle circle2 = APO_Circle::createFrom2Points(p, circle.center);
std::vector<APO_Point> touching_points
= getIntersectionPoints(circle2, circle, true);
if (touching_points.size() == 1)
{
res.push_back(APO_Line(p, touching_points[0]));
}
else if (touching_points.size() == 2)
{
res.push_back(APO_Line(p, touching_points[0]));
res.push_back(APO_Line(p, touching_points[1]));
}
}
return res;
}
std::vector<APO_Line>
getAllTangents(const APO_Shape& shp1, const APO_Shape& shp2)
{
// Collection of tangent lines to return
std::vector<APO_Line> tangents;
// Distance between center points, difference of radii, sum of radii
double dC1C2, rDiff, rSum;
// Tangent being constructed
APO_Line tangent;
// Angles
double a1, a2, at;
// Offset points
APO_Point offset1, offset2;
// Line shape
APO_Line line;
// Intersection points
std::vector<APO_Point> ips;
// Most common usage: Handle 2 circle shapes first:
if (APO_IS_CIRCLE(shp1) && APO_IS_CIRCLE(shp2))
{
// Normalized circle shape clones, dummy to order c1-c2 on size
APO_Circle c1, c2;
// Radii of circle shapes
double c1Radius, c2Radius;
// Centers of circle shapes
APO_Point c1Center, c2Center;
// Validate first circle shape:
if (std::isnan(shp1.circle.radius))
{
return tangents; // Empty, invalid radius
}
else if (fabs(shp1.circle.radius) < APO_TOLERANCE)
{
// Handle as zero sized 2D circle:
c1 = shp1.circle;
c1.radius = 0.0;
}
else
{
// Handle as normalized 2D circle:
c1 = shp1.circle;
c1.radius = fabs(shp1.circle.radius);
}
// Validate second circle shape:
if (std::isnan(shp2.circle.radius))
{
return tangents; // Empty, invalid radius
}
else if (fabs(shp2.circle.radius) < APO_TOLERANCE)
{
// Handle as zero sized 2D circle:
c2 = shp2.circle;
c2.radius = 0.0;
}
else
{
// Handle as normalized 2D circle:
c2 = shp2.circle;
c2.radius = fabs(shp2.circle.radius);
}
// Ensure that c1 is the smaller circle:
// Does not swap equal sized circles
if (c1.radius > c2.radius)
{
std::swap(c1, c2);
}
// With 2 valid circle shapes;
c1Radius = c1.radius;
c1Center = c1.center;
c2Radius = c2.radius;
c2Center = c2.center;
// Not expecting NaN with 2 valid circle shapes:
dC1C2 = c1Center.getDistanceTo(c2Center);
// Reject (almost) concentric circles:
if (dC1C2 < 1e-6)
{
return tangents; // Empty, no solutions
}
// (Almost) internally touching circles:
if (fuzzyCompare(dC1C2 + c1Radius, c2Radius))
{ // APO_TOLERANCE
tangent = APO_Line(c2Center, c1Center);
// With 2 radii larger than zero:
if (c1Radius > 0.0)
{
tangent.setLength((dC1C2 + c1Radius) * 2, true); // fromStart
}
// Handle point on circle here instead of externally touching:
// Ensuring that the single valid tangent is the first
else
{
tangent.setLength(c2Radius * 2, true); // fromStart
}
tangent.rotate(M_PI / 2, tangent.getMiddlePoint());
// First and final solution:
tangents.push_back(tangent);
return tangents; // One single solution
}
// Exclude other nested circles:
if (dC1C2 + c1Radius < c2Radius)
{
return tangents; // Empty, no solutions
}
// Include external tangents:
rDiff = c2Radius - c1Radius;
if (dC1C2 > rDiff)
{
a1 = c1Center.getAngleTo(c2Center);
a2 = std::asin(rDiff / dC1C2);
offset1 = APO_Point();
offset2 = APO_Point();
// First solution:
at = a1 + a2 + M_PI / 2.0;
offset1.setPolar(c1Radius, at);
offset2.setPolar(c2Radius, at);
tangents.push_back(APO_Line(c1Center + offset1, c2Center + offset2));
// Second solution, exclude for R1=R2=zero:
if (c2Radius < APO_TOLERANCE)
{
// No second solution
}
else
{
at = a1 - a2 - M_PI / 2.0;
offset1.setPolar(c1Radius, at);
offset2.setPolar(c2Radius, at);
tangents.push_back(APO_Line(c1Center + offset1, c2Center + offset2));
}
}
// No external tangents:
else
{
// No tangents
}
// (Almost) externally touching circles:
rSum = c2Radius + c1Radius;
if (fuzzyCompare(dC1C2, rSum))
{ // APO_TOLERANCE
tangent = APO_Line(c2Center, c1Center);
tangent.setLength(c2Radius * 2, true); // fromStart
tangent.rotate(M_PI / 2, tangent.getMiddlePoint());
// Third and final solution:
tangents.push_back(tangent);
// only one solution
return tangents;
}
// Include internal tangents but only for radii larger than zero:
if (dC1C2 > rSum && c1Radius > 0.0)
{
a1 = c1Center.getAngleTo(c2Center);
a2 = std::asin(rSum / dC1C2);
offset1 = APO_Point();
offset2 = APO_Point();
// Third solution:
at = a1 + a2 + M_PI / 2.0;
offset1.setPolar(c1Radius, at);
offset2.setPolar(c2Radius, at);
tangents.push_back(APO_Line(c1Center - offset1, c2Center + offset2));
// Fourth solution:
at = a1 - a2 - M_PI / 2.0;
offset1.setPolar(c1Radius, at);
offset2.setPolar(c2Radius, at);
tangents.push_back(APO_Line(c1Center - offset1, c2Center + offset2));
}
// No internal tangents:
else
{
// No tangents
}
return tangents;
} // End 2 circles
// With 2 line shapes (Circles with infinite radii):
else if (APO_IS_LINE(shp1) && APO_IS_LINE(shp2))
{
APO_Line l1, l2;
l1 = APO_Line(shp1.line.begin_point, shp1.line.end_point);
l2 = APO_Line(shp2.line.begin_point, shp2.line.end_point);
// The angle of a near zero-length line is zero by default (APO_Point::getAngle())
// Exclude solutions for a line with almost no length:
if (l1.getLength() <= 1.0e-6 || l2.getLength() <= 1.0e-6)
{
return tangents; // Empty, not processable line(s)