-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcircle_test.go
More file actions
1348 lines (1207 loc) · 52.1 KB
/
Copy pathcircle_test.go
File metadata and controls
1348 lines (1207 loc) · 52.1 KB
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
package geom
import (
"encoding/json"
"fmt"
"math"
"slices"
"testing"
"github.com/gravitton/assert"
)
func TestCircle_Constructor(t *testing.T) {
t.Run("int", func(t *testing.T) {
AssertCircle(t, Circ(Pt(10, 16), 12), Circle[int]{Center: Pt(10, 16), Radius: 12})
})
t.Run("float", func(t *testing.T) {
AssertCircle(t, Circ(Pt(0.16, 204), 5.1), Circle[float64]{Center: Pt(0.16, 204.0), Radius: 5.1})
})
t.Run("a negative radius is taken absolute", func(t *testing.T) {
AssertCircle(t, Circ(Pt(10, 16), -12), Circ(Pt(10, 16), 12))
})
}
func TestCircle_Anchor(t *testing.T) {
c := Circ(Pt(10.0, 10.0), 5.0)
t.Run("cardinal directions", func(t *testing.T) {
AssertPoint(t, c.Anchor(Right), Pt(15.0, 10.0))
AssertPoint(t, c.Anchor(Left), Pt(5.0, 10.0))
AssertPoint(t, c.Anchor(Top), Pt(10.0, 5.0))
AssertPoint(t, c.Anchor(Bottom), Pt(10.0, 15.0))
})
t.Run("diagonals land on the boundary", func(t *testing.T) {
// unlike Rectangle, whose diagonals reach the corners
AssertNumber(t, c.Center.DistanceTo(c.Anchor(DirectionUpRight)), c.Radius)
})
t.Run("none is the center", func(t *testing.T) {
AssertPoint(t, c.Anchor(DirectionNone), Pt(10.0, 10.0))
})
}
func TestCircle_Centroid(t *testing.T) {
AssertPoint(t, Circ(Pt(1, 2), 10).Centroid(), Pt(1, 2))
}
func TestCircle_Area(t *testing.T) {
t.Run("int", func(t *testing.T) {
AssertNumber(t, Circ(Pt(1, 2), 10).Area(), Pi*100.0)
})
t.Run("float", func(t *testing.T) {
AssertNumber(t, Circ(Pt(0.6, -0.25), 1.2).Area(), Pi*1.44)
})
t.Run("large integer radius does not overflow", func(t *testing.T) {
AssertNumber(t, Circ(Pt(0, 0), 3037000500).Area(), Pi*3037000500*3037000500)
})
}
func TestCircle_Perimeter(t *testing.T) {
t.Run("int", func(t *testing.T) {
AssertNumber(t, Circ(Pt(1, 2), 10).Perimeter(), Pi*20.0)
})
t.Run("float", func(t *testing.T) {
AssertNumber(t, Circ(Pt(0.6, -0.25), 1.2).Perimeter(), Pi*2.4)
})
}
func TestCircle_Inertia(t *testing.T) {
t.Run("int", func(t *testing.T) {
AssertNumber(t, Circ(Pt(1, 2), 10).Inertia(), Pi*5000.0)
})
t.Run("float", func(t *testing.T) {
AssertNumber(t, Circ(Pt(0.6, -0.25), 1.2).Inertia(), Pi*1.0368)
})
t.Run("agrees with the ellipse and is approached by the polygon", func(t *testing.T) {
for _, c := range circleFixtures {
AssertNumber(t, c.Inertia(), c.Ellipse().Inertia(), c.String())
assert.EqualDelta(t, c.Inertia(), c.RegularPolygon(360, OrientationFlatTop).Inertia(), c.Inertia()*1e-3, c.String())
}
})
}
func TestCircle_Diameter(t *testing.T) {
t.Run("int", func(t *testing.T) {
AssertNumber(t, Circ(Pt(1, 2), 10).Diameter(), 20)
})
t.Run("float", func(t *testing.T) {
AssertNumber(t, Circ(Pt(0.6, -0.25), 1.2).Diameter(), 2.4)
})
}
func TestCircle_Bounds(t *testing.T) {
t.Run("int", func(t *testing.T) {
AssertBox(t, Circ(Pt(1, 2), 10).Bounds(), BoxFromMinMax(Pt(-9, -8), Pt(11, 12)))
})
t.Run("float", func(t *testing.T) {
AssertBox(t, Circ(Pt(0.6, -0.25), 1.2).Bounds(), BoxFromMinMax(Pt(-0.6, -1.45), Pt(1.8, 0.95)))
})
}
func TestCircle_Translate(t *testing.T) {
t.Run("int", func(t *testing.T) {
AssertCircle(t, Circ(Pt(1, 2), 10).Translate(Vec(3, -2)), Circ(Pt(4, 0), 10))
})
t.Run("float", func(t *testing.T) {
AssertCircle(t, Circ(Pt(0.6, -0.25), 1.2).Translate(Vec(100.1, -0.1)), Circ(Pt(100.7, -0.35), 1.2))
})
}
func TestCircle_MoveTo(t *testing.T) {
t.Run("int", func(t *testing.T) {
AssertCircle(t, Circ(Pt(1, 2), 10).MoveTo(Pt(3, -2)), Circ(Pt(3, -2), 10))
})
t.Run("float", func(t *testing.T) {
AssertCircle(t, Circ(Pt(0.6, -0.25), 1.2).MoveTo(Pt(100.1, -0.1)), Circ(Pt(100.1, -0.1), 1.2))
})
}
func TestCircle_Scale(t *testing.T) {
t.Run("int", func(t *testing.T) {
AssertCircle(t, Circ(Pt(1, 2), 10).Scale(2.5), Circ(Pt(1, 2), 25))
})
t.Run("float", func(t *testing.T) {
AssertCircle(t, Circ(Pt(0.6, -0.25), 1.2).Scale(2.5), Circ(Pt(0.6, -0.25), 3.0))
})
t.Run("a negative factor scales by its absolute value", func(t *testing.T) {
AssertCircle(t, Circ(Pt(1, 2), 10).Scale(-2), Circ(Pt(1, 2), 20))
})
}
func TestCircle_Unscale(t *testing.T) {
t.Run("int", func(t *testing.T) {
AssertCircle(t, Circ(Pt(1, 2), 25).Unscale(2.5), Circ(Pt(1, 2), 10))
})
t.Run("float", func(t *testing.T) {
AssertCircle(t, Circ(Pt(0.6, -0.25), 3.0).Unscale(2.5), Circ(Pt(0.6, -0.25), 1.2))
})
t.Run("a negative factor scales by its absolute value", func(t *testing.T) {
AssertCircle(t, Circ(Pt(1, 2), 20).Unscale(-2), Circ(Pt(1, 2), 10))
})
t.Run("zero factor panics", func(t *testing.T) {
assert.Panics(t, func() {
Circ(Pt(1, 2), 10).Unscale(0)
}, "geom: division by zero")
})
}
func TestCircle_Resize(t *testing.T) {
t.Run("int", func(t *testing.T) {
AssertCircle(t, Circ(Pt(1, 2), 10).Resize(8), Circ(Pt(1, 2), 8))
})
t.Run("float", func(t *testing.T) {
AssertCircle(t, Circ(Pt(0.6, -0.25), 1.2).Resize(3.1), Circ(Pt(0.6, -0.25), 3.1))
})
t.Run("a negative radius is taken absolute", func(t *testing.T) {
AssertCircle(t, Circ(Pt(1, 2), 10).Resize(-8), Circ(Pt(1, 2), 8))
})
}
func TestCircle_Canonical(t *testing.T) {
t.Run("takes a literal negative radius absolute and keeps the center", func(t *testing.T) {
AssertCircle(t, Circle[int]{Pt(1, 2), -8}.Canonical(), Circ(Pt(1, 2), 8))
AssertCircle(t, Circle[float64]{Pt(0.6, -0.25), -1.2}.Canonical(), Circ(Pt(0.6, -0.25), 1.2))
})
t.Run("is the circle Circ builds", func(t *testing.T) {
c := Circle[int]{Pt(1, 2), -8}
AssertCircle(t, c.Canonical(), Circ(c.Center, c.Radius))
assert.True(t, c.Canonical().Contains(c.Canonical().Anchor(Right)))
})
t.Run("repairs decoded JSON", func(t *testing.T) {
var c Circle[int]
assert.Nil(t, json.Unmarshal([]byte(`{"x":1,"y":2,"r":-8}`), &c))
AssertCircle(t, c.Canonical(), Circ(Pt(1, 2), 8))
})
t.Run("a well-formed circle is unchanged", func(t *testing.T) {
for _, c := range circleFixtures {
AssertCircle(t, c.Canonical(), c, c.String())
}
})
}
func TestCircle_Grow(t *testing.T) {
t.Run("int", func(t *testing.T) {
AssertCircle(t, Circ(Pt(1, 2), 10).Grow(8), Circ(Pt(1, 2), 18))
AssertCircle(t, Circ(Pt(1, 2), 10).Grow(-12), Circ(Pt(1, 2), 0))
})
t.Run("float", func(t *testing.T) {
AssertCircle(t, Circ(Pt(0.6, -0.25), 1.2).Grow(3.1), Circ(Pt(0.6, -0.25), 4.3))
})
}
func TestCircle_Shrink(t *testing.T) {
t.Run("reduces the radius", func(t *testing.T) {
AssertCircle(t, Circ(Pt(1, 2), 10).Shrink(8), Circ(Pt(1, 2), 2))
AssertCircle(t, Circ(Pt(0.6, -0.25), 1.2).Shrink(0.3), Circ(Pt(0.6, -0.25), 0.9))
})
t.Run("clamps to zero", func(t *testing.T) {
AssertCircle(t, Circ(Pt(1, 2), 10).Shrink(100), Circ(Pt(1, 2), 0))
AssertCircle(t, Circ(Pt(0.6, -0.25), 1.2).Shrink(5.0), Circ(Pt(0.6, -0.25), 0.0))
})
}
func TestCircle_Lerp(t *testing.T) {
a, b := Circ(Pt(0.0, 0.0), 2.0), Circ(Pt(10.0, 20.0), 8.0)
t.Run("moves the center and the radius together", func(t *testing.T) {
AssertCircle(t, a.Lerp(b, 0.5), Circ(Pt(5.0, 10.0), 5.0))
})
t.Run("the ends are the circles themselves", func(t *testing.T) {
AssertCircle(t, a.Lerp(b, 0), a)
AssertCircle(t, a.Lerp(b, 1), b)
})
t.Run("extrapolates and keeps the radius absolute", func(t *testing.T) {
AssertCircle(t, a.Lerp(b, 2), Circ(Pt(20.0, 40.0), 14.0))
AssertCircle(t, Circ(Pt(0.0, 0.0), 2.0).Lerp(Circ(Pt(0.0, 0.0), 0.0), 2), Circ(Pt(0.0, 0.0), 2.0))
})
}
func TestCircle_Rotate(t *testing.T) {
circle := Circ(Pt(1.0, 2.0), 3.0)
t.Run("no angle moves a circle", func(t *testing.T) {
AssertCircle(t, circle.Rotate(Pi/3), circle)
AssertCircle(t, circle.Rotate(-Pi), circle)
})
t.Run("the bounds turn with nothing", func(t *testing.T) {
for _, c := range circleFixtures {
AssertCircle(t, c.Rotate(Pi/7), c, c.String())
}
})
}
func TestCircle_AlignTo(t *testing.T) {
c := Circ(Pt(10.0, 10.0), 5.0)
t.Run("moves the anchor onto the point", func(t *testing.T) {
AssertCircle(t, c.AlignTo(Bottom, Pt(0.0, 0.0)), Circ(Pt(0.0, -5.0), 5.0))
AssertCircle(t, Circ(Pt(1, 2), 3).AlignTo(Left, Pt(0, 0)), Circ(Pt(3, 0), 3))
})
t.Run("none aligns the center like MoveTo", func(t *testing.T) {
AssertCircle(t, c.AlignTo(DirectionNone, Pt(1.0, 2.0)), c.MoveTo(Pt(1.0, 2.0)))
})
t.Run("is the inverse of Anchor", func(t *testing.T) {
for _, c := range circleFixtures {
for _, direction := range Directions() {
AssertPoint(t, c.AlignTo(direction, Pt(1.5, -2.5)).Anchor(direction), Pt(1.5, -2.5), fmt.Sprintf("%s %s: ", c, direction))
}
}
})
}
func TestCircle_Contains(t *testing.T) {
t.Run("inside", func(t *testing.T) {
assert.True(t, Circ(Pt(1, 2), 10).Contains(Pt(4, 4)))
assert.True(t, Circ(Pt(0.6, -0.25), 1.2).Contains(Pt(0.1, 0.8)))
})
t.Run("outside", func(t *testing.T) {
assert.False(t, Circ(Pt(1, 2), 10).Contains(Pt(1, 13)))
assert.False(t, Circ(Pt(0.6, -0.25), 1.2).Contains(Pt(0.0, 1.7)))
})
t.Run("the center and the boundary are inside", func(t *testing.T) {
c := Circ(Pt(1, 2), 10)
assert.True(t, c.Contains(c.Center))
assert.True(t, c.Contains(c.Anchor(Right)))
assert.False(t, c.Contains(c.Anchor(Right).AddXY(1, 0)))
})
t.Run("agrees with a zero-length segment at the point", func(t *testing.T) {
for _, c := range circleFixtures {
for _, p := range pointFixtures {
assert.Equal(t, c.Contains(p), Seg(p, p).IntersectsCircle(c), fmt.Sprintf("%s → %s: ", c, p))
}
}
})
t.Run("a float anchor is inside despite rounding", func(t *testing.T) {
c := Circ(Pt(0.1, 0.2), 0.7)
for _, direction := range Directions() {
assert.True(t, c.Contains(c.Anchor(direction)), direction.String())
}
assert.False(t, c.Contains(c.Anchor(Right).AddXY(2*Delta, 0)))
})
}
func TestCircle_DistanceTo(t *testing.T) {
circle := Circ(Pt(0, 0), 3)
t.Run("outside measures to the boundary", func(t *testing.T) {
AssertNumber(t, circle.DistanceTo(Pt(7, 0)), 4.0)
AssertNumber(t, circle.DistanceTo(Pt(3, 4)), 2.0)
})
t.Run("inside and on the boundary are zero", func(t *testing.T) {
assert.Equal(t, circle.DistanceTo(Pt(1, 1)), 0.0)
assert.Equal(t, circle.DistanceTo(Pt(3, 0)), 0.0)
assert.Equal(t, circle.DistanceTo(circle.Center), 0.0)
})
t.Run("float", func(t *testing.T) {
AssertNumber(t, Circ(Pt(0.0, 0.0), 1.0).DistanceTo(Pt(1.0, 1.0)), Sqrt2-1)
})
t.Run("float within the tolerance is zero, beyond it is measured", func(t *testing.T) {
c := Circ(Pt(0.0, 0.0), 1.0)
assert.Equal(t, c.DistanceTo(Pt(1.0+Delta/2, 0.0)), 0.0)
AssertNumber(t, c.DistanceTo(Pt(1.0+2*Delta, 0.0)), 2*Delta)
})
t.Run("zero exactly where Contains holds", func(t *testing.T) {
for _, c := range circleFixtures {
for _, p := range pointFixtures {
assert.Equal(t, c.DistanceTo(p) == 0, c.Contains(p), fmt.Sprintf("%s → %s: ", c, p))
}
}
})
}
func TestCircle_DistanceSquaredTo(t *testing.T) {
circle := Circ(Pt(0, 0), 3)
t.Run("is the square of DistanceTo", func(t *testing.T) {
AssertNumber(t, circle.DistanceSquaredTo(Pt(7, 0)), 16.0)
AssertNumber(t, circle.DistanceSquaredTo(Pt(3, 4)), 4.0)
assert.Equal(t, circle.DistanceSquaredTo(Pt(1, 1)), 0.0)
})
t.Run("agrees with DistanceTo", func(t *testing.T) {
for _, c := range circleFixtures {
for _, p := range pointFixtures {
AssertNumber(t, c.DistanceSquaredTo(p), c.DistanceTo(p)*c.DistanceTo(p), fmt.Sprintf("%s → %s: ", c, p))
}
}
})
}
func TestCircle_Nearest(t *testing.T) {
circle := Circ(Pt(0.0, 0.0), 3.0)
t.Run("the boundary toward the point", func(t *testing.T) {
AssertPoint(t, circle.Nearest(Pt(7.0, 0.0)), Pt(3.0, 0.0))
AssertPoint(t, circle.Nearest(Pt(3.0, 4.0)), Pt(1.8, 2.4))
})
t.Run("a point inside is its own nearest point", func(t *testing.T) {
AssertPoint(t, circle.Nearest(Pt(1.0, 1.0)), Pt(1.0, 1.0))
})
t.Run("a point within the tolerance is kept as it is", func(t *testing.T) {
assert.Equal(t, circle.Nearest(Pt(3.0+Delta/2, 0.0)), Pt(3.0+Delta/2, 0.0))
})
t.Run("int rounds once", func(t *testing.T) {
AssertPoint(t, Circ(Pt(0, 0), 2).Nearest(Pt(5, 1)), Pt(2, 0))
})
t.Run("a zero radius is its center", func(t *testing.T) {
AssertPoint(t, Circ(Pt(1.0, 1.0), 0.0).Nearest(Pt(5.0, 4.0)), Pt(1.0, 1.0))
})
t.Run("over the fixtures", func(t *testing.T) {
for _, c := range circleFixtures {
for _, p := range pointFixtures {
assertNearest[float64](t, c, p)
}
}
})
}
func TestCircle_EnclosesCircle(t *testing.T) {
circle := Circ(Pt(0, 0), 10)
t.Run("inside", func(t *testing.T) {
assert.True(t, circle.EnclosesCircle(Circ(Pt(2, 0), 5)))
assert.True(t, circle.EnclosesCircle(Circ(Pt(0, 0), 0)))
})
t.Run("touching the boundary from inside counts", func(t *testing.T) {
assert.True(t, circle.EnclosesCircle(Circ(Pt(5, 0), 5)))
assert.False(t, circle.EnclosesCircle(Circ(Pt(6, 0), 5)))
})
t.Run("larger or apart", func(t *testing.T) {
assert.False(t, circle.EnclosesCircle(Circ(Pt(0, 0), 11)))
assert.False(t, circle.EnclosesCircle(Circ(Pt(30, 0), 1)))
})
t.Run("float within the tolerance of the boundary counts, beyond it not", func(t *testing.T) {
assert.True(t, Circ(Pt(0.0, 0.0), 10.0).EnclosesCircle(Circ(Pt(5.0+Delta/2, 0.0), 5.0)))
assert.False(t, Circ(Pt(0.0, 0.0), 10.0).EnclosesCircle(Circ(Pt(5.0+2*Delta, 0.0), 5.0)))
})
t.Run("encloses exactly where it encloses the far point", func(t *testing.T) {
for _, a := range circleFixtures {
for _, b := range circleFixtures {
far := b.Center.AddXY(b.Radius, 0)
if direction := b.Center.Subtract(a.Center); direction.hasDirection() {
far = b.Center.Add(direction.Resize(b.Radius))
}
assert.Equal(t, a.EnclosesCircle(b), a.Contains(far), fmt.Sprintf("%s → %s: ", a, b))
}
}
})
}
func TestCircle_EnclosesSegment(t *testing.T) {
circle := Circ(Pt(0, 0), 10)
t.Run("inside", func(t *testing.T) {
assert.True(t, circle.EnclosesSegment(Seg(Pt(-5, 0), Pt(5, 5))))
})
t.Run("a chord counts", func(t *testing.T) {
assert.True(t, circle.EnclosesSegment(Seg(Pt(-10, 0), Pt(10, 0))))
assert.True(t, circle.EnclosesSegment(Seg(Pt(-6, 8), Pt(8, -6))))
})
t.Run("an endpoint outside", func(t *testing.T) {
assert.False(t, circle.EnclosesSegment(Seg(Pt(0, 0), Pt(11, 0))))
})
}
func TestCircle_EnclosesPolygon(t *testing.T) {
circle := Circ(Pt(1, 1), 2)
t.Run("inside", func(t *testing.T) {
assert.True(t, circle.EnclosesPolygon(Pol(squareVertices())))
})
t.Run("a vertex outside", func(t *testing.T) {
assert.False(t, Circ(Pt(1, 1), 1).EnclosesPolygon(Pol(squareVertices())))
})
t.Run("an empty polygon is enclosed by nothing", func(t *testing.T) {
assert.False(t, circle.EnclosesPolygon(Pol[int](nil)))
})
}
func TestCircle_EnclosesRectangle(t *testing.T) {
t.Run("corners on the boundary count", func(t *testing.T) {
assert.True(t, Circ(Pt(0, 0), 5).EnclosesRectangle(Rect(Pt(0, 0), Sz(6, 8))))
assert.False(t, Circ(Pt(0, 0), 5).EnclosesRectangle(Rect(Pt(0, 0), Sz(6, 9))))
})
t.Run("rotated is tested on its turned corners", func(t *testing.T) {
long := Rect(Pt(0.0, 0.0), Sz(9.0, 1.0))
assert.False(t, Circ(Pt(0.0, 0.0), 4.0).EnclosesRectangle(long))
assert.False(t, Circ(Pt(0.0, 0.0), 4.0).EnclosesRectangle(long.Rotate(Pi/4)))
assert.True(t, Circ(Pt(0.0, 0.0), 5.0).EnclosesRectangle(long.Rotate(Pi/4)))
})
}
func TestCircle_EnclosesRegularPolygon(t *testing.T) {
t.Run("a circle encloses its inscribed polygon", func(t *testing.T) {
for _, c := range circleFixtures {
assert.True(t, c.EnclosesRegularPolygon(c.RegularPolygon(7, OrientationFlatTop)), c.String())
}
})
t.Run("a vertex outside", func(t *testing.T) {
assert.False(t, Circ(Pt(0, 0), 2).EnclosesRegularPolygon(RegPol(Pt(1, 0), Sz(2, 2), 4, 0)))
})
t.Run("an empty polygon is enclosed by nothing", func(t *testing.T) {
assert.False(t, Circ(Pt(0, 0), 2).EnclosesRegularPolygon(RegPol(Pt(0, 0), Sz(1, 1), 0, 0)))
})
}
func TestCircle_EnclosesBox(t *testing.T) {
t.Run("an inscribed box counts, the bounds of the circle not", func(t *testing.T) {
c := Circ(Pt(0, 0), 5)
assert.True(t, c.EnclosesBox(BoxFromMinMax(Pt(-3, -4), Pt(3, 4))))
assert.False(t, c.EnclosesBox(c.Bounds()))
})
}
func TestCircle_IntersectsCircle(t *testing.T) {
circle := Circ(Pt(0.0, 0.0), 100.0)
t.Run("overlapping", func(t *testing.T) {
assert.True(t, circle.IntersectsCircle(Circ(Pt(199.0, 0.0), 100.0)))
})
t.Run("apart", func(t *testing.T) {
assert.False(t, circle.IntersectsCircle(Circ(Pt(210.0, 0.0), 100.0)))
})
t.Run("exactly touching counts as an intersection", func(t *testing.T) {
assert.True(t, circle.IntersectsCircle(Circ(Pt(200.0, 0.0), 100.0)))
assert.False(t, circle.IntersectsCircle(Circ(Pt(201.0, 0.0), 100.0)))
})
t.Run("one contained in the other", func(t *testing.T) {
assert.True(t, circle.IntersectsCircle(Circ(Pt(0.0, 0.0), 50.0)))
})
t.Run("narrow integers do not overflow the radii sum", func(t *testing.T) {
assert.True(t, Circ(Pt[int8](0, 0), 100).IntersectsCircle(Circ(Pt[int8](0, 50), 100)))
assert.False(t, Circ(Pt[int8](-20, 0), 50).IntersectsCircle(Circ(Pt[int8](100, 0), 50)))
})
t.Run("holds wherever Intersection finds a point", func(t *testing.T) {
for _, a := range circleFixtures {
for _, b := range circleFixtures {
if len(a.IntersectionCircle(b)) > 0 {
assert.True(t, a.IntersectsCircle(b), fmt.Sprintf("%s → %s: ", a, b))
}
}
}
})
t.Run("symmetric", func(t *testing.T) {
for _, a := range circleFixtures {
for _, b := range circleFixtures {
assert.Equal(t, a.IntersectsCircle(b), b.IntersectsCircle(a), fmt.Sprintf("%s → %s: ", a, b))
}
}
})
}
func TestCircle_IntersectionCircle(t *testing.T) {
circle := Circ(Pt(0.0, 0.0), 5.0)
t.Run("overlapping gives two points mirrored across the centers", func(t *testing.T) {
AssertVertices(t, circle.IntersectionCircle(Circ(Pt(6.0, 0.0), 5.0)), []Point[float64]{Pt(3.0, 4.0), Pt(3.0, -4.0)})
AssertVertices(t, circle.IntersectionCircle(Circ(Pt(0.0, 6.0), 5.0)), []Point[float64]{Pt(-4.0, 3.0), Pt(4.0, 3.0)})
})
t.Run("a larger circle behind the first", func(t *testing.T) {
height := math.Sqrt(1 - 0.125*0.125)
AssertVertices(t, Circ(Pt(0.0, 0.0), 1.0).IntersectionCircle(Circ(Pt(1.0, 0.0), 1.5)), []Point[float64]{Pt(-0.125, height), Pt(-0.125, -height)})
})
t.Run("tangent from outside gives one point", func(t *testing.T) {
AssertVertices(t, circle.IntersectionCircle(Circ(Pt(8.0, 0.0), 3.0)), []Point[float64]{Pt(5.0, 0.0)})
})
t.Run("tangent from inside gives one point", func(t *testing.T) {
AssertVertices(t, circle.IntersectionCircle(Circ(Pt(2.0, 0.0), 3.0)), []Point[float64]{Pt(5.0, 0.0)})
AssertVertices(t, Circ(Pt(0.0, 0.0), 3.0).IntersectionCircle(Circ(Pt(2.0, 0.0), 5.0)), []Point[float64]{Pt(-3.0, 0.0)})
})
t.Run("apart, nested and concentric give none", func(t *testing.T) {
assert.Nil(t, circle.IntersectionCircle(Circ(Pt(20.0, 0.0), 5.0)))
assert.Nil(t, circle.IntersectionCircle(Circ(Pt(1.0, 0.0), 1.0)))
assert.Nil(t, circle.IntersectionCircle(Circ(Pt(0.0, 0.0), 3.0)))
assert.Nil(t, circle.IntersectionCircle(circle))
})
t.Run("float treats centers within Epsilon as coincident", func(t *testing.T) {
assert.Nil(t, circle.IntersectionCircle(Circ(Pt(1e-7, 0.0), 5.0)))
assert.Nil(t, circle.IntersectionCircle(Circ(Pt(0.0, -1e-7), 3.0)))
})
t.Run("float is tolerant at a tangent", func(t *testing.T) {
assert.Length(t, circle.IntersectionCircle(Circ(Pt(8.0+Delta/2, 0.0), 3.0)), 1)
assert.Nil(t, circle.IntersectionCircle(Circ(Pt(8.0+2*Delta, 0.0), 3.0)))
})
t.Run("a tangent exactly Delta outside is judged like Intersects, on both sides", func(t *testing.T) {
for _, other := range []Circle[float64]{Circ(Pt(8.0+Delta, 0.0), 3.0), Circ(Pt(2.0+Delta, 0.0), 3.0)} {
assert.True(t, circle.IntersectsCircle(other))
AssertVertices(t, circle.IntersectionCircle(other), []Point[float64]{Pt(5.0, 0.0)})
}
})
t.Run("a tangent within the tolerance lands on both boundaries", func(t *testing.T) {
a, b := Circ(Pt(0.0, 0.0), 6.0), Circ(Pt(2.0000005, 0.0), 4.0)
points := a.IntersectionCircle(b)
assert.Length(t, points, 1)
for _, p := range points {
assert.True(t, a.touchesSquared(a.Center.DistanceSquaredTo(p), b.magnitude()), "on a")
assert.True(t, b.touchesSquared(b.Center.DistanceSquaredTo(p), a.magnitude()), "on b")
}
})
t.Run("int rounds the points", func(t *testing.T) {
AssertVertices(t, Circ(Pt(0, 0), 5).IntersectionCircle(Circ(Pt(6, 0), 5)), []Point[int]{Pt(3, 4), Pt(3, -4)})
AssertVertices(t, Circ(Pt(0, 0), 2).IntersectionCircle(Circ(Pt(3, 0), 2)), []Point[int]{Pt(2, 1), Pt(2, -1)})
})
t.Run("points lie on both circles and mirror Intersects", func(t *testing.T) {
for _, a := range circleFixtures {
for _, b := range circleFixtures {
points := a.IntersectionCircle(b)
assert.True(t, len(points) <= 2, fmt.Sprintf("%s → %s: at most two points: ", a, b))
if len(points) > 0 {
assert.True(t, a.IntersectsCircle(b), fmt.Sprintf("%s → %s: ", a, b))
}
for _, p := range points {
assert.True(t, a.touchesSquared(a.Center.DistanceSquaredTo(p), b.magnitude()), fmt.Sprintf("%s → %s: %s on a: ", a, b, p))
assert.True(t, b.touchesSquared(b.Center.DistanceSquaredTo(p), a.magnitude()), fmt.Sprintf("%s → %s: %s on b: ", a, b, p))
}
}
}
})
t.Run("far from the origin float32 points lie on both circles", func(t *testing.T) {
for _, offset := range farOffsets {
for _, a := range circleFixtures {
for _, b := range circleFixtures {
a, b := a.Cast[float32]().Translate(offset), b.Cast[float32]().Translate(offset)
for _, p := range a.IntersectionCircle(b) {
assert.True(t, a.touchesSquared(a.centerDistanceSquared(p), b.magnitude()), fmt.Sprintf("%s → %s: %s on a: ", a, b, p))
assert.True(t, b.touchesSquared(b.centerDistanceSquared(p), a.magnitude()), fmt.Sprintf("%s → %s: %s on b: ", a, b, p))
}
}
}
}
})
}
func BenchmarkCircle_IntersectionCircle(b *testing.B) {
circle, other := Circ(Pt(0.0, 0.0), 5.0), Circ(Pt(6.0, 0.0), 5.0)
for b.Loop() {
_ = circle.IntersectionCircle(other)
}
}
func TestCircle_AppendIntersectionCircle(t *testing.T) {
circle := Circ(Pt(0, 0), 5)
t.Run("appends after the points in dst, comparing and ordering only its own", func(t *testing.T) {
AssertVertices(t, circle.AppendIntersectionCircle([]Point[int]{Pt(3, 4)}, Circ(Pt(6, 0), 5)), []Point[int]{Pt(3, 4), Pt(3, 4), Pt(3, -4)})
})
t.Run("none leaves dst as it is", func(t *testing.T) {
AssertVertices(t, circle.AppendIntersectionCircle([]Point[int]{Pt(9, 9)}, Circ(Pt(20, 0), 5)), []Point[int]{Pt(9, 9)})
assert.Nil(t, circle.AppendIntersectionCircle(nil, Circ(Pt(20, 0), 5)))
})
t.Run("a buffer with room allocates nothing", func(t *testing.T) {
buffer := make([]Point[int], 0, 2)
AssertNumber(t, testing.AllocsPerRun(100, func() {
sinkPoints = circle.AppendIntersectionCircle(buffer[:0], Circ(Pt(6, 0), 5))
}), 0)
})
t.Run("matches IntersectionCircle after the points in dst", func(t *testing.T) {
for _, c := range circleFixtures {
for _, other := range circleFixtures {
AssertVertices(t, c.AppendIntersectionCircle(bufferWith(prefixPoint), other), append([]Point[float64]{prefixPoint}, c.IntersectionCircle(other)...), fmt.Sprintf("%s → %s: ", c, other))
}
}
})
}
func FuzzCircle_IntersectionCircle(f *testing.F) {
f.Add(0.0, 0.0, 1.0, 2.0, 0.0, 1.0)
f.Add(0.0, 0.0, 1.0, 2.0+Delta/2, 0.0, 1.0)
f.Add(0.0, 0.0, 2.0, 0.0, 0.0, 1.0)
f.Add(0.0, 0.0, 6.0, 2.0000005, 0.0, 4.0)
f.Fuzz(func(t *testing.T, x1, y1, r1, x2, y2, r2 float64) {
for _, v := range []float64{x1, y1, r1, x2, y2, r2} {
if math.IsNaN(v) || math.Abs(v) > 1e3 {
t.Skip()
}
}
a, b := Circ(Pt(x1, y1), r1), Circ(Pt(x2, y2), r2)
points := a.IntersectionCircle(b)
assert.True(t, len(points) <= 2, fmt.Sprintf("%s → %s: at most two crossings, got %d: ", a, b, len(points)))
for _, p := range points {
assert.True(t, a.touchesSquared(a.Center.Float().DistanceSquaredTo(p.Float()), b.magnitude()), fmt.Sprintf("%s → %s: %s on the first: ", a, b, p))
assert.True(t, b.touchesSquared(b.Center.Float().DistanceSquaredTo(p.Float()), a.magnitude()), fmt.Sprintf("%s → %s: %s on the second: ", a, b, p))
}
if len(points) == 2 {
assert.True(t, !points[0].Equal(points[1]), fmt.Sprintf("%s → %s: two distinct points: ", a, b))
}
if len(points) > 0 {
assert.True(t, a.IntersectsCircle(b), fmt.Sprintf("%s → %s: points imply intersects: ", a, b))
}
})
}
func TestCircle_IntersectsSegment(t *testing.T) {
circle := Circ(Pt(0.0, 0.0), 1.0)
t.Run("passing through", func(t *testing.T) {
assert.True(t, circle.IntersectsSegment(Seg(Pt(-2.0, 0.0), Pt(2.0, 0.0))))
})
t.Run("apart", func(t *testing.T) {
assert.False(t, circle.IntersectsSegment(Seg(Pt(-2.0, 2.0), Pt(2.0, 2.0))))
assert.False(t, circle.IntersectsSegment(Seg(Pt(2.0, 0.0), Pt(3.0, 0.0))))
})
t.Run("tangent counts", func(t *testing.T) {
assert.True(t, circle.IntersectsSegment(Seg(Pt(-2.0, 1.0), Pt(2.0, 1.0))))
assert.False(t, circle.IntersectsSegment(Seg(Pt(-2.0, 1.0+2*Delta), Pt(2.0, 1.0+2*Delta))))
})
t.Run("an endpoint inside counts", func(t *testing.T) {
assert.True(t, circle.IntersectsSegment(Seg(Pt(0.5, 0.0), Pt(5.0, 0.0))))
})
t.Run("a segment inside counts", func(t *testing.T) {
assert.True(t, circle.IntersectsSegment(Seg(Pt(-0.5, 0.0), Pt(0.5, 0.0))))
})
}
func TestCircle_IntersectionSegment(t *testing.T) {
circle := Circ(Pt(0.0, 0.0), 1.0)
t.Run("passing through gives both crossings from Start to End", func(t *testing.T) {
AssertVertices(t, circle.IntersectionSegment(Seg(Pt(-2.0, 0.0), Pt(2.0, 0.0))), []Point[float64]{Pt(-1.0, 0.0), Pt(1.0, 0.0)})
AssertVertices(t, circle.IntersectionSegment(Seg(Pt(2.0, 0.0), Pt(-2.0, 0.0))), []Point[float64]{Pt(1.0, 0.0), Pt(-1.0, 0.0)})
})
t.Run("ending inside gives one crossing", func(t *testing.T) {
AssertVertices(t, circle.IntersectionSegment(Seg(Pt(-2.0, 0.0), Pt(0.0, 0.0))), []Point[float64]{Pt(-1.0, 0.0)})
AssertVertices(t, circle.IntersectionSegment(Seg(Pt(0.5, 0.0), Pt(5.0, 0.0))), []Point[float64]{Pt(1.0, 0.0)})
})
t.Run("allocates once for the result and not at all for none", func(t *testing.T) {
unit := Circ(Pt(0, 0), 5)
through, apart := Seg(Pt(-10, 0), Pt(10, 0)), Seg(Pt(-10, 9), Pt(10, 9))
AssertNumber(t, testing.AllocsPerRun(100, func() {
sinkPoints = unit.IntersectionSegment(through)
}), 1)
AssertNumber(t, testing.AllocsPerRun(100, func() {
sinkPoints = unit.IntersectionSegment(apart)
}), 0)
})
t.Run("tangent gives one point", func(t *testing.T) {
AssertVertices(t, circle.IntersectionSegment(Seg(Pt(-2.0, 1.0), Pt(2.0, 1.0))), []Point[float64]{Pt(0.0, 1.0)})
AssertVertices(t, circle.IntersectionSegment(Seg(Pt(-2.0, 1.0+Delta/2), Pt(2.0, 1.0+Delta/2))), []Point[float64]{Pt(0.0, 1.0+Delta/2)})
assert.Nil(t, circle.IntersectionSegment(Seg(Pt(-2.0, 1.0+2*Delta), Pt(2.0, 1.0+2*Delta))))
})
t.Run("a chord within the tolerance band of the gap keeps both ends", func(t *testing.T) {
height := 1.0 - Delta/2
half := math.Sqrt(1 - height*height)
AssertVertices(t, circle.IntersectionSegment(Seg(Pt(-1.0, height), Pt(1.0, height))), []Point[float64]{Pt(-half, height), Pt(half, height)})
})
t.Run("a chord shorter than Delta is a tangent", func(t *testing.T) {
height := math.Sqrt(1 - Delta*Delta/16)
AssertVertices(t, circle.IntersectionSegment(Seg(Pt(-1.0, height), Pt(1.0, height))), []Point[float64]{Pt(0.0, height)})
})
t.Run("a segment within the tolerance with both ends on the boundary gives one point", func(t *testing.T) {
AssertVertices(t, circle.IntersectionSegment(Seg(Pt(1.0, 0.0), Pt(1.0, Delta/10))), []Point[float64]{Pt(1.0, 0.0)})
})
t.Run("a tangent beyond the segment is missed", func(t *testing.T) {
assert.Nil(t, circle.IntersectionSegment(Seg(Pt(1.0, 1.0), Pt(2.0, 1.0))))
})
t.Run("a shallow touch is decided on the endpoint distance, like IntersectsSegment", func(t *testing.T) {
shallow := Seg(Pt(-1.0, 1.0+Delta/2), Pt(-0.0005, 1.0+Delta/2))
assert.True(t, circle.IntersectsSegment(shallow))
AssertVertices(t, circle.IntersectionSegment(shallow), []Point[float64]{shallow.End})
AssertVertices(t, circle.IntersectionSegment(shallow.Reverse()), []Point[float64]{shallow.End})
})
t.Run("an interior graze exactly Delta outside is judged like IntersectsSegment", func(t *testing.T) {
s, c := Seg(Pt(-1.0, 1.000001), Pt(1.0, 1.000001)), Circ(Pt(0.0, 0.0), 1.0)
assert.Equal(t, len(c.IntersectionSegment(s)) > 0, c.IntersectsSegment(s))
for _, y := range []float64{1.0000009, 1.0000011, 1.0000015} {
s := Seg(Pt(-1.0, y), Pt(1.0, y))
assert.Equal(t, len(c.IntersectionSegment(s)) > 0, c.IntersectsSegment(s), s.String())
}
})
t.Run("an endpoint exactly Delta outside is judged like IntersectsSegment", func(t *testing.T) {
s, c := Seg(Pt(-1.0, 2.000001), Pt(-882.0315, 56.11111116666667)), Circ(Pt(-1.0, 0.0), 2.0)
assert.Equal(t, len(c.IntersectionSegment(s)) > 0, c.IntersectsSegment(s))
})
t.Run("an endpoint on the boundary is counted once with its crossing", func(t *testing.T) {
AssertVertices(t, circle.IntersectionSegment(Seg(Pt(-1.0, 0.0), Pt(2.0, 0.0))), []Point[float64]{Pt(-1.0, 0.0), Pt(1.0, 0.0)})
AssertVertices(t, circle.IntersectionSegment(Seg(Pt(-2.0, 0.0), Pt(1.0, 0.0))), []Point[float64]{Pt(-1.0, 0.0), Pt(1.0, 0.0)})
})
t.Run("an endpoint on the boundary replaces the crossing nearest to it", func(t *testing.T) {
s, c := Seg(Pt(-1.0, 2.000001), Pt(-877.0315, 0.11111116666666668)), Circ(Pt(-1.0, 0.0), 2.0)
points := c.IntersectionSegment(s)
assert.Length(t, points, 2)
AssertPoint(t, points[0], s.Start)
})
t.Run("a tangent segment with both ends on the boundary gives its ends", func(t *testing.T) {
grazing := Seg(Pt(-0.0003, 1.0), Pt(0.0003, 1.0))
AssertVertices(t, circle.IntersectionSegment(grazing), []Point[float64]{grazing.Start, grazing.End})
})
t.Run("apart and inside give none", func(t *testing.T) {
assert.Nil(t, circle.IntersectionSegment(Seg(Pt(-2.0, 2.0), Pt(2.0, 2.0))))
assert.Nil(t, circle.IntersectionSegment(Seg(Pt(2.0, 0.0), Pt(3.0, 0.0))))
assert.Nil(t, circle.IntersectionSegment(Seg(Pt(-0.5, 0.0), Pt(0.5, 0.0))))
})
t.Run("a degenerate segment is a point on the boundary or nothing", func(t *testing.T) {
AssertVertices(t, circle.IntersectionSegment(Seg(Pt(1.0, 0.0), Pt(1.0, 0.0))), []Point[float64]{Pt(1.0, 0.0)})
assert.Nil(t, circle.IntersectionSegment(Seg(Pt(0.5, 0.0), Pt(0.5, 0.0))))
})
t.Run("int rounds the crossings", func(t *testing.T) {
AssertVertices(t, Circ(Pt(0, 0), 5).IntersectionSegment(Seg(Pt(-5, -5), Pt(5, 5))), []Point[int]{Pt(-4, -4), Pt(4, 4)})
})
t.Run("every point lies on the segment and the boundary, and exists where IntersectsSegment holds", func(t *testing.T) {
for _, s := range segmentFixtures {
for _, c := range circleFixtures {
points := c.IntersectionSegment(s)
assert.True(t, len(points) <= 2, fmt.Sprintf("%s → %s: at most two crossings: ", s, c))
for _, p := range points {
assert.True(t, s.Contains(p), fmt.Sprintf("%s → %s: %s on the segment: ", s, c, p))
assert.True(t, c.touchesSquared(c.Center.DistanceSquaredTo(p), s.magnitude()), fmt.Sprintf("%s → %s: %s on the boundary: ", s, c, p))
}
if len(points) > 0 {
assert.True(t, c.IntersectsSegment(s), fmt.Sprintf("%s → %s: ", s, c))
}
}
}
})
t.Run("far from the origin every float32 point lies on the segment and the boundary", func(t *testing.T) {
for _, offset := range farOffsets {
for _, s := range segmentFixtures {
for _, c := range circleFixtures {
s, c := s.Cast[float32]().Translate(offset), c.Cast[float32]().Translate(offset)
points := c.IntersectionSegment(s)
assert.True(t, len(points) <= 2, fmt.Sprintf("%s → %s: at most two crossings: ", s, c))
for _, p := range points {
assert.True(t, s.Contains(p), fmt.Sprintf("%s → %s: %s on the segment: ", s, c, p))
assert.True(t, c.touchesSquared(c.centerDistanceSquared(p), s.magnitude()), fmt.Sprintf("%s → %s: %s on the boundary: ", s, c, p))
}
assert.True(t, len(points) == 0 || c.IntersectsSegment(s), fmt.Sprintf("%s → %s: ", s, c))
}
}
}
})
}
func TestCircle_AppendIntersectionSegment(t *testing.T) {
circle := Circ(Pt(0, 0), 5)
through := Seg(Pt(-10, 0), Pt(10, 0))
t.Run("appends after the points in dst, comparing and ordering only its own", func(t *testing.T) {
AssertVertices(t, circle.AppendIntersectionSegment([]Point[int]{Pt(5, 0)}, through), []Point[int]{Pt(5, 0), Pt(-5, 0), Pt(5, 0)})
})
t.Run("none leaves dst as it is", func(t *testing.T) {
AssertVertices(t, circle.AppendIntersectionSegment([]Point[int]{Pt(9, 9)}, Seg(Pt(-10, 9), Pt(10, 9))), []Point[int]{Pt(9, 9)})
assert.Nil(t, circle.AppendIntersectionSegment(nil, Seg(Pt(-10, 9), Pt(10, 9))))
})
t.Run("a buffer with room allocates nothing", func(t *testing.T) {
buffer := make([]Point[int], 0, 2)
AssertNumber(t, testing.AllocsPerRun(100, func() {
sinkPoints = circle.AppendIntersectionSegment(buffer[:0], through)
}), 0)
})
t.Run("matches IntersectionSegment after the points in dst", func(t *testing.T) {
for _, c := range circleFixtures {
for _, s := range segmentFixtures {
AssertVertices(t, c.AppendIntersectionSegment(bufferWith(prefixPoint), s), append([]Point[float64]{prefixPoint}, c.IntersectionSegment(s)...), fmt.Sprintf("%s → %s: ", c, s))
}
}
})
}
func FuzzCircle_IntersectionSegment(f *testing.F) {
f.Add(-2.0, 0.0, 2.0, 0.0, 0.0, 0.0, 1.0)
f.Add(-1.0, 1.0+Delta/2, -0.0005, 1.0+Delta/2, 0.0, 0.0, 1.0)
f.Add(-0.5, 0.0, 0.5, 0.0, 0.0, 0.0, 1.0)
f.Add(1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0)
f.Fuzz(func(t *testing.T, x1, y1, x2, y2, cx, cy, r float64) {
for _, v := range []float64{x1, y1, x2, y2, cx, cy, r} {
if math.IsNaN(v) || math.Abs(v) > 1e3 {
t.Skip()
}
}
s, c := Seg(Pt(x1, y1), Pt(x2, y2)), Circ(Pt(cx, cy), r)
points := c.IntersectionSegment(s)
assert.True(t, len(points) <= 2, fmt.Sprintf("%s → %s: at most two crossings, got %d: ", s, c, len(points)))
for _, p := range points {
assert.True(t, s.Contains(p), fmt.Sprintf("%s → %s: %s on the segment: ", s, c, p))
assert.True(t, c.touchesSquared(c.Center.Float().DistanceSquaredTo(p.Float()), s.magnitude()), fmt.Sprintf("%s → %s: %s on the boundary: ", s, c, p))
}
if inside := c.Contains(s.Start) && c.Contains(s.End); !inside {
assert.Equal(t, len(points) > 0, c.IntersectsSegment(s), fmt.Sprintf("%s → %s: ", s, c))
}
})
}
func TestCircle_IntersectsRay(t *testing.T) {
circle := Circ(Pt(0.0, 0.0), 1.0)
t.Run("passing through", func(t *testing.T) {
assert.True(t, circle.IntersectsRay(RayAlong(Pt(-2.0, 0.0), Vec(1.0, 0.0))))
})
t.Run("pointing away", func(t *testing.T) {
assert.False(t, circle.IntersectsRay(RayAlong(Pt(-2.0, 0.0), Vec(-1.0, 0.0))))
})
t.Run("tangent counts", func(t *testing.T) {
assert.True(t, circle.IntersectsRay(RayAlong(Pt(-2.0, 1.0), Vec(1.0, 0.0))))
assert.False(t, circle.IntersectsRay(RayAlong(Pt(-2.0, 1.0+2*Delta), Vec(1.0, 0.0))))
})
t.Run("a tangent behind the origin is missed", func(t *testing.T) {
assert.False(t, circle.IntersectsRay(RayAlong(Pt(1.5, 1.0), Vec(1.0, 0.0))))
})
t.Run("the origin inside counts", func(t *testing.T) {
assert.True(t, circle.IntersectsRay(RayAlong(Pt(0.5, 0.0), Vec(0.0, -3.0))))
})
t.Run("a zero direction is its origin", func(t *testing.T) {
assert.True(t, circle.IntersectsRay(RayAlong(Pt(0.5, 0.0), Vec(0.0, 0.0))))
assert.False(t, circle.IntersectsRay(RayAlong(Pt(2.0, 0.0), Vec(0.0, 0.0))))
})
t.Run("matches a segment reaching past the circle along the ray", func(t *testing.T) {
for _, c := range circleFixtures {
for _, r := range rayFixtures {
assert.Equal(t, c.IntersectsRay(r), c.IntersectsSegment(far(r)), fmt.Sprintf("%s → %s: ", c, r))
}
}
})
}
func TestCircle_IntersectionRay(t *testing.T) {
circle := Circ(Pt(0, 0), 5)
t.Run("passing through gives both crossings from Origin on", func(t *testing.T) {
AssertVertices(t, circle.IntersectionRay(RayAlong(Pt(-10, 0), Vec(1, 0))), []Point[int]{Pt(-5, 0), Pt(5, 0)})
AssertVertices(t, circle.IntersectionRay(RayAlong(Pt(10, 0), Vec(-2, 0))), []Point[int]{Pt(5, 0), Pt(-5, 0)})
})
t.Run("starting inside gives the exit", func(t *testing.T) {
AssertVertices(t, circle.IntersectionRay(RayAlong(Pt(0, 0), Vec(0, 1))), []Point[int]{Pt(0, 5)})
})
t.Run("tangent gives one point", func(t *testing.T) {
AssertVertices(t, circle.IntersectionRay(RayAlong(Pt(-10, 5), Vec(1, 0))), []Point[int]{Pt(0, 5)})
})
t.Run("pointing away gives none", func(t *testing.T) {
assert.Nil(t, circle.IntersectionRay(RayAlong(Pt(-10, 0), Vec(-1, 0))))
})
t.Run("allocates once for the result and not at all for none", func(t *testing.T) {
through, away := RayAlong(Pt(-10, 0), Vec(1, 0)), RayAlong(Pt(-10, 0), Vec(-1, 0))
AssertNumber(t, testing.AllocsPerRun(100, func() {
sinkPoints = circle.IntersectionRay(through)
}), 1)
AssertNumber(t, testing.AllocsPerRun(100, func() {
sinkPoints = circle.IntersectionRay(away)
}), 0)
})
t.Run("every point lies on the ray and the boundary, and exists exactly where IntersectsRay holds for a ray with a direction", func(t *testing.T) {
for _, c := range circleFixtures {
for _, r := range rayFixtures {
points := c.IntersectionRay(r)
message := fmt.Sprintf("%s → %s: ", c, r)
for _, point := range points {
assert.True(t, r.Contains(point), message+point.String()+" on the ray: ")
assert.True(t, c.touchesSquared(c.centerDistanceSquared(point), point.magnitude()), message+point.String()+" on the boundary: ")
}
assert.True(t, len(points) <= 2, message)
if r.Direction.hasDirection() {
assert.Equal(t, len(points) > 0, c.IntersectsRay(r), message)
}
}
}
})
}
func TestCircle_AppendIntersectionRay(t *testing.T) {
circle := Circ(Pt(0, 0), 5)
through := RayAlong(Pt(-10, 0), Vec(1, 0))
t.Run("appends after the points in dst, comparing and ordering only its own", func(t *testing.T) {
AssertVertices(t, circle.AppendIntersectionRay([]Point[int]{Pt(5, 0)}, through), []Point[int]{Pt(5, 0), Pt(-5, 0), Pt(5, 0)})
})
t.Run("none leaves dst as it is", func(t *testing.T) {
AssertVertices(t, circle.AppendIntersectionRay([]Point[int]{Pt(9, 9)}, RayAlong(Pt(-10, 0), Vec(-1, 0))), []Point[int]{Pt(9, 9)})
assert.Nil(t, circle.AppendIntersectionRay(nil, RayAlong(Pt(-10, 0), Vec(-1, 0))))
})
t.Run("a buffer with room allocates nothing", func(t *testing.T) {
buffer := make([]Point[int], 0, 2)
AssertNumber(t, testing.AllocsPerRun(100, func() {
sinkPoints = circle.AppendIntersectionRay(buffer[:0], through)
}), 0)
})
t.Run("matches IntersectionRay after the points in dst", func(t *testing.T) {
for _, c := range circleFixtures {
for _, r := range rayFixtures {
AssertVertices(t, c.AppendIntersectionRay(bufferWith(prefixPoint), r), append([]Point[float64]{prefixPoint}, c.IntersectionRay(r)...), fmt.Sprintf("%s → %s: ", c, r))
}
}
})
}
func TestCircle_IntersectsPolygon(t *testing.T) {
square := Pol(squareVertices())
t.Run("center inside", func(t *testing.T) {
assert.True(t, Circ(Pt(1, 1), 5).IntersectsPolygon(square))
})
t.Run("an edge within the radius", func(t *testing.T) {
assert.True(t, Circ(Pt(3, 1), 1).IntersectsPolygon(square))
})
t.Run("apart", func(t *testing.T) {
assert.False(t, Circ(Pt(4, 1), 1).IntersectsPolygon(square))
assert.False(t, Circ(Pt(3, 3), 1).IntersectsPolygon(square))
})
t.Run("an empty polygon intersects nothing", func(t *testing.T) {
assert.False(t, Circ(Pt(0, 0), 1).IntersectsPolygon(Pol[int](nil)))
})
t.Run("matches the circle test on the polygon edges", func(t *testing.T) {
for _, p := range polygonFixtures() {
for _, c := range circleFixtures {
expected := p.Contains(c.Center) || slices.ContainsFunc(slices.Collect(p.Edges()), func(edge Segment[float64]) bool {
return c.IntersectsSegment(edge)
})
assert.Equal(t, c.IntersectsPolygon(p), expected, fmt.Sprintf("%s → %s: ", p, c))
}
}
})
}
func TestCircle_IntersectsRectangle(t *testing.T) {
rectangle := Rect(Pt(0.0, 0.0), Sz(200.0, 100.0))
t.Run("overlapping", func(t *testing.T) {
assert.True(t, Circ(Pt(150.0, 0.0), 60.0).IntersectsRectangle(rectangle))
assert.True(t, Circ(Pt(110.0, 80.0), 60.0).IntersectsRectangle(rectangle))
})
t.Run("apart", func(t *testing.T) {
assert.False(t, Circ(Pt(150.0, 0.0), 40.0).IntersectsRectangle(rectangle))
})