-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregular_polygon_test.go
More file actions
1282 lines (1147 loc) · 51.8 KB
/
Copy pathregular_polygon_test.go
File metadata and controls
1282 lines (1147 loc) · 51.8 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 TestRegularPolygon_Constructor(t *testing.T) {
t.Run("int", func(t *testing.T) {
AssertRegularPolygon(t, RegPol(Pt(0, 0), Sz(2, 2), 4, 0), RegularPolygon[int]{Center: Pt(0, 0), Size: Sz(2, 2), N: 4, Angle: 0})
})
t.Run("float", func(t *testing.T) {
AssertRegularPolygon(t, RegPol(Pt(0.5, -1.25), Sz(2.5, 3.75), 6, Pi/3), RegularPolygon[float64]{Center: Pt(0.5, -1.25), Size: Sz(2.5, 3.75), N: 6, Angle: Pi / 3})
})
t.Run("a negative size is taken absolute", func(t *testing.T) {
AssertRegularPolygon(t, RegPol(Pt(0, 0), Sz(-2, 3), 4, 0), RegPol(Pt(0, 0), Sz(2, 3), 4, 0))
AssertRegularPolygon(t, Square(Pt(0.0, 0.0), Sz(-2.0, -3.0), OrientationPointyTop), Square(Pt(0.0, 0.0), Sz(2.0, 3.0), OrientationPointyTop))
})
}
func TestRegularPolygonWithOrientation(t *testing.T) {
center, size := Pt(0, 0), Sz(10, 10)
pointy := RegularPolygonWithOrientation(center, size, 6, OrientationPointyTop)
flat := RegularPolygonWithOrientation(center, size, 6, OrientationFlatTop)
t.Run("carries the orientation angle", func(t *testing.T) {
AssertRegularPolygon(t, pointy, RegPol(center, size, 6, RegularPolygonOrientationAngle(6, OrientationPointyTop)))
AssertRegularPolygon(t, flat, RegPol(center, size, 6, RegularPolygonOrientationAngle(6, OrientationFlatTop)))
assert.NotEqual(t, pointy.Angle, flat.Angle)
})
t.Run("pointy top starts above the center", func(t *testing.T) {
assert.True(t, slices.Collect(pointy.Vertices())[0].Y < center.Y)
})
t.Run("flat top starts left of and above the center", func(t *testing.T) {
first := slices.Collect(flat.Vertices())[0]
assert.True(t, first.X < center.X)
assert.True(t, first.Y < center.Y)
})
}
func TestTriangle(t *testing.T) {
triangle := Triangle(Pt(1, -1), Sz(3, 3), OrientationPointyTop)
t.Run("has three sides at the orientation angle", func(t *testing.T) {
AssertRegularPolygon(t, triangle, RegPol(Pt(1, -1), Sz(3, 3), 3, RegularPolygonOrientationAngle(3, OrientationPointyTop)))
})
t.Run("integer vertices round after scaling", func(t *testing.T) {
// vertices 1 and 2 land within one unit of the exact (3.598, 0.5) and (-1.598, 0.5);
// their Y of 0.5 falls just below the .5 tie in float64 and rounds down to 0
AssertVertices(t, slices.Collect(triangle.Vertices()), []Point[int]{Pt(1, -4), Pt(4, 0), Pt(-2, 0)})
})
}
func TestSquare(t *testing.T) {
square := Square(Pt(50.0, 50.0), Sz(100.0, 100.0), OrientationPointyTop)
t.Run("has four sides at the orientation angle", func(t *testing.T) {
AssertRegularPolygon(t, square, RegPol(Pt(50.0, 50.0), Sz(100.0, 100.0), 4, RegularPolygonOrientationAngle(4, OrientationPointyTop)))
})
t.Run("pointy top starts at the visual top and winds to the right", func(t *testing.T) {
AssertVertices(t, slices.Collect(square.Vertices()), []Point[float64]{Pt(50.0, -50.0), Pt(150.0, 50.0), Pt(50.0, 150.0), Pt(-50.0, 50.0)})
})
}
func TestHexagon(t *testing.T) {
// float64 avoids the int-rounding collapse where sin(±π/6) ≈ 0.4999 would truncate to 0
hexagon := Hexagon(Pt(0.0, 0.0), Sz(10.0, 10.0), OrientationPointyTop)
t.Run("has six sides at the orientation angle", func(t *testing.T) {
AssertRegularPolygon(t, hexagon, RegPol(Pt(0.0, 0.0), Sz(10.0, 10.0), 6, RegularPolygonOrientationAngle(6, OrientationPointyTop)))
})
t.Run("pointy top has vertices at top and bottom and flat sides left and right", func(t *testing.T) {
AssertVertices(t, slices.Collect(hexagon.Vertices()), []Point[float64]{
Pt(0.0, -10.0),
Pt(5*Sqrt3, -5.0),
Pt(5*Sqrt3, 5.0),
Pt(0.0, 10.0),
Pt(-5*Sqrt3, 5.0),
Pt(-5*Sqrt3, -5.0),
})
})
}
func TestRegularPolygon_Anchor(t *testing.T) {
t.Run("a flat-top hexagon anchors the top edge midpoint", func(t *testing.T) {
hex := Hexagon(Pt(0.0, 0.0), SzU(10.0), OrientationFlatTop)
AssertPoint(t, hex.Anchor(Top), Pt(0.0, -10*math.Cos(Pi/6)))
AssertPoint(t, hex.Anchor(Bottom), Pt(0.0, 10*math.Cos(Pi/6)))
AssertPoint(t, hex.Anchor(Right), Pt(10.0, 0.0))
})
t.Run("a pointy-top hexagon anchors the top vertex", func(t *testing.T) {
hex := Hexagon(Pt(0.0, 0.0), SzU(10.0), OrientationPointyTop)
AssertPoint(t, hex.Anchor(Top), Pt(0.0, -10.0))
AssertPoint(t, hex.Anchor(Right), Pt(10*math.Cos(Pi/6), 0.0))
})
t.Run("a diagonal leaves through the edge on the diagonal", func(t *testing.T) {
diamond := RegPol(Pt(0.0, 0.0), Sz(10.0, 4.0), 4, 0)
reach := 1 / (1/10.0 + 1/4.0)
AssertPoint(t, diamond.Anchor(DirectionDownRight), Pt(reach, reach))
AssertPoint(t, diamond.Anchor(DirectionUpLeft), Pt(-reach, -reach))
})
t.Run("the direction is taken in the world, so a turn moves the anchor along the boundary", func(t *testing.T) {
square := RegPol(Pt(0.0, 0.0), SzU(10.0), 4, 0)
AssertPoint(t, square.Anchor(Right), Pt(10.0, 0.0))
AssertPoint(t, square.Rotate(Pi/4).Anchor(Right), Pt(10*OneOverSqrt2, 0.0))
AssertPoint(t, square.Rotate(Pi/2).Anchor(Right), Pt(10.0, 0.0))
})
t.Run("int lands on the rounded edge", func(t *testing.T) {
hex := Hexagon(Pt(0, 0), SzU(10), OrientationPointyTop)
AssertPoint(t, hex.Anchor(Top), Pt(0, -10))
assert.True(t, hex.Contains(hex.Anchor(DirectionDownRight)))
})
t.Run("none is the center", func(t *testing.T) {
AssertPoint(t, Hexagon(Pt(1.0, 2.0), SzU(10.0), OrientationFlatTop).Anchor(DirectionNone), Pt(1.0, 2.0))
})
t.Run("fewer than three vertices anchor at the center", func(t *testing.T) {
AssertPoint(t, RegPol(Pt(1.0, 2.0), SzU(10.0), 2, 0).Anchor(Right), Pt(1.0, 2.0))
AssertPoint(t, RegPol(Pt(1.0, 2.0), SzU(10.0), 0, 0).Anchor(Right), Pt(1.0, 2.0))
})
t.Run("a semi-axis along the ray anchors at the center", func(t *testing.T) {
AssertPoint(t, RegPol(Pt(1.0, 2.0), Sz(10.0, 0.0), 4, 0).Anchor(Right), Pt(1.0, 2.0))
AssertPoint(t, RegPol(Pt(1.0, 2.0), Sz(10.0, 0.0), 4, 0).Anchor(Top), Pt(1.0, 2.0))
})
}
func TestRegularPolygon_Vertices(t *testing.T) {
t.Run("fewer than one side yields nothing", func(t *testing.T) {
assert.Nil(t, slices.Collect(RegPol(Pt(0, 0), Sz(1, 1), 0, 0).Vertices()))
assert.Nil(t, slices.Collect(RegPol(Pt(0.0, 0.0), Sz(1.0, 1.0), -3, 0).Vertices()))
})
t.Run("int", func(t *testing.T) {
AssertVertices(t, slices.Collect(RegPol(Pt(0, 0), Sz(1, 1), 4, 0).Vertices()), []Point[int]{
Pt(1, 0),
Pt(0, 1),
Pt(-1, 0),
Pt(0, -1),
})
})
t.Run("non-square size stretches the ellipse", func(t *testing.T) {
AssertVertices(t, slices.Collect(RegPol(Pt(0, 0), Sz(2, 3), 4, 0).Vertices()), []Point[int]{
Pt(2, 0),
Pt(0, 3),
Pt(-2, 0),
Pt(0, -3),
})
})
t.Run("float", func(t *testing.T) {
AssertVertices(t, slices.Collect(RegPol(Pt(0.0, 0.0), Sz(2.0, 3.0), 6, 0).Vertices()), []Point[float64]{
Pt(2.0, 0.0),
Pt(1.0, 1.5*Sqrt3),
Pt(-1.0, 1.5*Sqrt3),
Pt(-2.0, 0.0),
Pt(-1.0, -1.5*Sqrt3),
Pt(1.0, -1.5*Sqrt3),
})
})
t.Run("stops where the caller breaks", func(t *testing.T) {
for vertex := range RegPol(Pt(0, 0), Sz(1, 1), 4, 0).Vertices() {
AssertPoint(t, vertex, Pt(1, 0))
break
}
})
t.Run("ranging allocates nothing", func(t *testing.T) {
for _, rp := range regularPolygonFixtures {
AssertNumber(t, testing.AllocsPerRun(100, func() {
for vertex := range rp.Vertices() {
sinkBool = vertex.IsZero()
}
}), 0, fmt.Sprintf("%s: ", rp))
}
})
}
func TestRegularPolygon_Edges(t *testing.T) {
t.Run("closes back to the first vertex", func(t *testing.T) {
edges := slices.Collect(RegPol(Pt(0, 0), Sz(1, 1), 4, 0).Edges())
assert.Equal(t, len(edges), 4)
AssertSegment(t, edges[0], Seg(Pt(1, 0), Pt(0, 1)))
AssertSegment(t, edges[3], Seg(Pt(0, -1), Pt(1, 0)))
})
t.Run("single vertex is one zero-length edge", func(t *testing.T) {
edges := slices.Collect(RegPol(Pt(0.0, 0.0), Sz(1.0, 1.0), 1, 0).Edges())
assert.Equal(t, len(edges), 1)
AssertSegment(t, edges[0], Seg(Pt(1.0, 0.0), Pt(1.0, 0.0)))
})
t.Run("fewer than one side yields nothing", func(t *testing.T) {
assert.Nil(t, slices.Collect(RegPol(Pt(0, 0), Sz(1, 1), 0, 0).Edges()))
assert.Nil(t, slices.Collect(RegPol(Pt(0.0, 0.0), Sz(1.0, 1.0), -3, 0).Edges()))
})
t.Run("stops where the caller breaks", func(t *testing.T) {
for edge := range RegPol(Pt(0, 0), Sz(1, 1), 4, 0).Edges() {
AssertSegment(t, edge, Seg(Pt(1, 0), Pt(0, 1)))
break
}
})
t.Run("are the edges of the polygon", func(t *testing.T) {
for _, rp := range regularPolygonFixtures {
edges, expected := slices.Collect(rp.Edges()), slices.Collect(rp.Polygon().Edges())
assert.Equal(t, len(edges), len(expected), fmt.Sprintf("%s: ", rp))
for i := range edges {
AssertSegment(t, edges[i], expected[i], fmt.Sprintf("%s #%d: ", rp, i))
}
}
})
t.Run("ranging allocates nothing", func(t *testing.T) {
for _, rp := range regularPolygonFixtures {
AssertNumber(t, testing.AllocsPerRun(100, func() {
for edge := range rp.Edges() {
sinkBool = edge.IsZero()
}
}), 0, fmt.Sprintf("%s: ", rp))
}
})
}
func TestRegularPolygon_Centroid(t *testing.T) {
AssertPoint(t, Hexagon(Pt(1, 2), SzU(10), OrientationFlatTop).Centroid(), Pt(1, 2))
}
func TestRegularPolygon_Area(t *testing.T) {
t.Run("agrees with the polygon", func(t *testing.T) {
hexagon := Hexagon(Pt(0.0, 0.0), SzU(2.0), OrientationFlatTop)
AssertNumber(t, hexagon.Area(), hexagon.Polygon().Area())
AssertNumber(t, hexagon.Area(), 3*Sqrt3/2*4) // 3√3/2 · r²
})
t.Run("a square of semi-axis r encloses 2r²", func(t *testing.T) {
AssertNumber(t, Square(Pt(0.0, 0.0), SzU(3.0), OrientationPointyTop).Area(), 18.0)
})
t.Run("an ellipse scales the area by both semi-axes", func(t *testing.T) {
AssertNumber(t, Square(Pt(0.0, 0.0), Sz(2.0, 5.0), OrientationPointyTop).Area(), 20.0)
})
t.Run("fewer than three vertices enclose nothing", func(t *testing.T) {
AssertNumber(t, RegPol(Pt(3.0, 4.0), SzU(10.0), 0, 0).Area(), 0.0)
AssertNumber(t, RegPol(Pt(3.0, 4.0), SzU(10.0), 2, 0).Area(), 0.0)
})
t.Run("agrees with the polygon at any angle and size", func(t *testing.T) {
for _, rp := range regularPolygonFixtures {
AssertNumber(t, rp.Area(), rp.Polygon().Area(), rp.String())
}
})
}
func TestRegularPolygon_Perimeter(t *testing.T) {
t.Run("agrees with the polygon", func(t *testing.T) {
hexagon := Hexagon(Pt(0.0, 0.0), SzU(2.0), OrientationFlatTop)
AssertNumber(t, hexagon.Perimeter(), hexagon.Polygon().Perimeter())
AssertNumber(t, hexagon.Perimeter(), 12.0) // six edges of length r
})
t.Run("a square of semi-axis r has edges of r√2", func(t *testing.T) {
AssertNumber(t, Square(Pt(0.0, 0.0), SzU(3.0), OrientationPointyTop).Perimeter(), 12*Sqrt2)
})
t.Run("an ellipse sums chords of different lengths", func(t *testing.T) {
square := Square(Pt(0.0, 0.0), Sz(2.0, 5.0), OrientationPointyTop)
AssertNumber(t, square.Perimeter(), 4*math.Hypot(2, 5))
AssertNumber(t, square.Perimeter(), square.Polygon().Perimeter())
})
t.Run("fewer than two vertices have no edge", func(t *testing.T) {
AssertNumber(t, RegPol(Pt(3.0, 4.0), SzU(10.0), 0, 0).Perimeter(), 0.0)
AssertNumber(t, RegPol(Pt(3.0, 4.0), SzU(10.0), 1, 0).Perimeter(), 0.0)
AssertNumber(t, RegPol(Pt(3.0, 4.0), SzU(10.0), 2, 0).Perimeter(), 40.0)
})
t.Run("int measures the exact polygon, not the rounded vertices", func(t *testing.T) {
hexagon := Hexagon(Pt(0, 0), SzU(1), OrientationPointyTop)
AssertNumber(t, hexagon.Perimeter(), 6.0)
assert.NotEqual(t, hexagon.Polygon().Perimeter(), 6.0)
})
t.Run("agrees with the polygon at any angle and size", func(t *testing.T) {
for _, rp := range regularPolygonFixtures {
AssertNumber(t, rp.Perimeter(), rp.Polygon().Perimeter(), rp.String())
}
})
}
func TestRegularPolygon_Inertia(t *testing.T) {
t.Run("agrees with the polygon", func(t *testing.T) {
hexagon := Hexagon(Pt(0.0, 0.0), SzU(2.0), OrientationFlatTop)
AssertNumber(t, hexagon.Inertia(), hexagon.Polygon().Inertia())
AssertNumber(t, hexagon.Inertia(), 5*Sqrt3/8*16) // 5√3/8 · r⁴
})
t.Run("a square of semi-axis r has the moment 2r⁴/3", func(t *testing.T) {
AssertNumber(t, Square(Pt(0.0, 0.0), SzU(3.0), OrientationPointyTop).Inertia(), 54.0)
})
t.Run("an ellipse scales each axis by the cube of one semi-axis", func(t *testing.T) {
square := Square(Pt(0.0, 0.0), Sz(2.0, 5.0), OrientationPointyTop)
AssertNumber(t, square.Inertia(), square.Polygon().Inertia())
AssertNumber(t, square.Inertia(), 4*10*(16+100)/48.0) // a rhombus of diagonals p, q: pq(p²+q²)/48
})
t.Run("fewer than three vertices enclose nothing", func(t *testing.T) {
AssertNumber(t, RegPol(Pt(3.0, 4.0), SzU(10.0), 0, 0).Inertia(), 0.0)
AssertNumber(t, RegPol(Pt(3.0, 4.0), SzU(10.0), 2, 0).Inertia(), 0.0)
})
t.Run("agrees with the polygon at any angle and size", func(t *testing.T) {
for _, rp := range regularPolygonFixtures {
AssertNumber(t, rp.Inertia(), rp.Polygon().Inertia(), rp.String())
}
})
}
func TestRegularPolygon_Bounds(t *testing.T) {
t.Run("vertices on the axes", func(t *testing.T) {
AssertBox(t, RegPol(Pt(1, 2), Sz(2, 2), 4, 0).Bounds(), BoxFromMinMax(Pt(-1, 0), Pt(3, 4)))
})
t.Run("hexagon is tight around its vertices", func(t *testing.T) {
// width = 2r, height = √3 r
bounds := Hexagon(Pt(0.0, 0.0), Sz(2.0, 2.0), OrientationFlatTop).Bounds()
AssertNumber(t, bounds.Width(), 4.0)
AssertNumber(t, bounds.Height(), 2.0*Sqrt3)
})
t.Run("one and two vertices reach only where they lie", func(t *testing.T) {
AssertBox(t, RegPol(Pt(0.0, 0.0), SzU(2.0), 1, Pi/2).Bounds(), BoxFromMinMax(Pt(0.0, 2.0), Pt(0.0, 2.0)))
AssertBox(t, RegPol(Pt(0.0, 0.0), SzU(2.0), 2, Pi/4).Bounds(), BoxFromMinMax(Pt(-Sqrt2, -Sqrt2), Pt(Sqrt2, Sqrt2)))
})
t.Run("no vertices is the zero rectangle", func(t *testing.T) {
AssertBox(t, RegPol(Pt(3, 4), Sz(10, 10), 0, 0).Bounds(), Box[int]{})
})
t.Run("is exactly the box around the vertices, rounded alike for int", func(t *testing.T) {
for _, rp := range regularPolygonFixtures {
for _, angle := range []float64{0, 0.3, Pi / 3, 2, -1} {
turned := rp.Rotate(angle)
AssertBox(t, turned.Bounds(), turned.Polygon().Bounds(), turned.String())
assert.Equal(t, turned.Int().Bounds(), turned.Int().Polygon().Bounds(), turned.String())
}
}
})
}
func TestRegularPolygon_Translate(t *testing.T) {
AssertRegularPolygon(t, RegPol(Pt(1, 2), Sz(2, 2), 4, 0).Translate(Vec(1, -2)), RegPol(Pt(2, 0), Sz(2, 2), 4, 0))
}
func TestRegularPolygon_MoveTo(t *testing.T) {
AssertRegularPolygon(t, RegPol(Pt(1, 2), Sz(2, 2), 4, 0).MoveTo(Pt(-3, 5)), RegPol(Pt(-3, 5), Sz(2, 2), 4, 0))
}
func TestRegularPolygon_Scale(t *testing.T) {
t.Run("uniform factor", func(t *testing.T) {
AssertRegularPolygon(t, RegPol(Pt(1, 2), Sz(2, 2), 4, 0).Scale(0.5), RegPol(Pt(1, 2), Sz(1, 1), 4, 0))
})
t.Run("per-axis factor", func(t *testing.T) {
AssertRegularPolygon(t, RegPol(Pt(1, 2), Sz(2, 2), 4, 0).ScaleXY(2, 3), RegPol(Pt(1, 2), Sz(4, 6), 4, 0))
})
t.Run("a negative factor scales by its absolute value", func(t *testing.T) {
AssertRegularPolygon(t, RegPol(Pt(1, 2), Sz(2, 2), 4, 0).Scale(-2), RegPol(Pt(1, 2), Sz(4, 4), 4, 0))
AssertRegularPolygon(t, RegPol(Pt(1, 2), Sz(2, 2), 4, 0).ScaleXY(-2, 3), RegPol(Pt(1, 2), Sz(4, 6), 4, 0))
})
}
func TestRegularPolygon_Unscale(t *testing.T) {
t.Run("uniform factor", func(t *testing.T) {
AssertRegularPolygon(t, RegPol(Pt(1, 2), Sz(4, 4), 4, 0).Unscale(2), RegPol(Pt(1, 2), Sz(2, 2), 4, 0))
})
t.Run("per-axis factor", func(t *testing.T) {
AssertRegularPolygon(t, RegPol(Pt(1, 2), Sz(4, 6), 4, 0).UnscaleXY(2, 3), RegPol(Pt(1, 2), Sz(2, 2), 4, 0))
})
t.Run("a negative factor scales by its absolute value", func(t *testing.T) {
AssertRegularPolygon(t, RegPol(Pt(1, 2), Sz(4, 4), 4, 0).Unscale(-2), RegPol(Pt(1, 2), Sz(2, 2), 4, 0))
})
t.Run("zero factor panics", func(t *testing.T) {
assert.Panics(t, func() {
RegPol(Pt(1, 2), Sz(4, 4), 4, 0).Unscale(0)
}, "geom: division by zero")
assert.Panics(t, func() {
RegPol(Pt(1, 2), Sz(4, 4), 4, 0).UnscaleXY(0, 2)
}, "geom: division by zero")
})
}
func TestRegularPolygon_Resize(t *testing.T) {
t.Run("int", func(t *testing.T) {
AssertRegularPolygon(t, RegPol(Pt(1, 2), Sz(10, 4), 6, 0).Resize(Sz(3, 7)), RegPol(Pt(1, 2), Sz(3, 7), 6, 0))
})
t.Run("a negative semi-axis is taken absolute", func(t *testing.T) {
AssertRegularPolygon(t, RegPol(Pt(1, 2), Sz(10, 4), 6, 0).Resize(Sz(-3, 7)), RegPol(Pt(1, 2), Sz(3, 7), 6, 0))
})
t.Run("keeps the vertex count and the angle", func(t *testing.T) {
for _, rp := range regularPolygonFixtures {
resized := rp.Resize(Sz(3.0, 7.0))
AssertRegularPolygon(t, resized, RegPol(rp.Center, Sz(3.0, 7.0), rp.N, rp.Angle), rp.String())
AssertRegularPolygon(t, resized.Resize(rp.Size), rp, rp.String())
}
})
}
func TestRegularPolygon_Canonical(t *testing.T) {
t.Run("takes a literal negative size absolute and keeps the rest", func(t *testing.T) {
AssertRegularPolygon(t, RegularPolygon[int]{Pt(1, 2), Sz(-2, 3), 6, Pi / 3}.Canonical(), RegPol(Pt(1, 2), Sz(2, 3), 6, Pi/3))
})
t.Run("is the polygon RegPol builds", func(t *testing.T) {
rp := RegularPolygon[float64]{Pt(0.5, -1.25), Sz(-2.5, -3.75), 5, 1}
AssertRegularPolygon(t, rp.Canonical(), RegPol(rp.Center, rp.Size, rp.N, rp.Angle))
AssertVertices(t, slices.Collect(rp.Canonical().Vertices()), slices.Collect(RegPol(rp.Center, rp.Size, rp.N, rp.Angle).Vertices()))
})
t.Run("normalizes the angle the way Rotate stores it", func(t *testing.T) {
AssertRegularPolygon(t, RegularPolygon[float64]{Pt(0.0, 0.0), SzU(2.0), 4, 7}.Canonical(), RegPol(Pt(0.0, 0.0), SzU(2.0), 4, 7-2*Pi))
AssertNumber(t, RegularPolygon[float64]{Pt(0.0, 0.0), SzU(2.0), 4, -Pi / 2}.Canonical().Angle, 3*Pi/2)
})
t.Run("snaps a residue of turning back to exactly zero, where Rotate does not", func(t *testing.T) {
drifted := RegPol(Pt(0.0, 0.0), SzU(2.0), 4, 0).Rotate(0.1).Rotate(0.2).Rotate(-0.3)
assert.True(t, drifted.Angle != 0)
assert.Equal(t, drifted.Canonical().Angle, 0.0)
assert.Equal(t, RegularPolygon[int]{Pt(0, 0), SzU(2), 4, -Delta / 2}.Canonical().Angle, 0.0)
assert.Equal(t, RegularPolygon[int]{Pt(0, 0), SzU(2), 4, 2 * Delta}.Canonical().Angle, 2*Delta)
})
t.Run("a well-formed polygon is unchanged", func(t *testing.T) {
for _, rp := range regularPolygonFixtures {
AssertRegularPolygon(t, rp.Canonical(), rp, rp.String())
AssertRegularPolygon(t, rp.Rotate(0).Canonical(), rp.Rotate(0), rp.String())
}
})
}
func TestRegularPolygon_Grow(t *testing.T) {
t.Run("int", func(t *testing.T) {
AssertRegularPolygon(t, RegPol(Pt(1, 2), Sz(10, 4), 6, 0).Grow(2), RegPol(Pt(1, 2), Sz(12, 6), 6, 0))
})
t.Run("clamped at zero", func(t *testing.T) {
AssertRegularPolygon(t, RegPol(Pt(1, 2), Sz(10, 4), 6, 0).Grow(-6), RegPol(Pt(1, 2), Sz(4, 0), 6, 0))
})
t.Run("shrink undoes it", func(t *testing.T) {
for _, rp := range regularPolygonFixtures {
AssertRegularPolygon(t, rp.Grow(1.5).Shrink(1.5), rp, rp.String())
}
})
}
func TestRegularPolygon_GrowXY(t *testing.T) {
t.Run("int", func(t *testing.T) {
AssertRegularPolygon(t, RegPol(Pt(1, 2), Sz(10, 4), 6, 0).GrowXY(2, 3), RegPol(Pt(1, 2), Sz(12, 7), 6, 0))
})
t.Run("clamped at zero", func(t *testing.T) {
AssertRegularPolygon(t, RegPol(Pt(1, 2), Sz(10, 4), 6, 0).GrowXY(0, -6), RegPol(Pt(1, 2), Sz(10, 0), 6, 0))
})
}
func TestRegularPolygon_Shrink(t *testing.T) {
t.Run("int", func(t *testing.T) {
AssertRegularPolygon(t, RegPol(Pt(1, 2), Sz(10, 4), 6, 0).Shrink(2), RegPol(Pt(1, 2), Sz(8, 2), 6, 0))
})
t.Run("clamped at zero", func(t *testing.T) {
AssertRegularPolygon(t, RegPol(Pt(1, 2), Sz(10, 4), 6, 0).Shrink(6), RegPol(Pt(1, 2), Sz(4, 0), 6, 0))
})
}
func TestRegularPolygon_ShrinkXY(t *testing.T) {
t.Run("int", func(t *testing.T) {
AssertRegularPolygon(t, RegPol(Pt(1, 2), Sz(10, 4), 6, 0).ShrinkXY(2, 3), RegPol(Pt(1, 2), Sz(8, 1), 6, 0))
})
t.Run("clamped at zero", func(t *testing.T) {
AssertRegularPolygon(t, RegPol(Pt(1, 2), Sz(10, 4), 6, 0).ShrinkXY(0, 6), RegPol(Pt(1, 2), Sz(10, 0), 6, 0))
})
}
func TestRegularPolygon_Lerp(t *testing.T) {
a, b := RegPol(Pt(0.0, 0.0), SzU(2.0), 6, 0), RegPol(Pt(10.0, 20.0), Sz(8.0, 4.0), 6, Pi/2)
t.Run("moves the center, the size and the angle together", func(t *testing.T) {
AssertRegularPolygon(t, a.Lerp(b, 0.5), RegPol(Pt(5.0, 10.0), Sz(5.0, 3.0), 6, Pi/4))
})
t.Run("turns along the shorter arc across the seam", func(t *testing.T) {
from, to := RegPol(Pt(0, 0), SzU(2), 4, ToRadians(350)), RegPol(Pt(0, 0), SzU(2), 4, ToRadians(10))
AssertRegularPolygon(t, from.Lerp(to, 0.5), RegPol(Pt(0, 0), SzU(2), 4, 0))
AssertRegularPolygon(t, to.Lerp(from, 0.5), RegPol(Pt(0, 0), SzU(2), 4, 0))
})
t.Run("the ends are the polygons themselves", func(t *testing.T) {
AssertRegularPolygon(t, a.Lerp(b, 0), a)
AssertRegularPolygon(t, a.Lerp(b, 1), b)
})
t.Run("extrapolates and keeps the size absolute", func(t *testing.T) {
AssertRegularPolygon(t, a.Lerp(b, 2), RegPol(Pt(20.0, 40.0), Sz(14.0, 6.0), 6, Pi))
AssertRegularPolygon(t, b.Lerp(a, 2), RegPol(Pt(-10.0, -20.0), Sz(4.0, 0.0), 6, 3*Pi/2))
})
t.Run("int rounds the center and the size", func(t *testing.T) {
AssertRegularPolygon(t, RegPol(Pt(0, 0), SzU(2), 4, 0).Lerp(RegPol(Pt(5, 5), SzU(5), 4, 0), 0.5), RegPol(Pt(3, 3), SzU(4), 4, 0))
})
t.Run("a different vertex count panics", func(t *testing.T) {
assert.PanicsWith(t, func() {
a.Lerp(RegPol(Pt(10.0, 20.0), Sz(8.0, 4.0), 5, Pi/2), 0.5)
}, "geom: lerp between polygons of 6 and 5 vertices")
})
t.Run("the ends hold over the fixtures and the angle never turns more than half a turn", func(t *testing.T) {
for _, from := range regularPolygonFixtures {
for _, to := range regularPolygonFixtures {
if from.N != to.N {
continue
}
AssertRegularPolygon(t, from.Lerp(to, 0), from, fmt.Sprintf("%s → %s: ", from, to))
AssertRegularPolygon(t, from.Lerp(to, 1), to, fmt.Sprintf("%s → %s: ", from, to))
assert.True(t, AngleDistance(from.Lerp(to, 0.5).Angle, from.Angle) <= Pi/2+Delta, fmt.Sprintf("%s → %s: ", from, to))
}
}
})
}
func TestRegularPolygon_Transform(t *testing.T) {
hexagon := Hexagon(Pt(2.0, 3.0), SzU(4.0), OrientationFlatTop)
t.Run("the identity keeps the polygon", func(t *testing.T) {
AssertRegularPolygon(t, hexagon.Transform(IdentityMatrix[float64]()), hexagon)
})
t.Run("a translation moves the center and keeps the vertex count", func(t *testing.T) {
moved := hexagon.Transform(TranslationMatrix(1.0, -1.0))
AssertRegularPolygon(t, moved, hexagon.Translate(Vec(1.0, -1.0)))
assert.Equal(t, moved.N, 6)
})
t.Run("a uniform scale scales the semi-axes", func(t *testing.T) {
AssertRegularPolygon(t, hexagon.Transform(ScaleMatrix(2.0, 2.0)), Hexagon(Pt(4.0, 6.0), SzU(8.0), OrientationFlatTop))
})
t.Run("a rotation turns the angle", func(t *testing.T) {
AssertRegularPolygon(t, hexagon.Transform(RotationMatrix[float64](Pi/2)), Hexagon(Pt(-3.0, 2.0), SzU(4.0), OrientationFlatTop).Rotate(Pi/2))
})
t.Run("a reflection mirrors the angle about the axis of the matrix", func(t *testing.T) {
turned := hexagon.Rotate(Pi / 6)
AssertRegularPolygon(t, turned.Transform(ReflectionMatrix[float64](AxisHorizontal)), RegPol(Pt(2.0, -3.0), SzU(4.0), 6, -turned.Angle))
})
t.Run("a shear gives the nearest polygon", func(t *testing.T) {
AssertRegularPolygon(t, hexagon.Transform(ShearMatrix(1.0, 0.0)), Hexagon(Pt(5.0, 3.0), SzU(4.0), OrientationFlatTop))
})
t.Run("an empty polygon stays empty", func(t *testing.T) {
assert.True(t, RegPol(Pt(1.0, 1.0), SzU(2.0), 0, 0).Transform(ScaleMatrix(2.0, 2.0)).IsEmpty())
})
t.Run("int rounds the center and the semi-axes once", func(t *testing.T) {
AssertRegularPolygon(t, RegPol(Pt(1, 1), SzU(3), 6, 0).Transform(ScaleMatrix(1.5, 1.5)), RegPol(Pt(2, 2), SzU(5), 6, 0))
})
t.Run("a turn of unequal semi-axes is exact too", func(t *testing.T) {
ellipse := RegPol(Pt(0.0, 0.0), Sz(2.0, 4.0), 5, 0)
turn := RotationMatrix[float64](Pi / 2)
AssertRegularPolygon(t, ellipse.Transform(turn), RegPol(Pt(0.0, 0.0), Sz(2.0, 4.0), 5, Pi/2))
AssertPolygon(t, ellipse.Transform(turn).Polygon(), ellipse.Polygon().Transform(turn))
})
t.Run("axes scaled by different factors turn the polygon into one it cannot hold", func(t *testing.T) {
turned := RegPol(Pt(0.0, 0.0), SzU(4.0), 5, Pi/4)
squeeze := ScaleMatrix(2.0, 3.0)
assert.False(t, turned.Transform(squeeze).Polygon().Equal(turned.Polygon().Transform(squeeze)))
})
t.Run("matches the polygon of the vertices wherever the matrix keeps one", func(t *testing.T) {
for _, rp := range regularPolygonFixtures {
matrices := []Matrix[float64]{
IdentityMatrix[float64](),
TranslationMatrix(3.0, -2.0),
ScaleMatrix(2.0, 2.0),
RotationMatrix[float64](Pi / 3),
RotationMatrix[float64](Pi / 3).Multiply(ScaleMatrix(2.0, 2.0)),
}
if rp.Angle == 0 {
matrices = append(matrices, ScaleMatrix(2.0, 3.0))
}
for _, m := range matrices {
AssertPolygon(t, rp.Transform(m).Polygon(), rp.Polygon().Transform(m), fmt.Sprintf("%s → %s: ", rp, m))
}
}
})
}
func TestRegularPolygon_Rotate(t *testing.T) {
t.Run("adds to the stored angle", func(t *testing.T) {
AssertRegularPolygon(t, RegPol(Pt(1, 2), Sz(2, 2), 4, 0).Rotate(Pi), RegPol(Pt(1, 2), Sz(2, 2), 4, Pi))
})
t.Run("normalizes to the unit turn", func(t *testing.T) {
AssertRegularPolygon(t, RegPol(Pt(1, 2), Sz(2, 2), 4, 0).Rotate(3*Pi), RegPol(Pt(1, 2), Sz(2, 2), 4, Pi)) // 0 + 3π → π
AssertRegularPolygon(t, RegPol(Pt(1, 2), Sz(2, 2), 4, 0).Rotate(-Pi/2), RegPol(Pt(1, 2), Sz(2, 2), 4, 3*Pi/2)) // 0 − π/2 → 3π/2
})
}
func TestRegularPolygon_AlignTo(t *testing.T) {
hex := Hexagon(Pt(0.0, 0.0), SzU(10.0), OrientationPointyTop)
t.Run("an anchor lands on the point", func(t *testing.T) {
AssertRegularPolygon(t, hex.AlignTo(Top, Pt(5.0, 5.0)), Hexagon(Pt(5.0, 15.0), SzU(10.0), OrientationPointyTop))
})
t.Run("none aligns the center", func(t *testing.T) {
AssertRegularPolygon(t, hex.AlignTo(DirectionNone, Pt(5.0, 5.0)), Hexagon(Pt(5.0, 5.0), SzU(10.0), OrientationPointyTop))
})
}
func TestRegularPolygon_Contains(t *testing.T) {
diamond := RegPol(Pt(0, 0), Sz(2, 2), 4, 0)
t.Run("inside", func(t *testing.T) {
assert.True(t, diamond.Contains(Pt(0, 0)))
assert.True(t, diamond.Contains(Pt(1, 0)))
})
t.Run("on an edge and on a vertex", func(t *testing.T) {
assert.True(t, diamond.Contains(Pt(1, 1)))
assert.True(t, diamond.Contains(Pt(2, 0)))
})
t.Run("outside", func(t *testing.T) {
assert.False(t, diamond.Contains(Pt(2, 2)))
assert.False(t, diamond.Contains(Pt(3, 0)))
})
t.Run("within the tolerance of an edge", func(t *testing.T) {
assert.True(t, RegPol(Pt(0.0, 0.0), Sz(2.0, 2.0), 4, 0).Contains(Pt(1.0, 1.0+Delta/2)))
assert.False(t, RegPol(Pt(0.0, 0.0), Sz(2.0, 2.0), 4, 0).Contains(Pt(1.0, 1.0+2*Delta)))
})
t.Run("an empty polygon contains nothing", func(t *testing.T) {
assert.False(t, RegPol(Pt(0, 0), Sz(2, 2), 0, 0).Contains(Pt(0, 0)))
})
t.Run("matches the polygon of the vertices", func(t *testing.T) {
for _, rp := range regularPolygonFixtures {
for _, p := range pointFixtures {
assert.Equal(t, rp.Contains(p), rp.Polygon().Contains(p), fmt.Sprintf("%s → %s: ", rp, p))
}
}
})
t.Run("int matches the polygon of the rounded vertices", func(t *testing.T) {
for _, rp := range regularPolygonFixtures {
for _, p := range pointFixtures {
assert.Equal(t, rp.Int().Contains(p.Int()), rp.Int().Polygon().Contains(p.Int()), fmt.Sprintf("%s → %s: ", rp.Int(), p.Int()))
}
}
})
}
func BenchmarkRegularPolygon_Contains(b *testing.B) {
hexagon := Hexagon(Pt(0.0, 0.0), SzU(10.0), OrientationFlatTop)
inside, outside := Pt(1.0, 2.0), Pt(9.0, 9.0)
b.Run("inside", func(b *testing.B) {
for b.Loop() {
sinkBool = hexagon.Contains(inside)
}
})
b.Run("outside within the extent", func(b *testing.B) {
for b.Loop() {
sinkBool = hexagon.Contains(outside)
}
})
}
func TestRegularPolygon_DistanceTo(t *testing.T) {
diamond := RegPol(Pt(0, 0), Sz(2, 2), 4, 0)
t.Run("zero within the polygon", func(t *testing.T) {
assert.Equal(t, diamond.DistanceTo(Pt(0, 0)), 0.0)
assert.Equal(t, diamond.DistanceTo(Pt(1, 1)), 0.0)
})
t.Run("to the nearest edge", func(t *testing.T) {
AssertNumber(t, diamond.DistanceTo(Pt(2, 2)), Sqrt2)
})
t.Run("to the nearest vertex", func(t *testing.T) {
AssertNumber(t, diamond.DistanceTo(Pt(3, 0)), 1.0)
})
t.Run("an empty polygon is infinitely far", func(t *testing.T) {
assert.Equal(t, RegPol(Pt(0, 0), Sz(2, 2), 0, 0).DistanceTo(Pt(0, 0)), math.Inf(1))
})
t.Run("matches the polygon of the vertices", func(t *testing.T) {
for _, rp := range regularPolygonFixtures {
for _, p := range pointFixtures {
AssertNumber(t, rp.DistanceTo(p), rp.Polygon().DistanceTo(p), fmt.Sprintf("%s → %s: ", rp, p))
}
}
})
}
func TestRegularPolygon_DistanceSquaredTo(t *testing.T) {
diamond := RegPol(Pt(0, 0), Sz(2, 2), 4, 0)
t.Run("is the square of DistanceTo", func(t *testing.T) {
AssertNumber(t, diamond.DistanceSquaredTo(Pt(2, 2)), 2.0)
AssertNumber(t, diamond.DistanceSquaredTo(Pt(3, 0)), 1.0)
assert.Equal(t, diamond.DistanceSquaredTo(Pt(0, 1)), 0.0)
})
t.Run("agrees with DistanceTo", func(t *testing.T) {
for _, rp := range regularPolygonFixtures {
for _, p := range pointFixtures {
AssertNumber(t, rp.DistanceSquaredTo(p), rp.DistanceTo(p)*rp.DistanceTo(p), fmt.Sprintf("%s → %s: ", rp, p))
}
}
})
}
func TestRegularPolygon_Nearest(t *testing.T) {
diamond := RegPol(Pt(0, 0), Sz(2, 2), 4, 0)
t.Run("the foot on the nearest edge", func(t *testing.T) {
AssertPoint(t, diamond.Nearest(Pt(2, 2)), Pt(1, 1))
AssertPoint(t, diamond.Nearest(Pt(3, 0)), Pt(2, 0))
})
t.Run("a point inside is its own nearest point", func(t *testing.T) {
AssertPoint(t, diamond.Nearest(Pt(0, 1)), Pt(0, 1))
})
t.Run("an empty polygon returns the zero point", func(t *testing.T) {
AssertPoint(t, RegPol(Pt(1, 1), Sz(2, 2), 0, 0).Nearest(Pt(3, 4)), Pt(0, 0))
})
t.Run("over the fixtures", func(t *testing.T) {
for _, rp := range regularPolygonFixtures {
for _, p := range pointFixtures {
assertNearest[float64](t, rp, p)
}
}
})
}
func TestRegularPolygon_EnclosesCircle(t *testing.T) {
square := RegPol(Pt(0.0, 0.0), Sz(2.0, 2.0), 4, Pi/4)
t.Run("the inscribed circle touches every side from inside", func(t *testing.T) {
assert.True(t, square.EnclosesCircle(Circ(Pt(0.0, 0.0), math.Sqrt2)))
assert.False(t, square.EnclosesCircle(Circ(Pt(0.0, 0.0), 1.5)))
})
t.Run("an empty polygon encloses nothing", func(t *testing.T) {
assert.False(t, RegPol(Pt(0, 0), Sz(2, 2), 0, 0).EnclosesCircle(Circ(Pt(0, 0), 0)))
})
t.Run("matches the polygon of the vertices", func(t *testing.T) {
for _, rp := range regularPolygonFixtures {
for _, c := range circleFixtures {
assert.Equal(t, rp.EnclosesCircle(c), rp.Polygon().EnclosesCircle(c), fmt.Sprintf("%s → %s: ", rp, c))
}
}
})
}
func TestRegularPolygon_EnclosesSegment(t *testing.T) {
diamond := RegPol(Pt(0, 0), Sz(2, 2), 4, 0)
t.Run("a diagonal counts, a segment past a vertex not", func(t *testing.T) {
assert.True(t, diamond.EnclosesSegment(Seg(Pt(-2, 0), Pt(2, 0))))
assert.False(t, diamond.EnclosesSegment(Seg(Pt(-2, 0), Pt(3, 0))))
})
t.Run("matches the polygon of the vertices", func(t *testing.T) {
for _, rp := range regularPolygonFixtures {
for _, s := range segmentFixtures {
assert.Equal(t, rp.EnclosesSegment(s), rp.Polygon().EnclosesSegment(s), fmt.Sprintf("%s → %s: ", rp, s))
}
}
})
}
func TestRegularPolygon_EnclosesPolygon(t *testing.T) {
t.Run("inside, inscribed and outside", func(t *testing.T) {
assert.True(t, RegPol(Pt(1, 1), Sz(2, 2), 4, Pi/4).EnclosesPolygon(Pol(squareVertices())))
assert.True(t, RegPol(Pt(1, 1), Sz(2, 2), 4, 0).EnclosesPolygon(Pol(squareVertices())))
assert.False(t, RegPol(Pt(1, 1), Sz(1, 1), 4, 0).EnclosesPolygon(Pol(squareVertices())))
})
t.Run("an empty polygon is enclosed by nothing", func(t *testing.T) {
assert.False(t, RegPol(Pt(0, 0), Sz(2, 2), 4, 0).EnclosesPolygon(Pol[int](nil)))
})
t.Run("matches the polygon of the vertices", func(t *testing.T) {
for _, rp := range regularPolygonFixtures {
for _, p := range polygonFixtures() {
assert.Equal(t, rp.EnclosesPolygon(p), rp.Polygon().EnclosesPolygon(p), fmt.Sprintf("%s → %s: ", rp, p))
}
}
})
}
func TestRegularPolygon_EnclosesRectangle(t *testing.T) {
t.Run("a square rotated into a diamond", func(t *testing.T) {
diamond := RegPol(Pt(0.0, 0.0), Sz(2.0, 2.0), 4, 0)
assert.True(t, diamond.EnclosesRectangle(Rect(Pt(0.0, 0.0), Sz(2.0, 2.0))))
assert.False(t, diamond.EnclosesRectangle(Rect(Pt(0.0, 0.0), Sz(2.0, 2.2))))
})
t.Run("matches the polygon of the vertices", func(t *testing.T) {
for _, rp := range regularPolygonFixtures {
for _, r := range rectFixtures {
assert.Equal(t, rp.EnclosesRectangle(r), rp.Polygon().EnclosesRectangle(r), fmt.Sprintf("%s → %s: ", rp, r))
}
}
})
}
func TestRegularPolygon_EnclosesRegularPolygon(t *testing.T) {
hexagon := Hexagon(Pt(0.0, 0.0), SzU(10.0), OrientationFlatTop)
t.Run("a smaller one and a turned one of the same size", func(t *testing.T) {
assert.True(t, hexagon.EnclosesRegularPolygon(hexagon.Unscale(2)))
assert.False(t, hexagon.EnclosesRegularPolygon(hexagon.Rotate(Pi/6)))
})
t.Run("an empty polygon on either side encloses nothing", func(t *testing.T) {
assert.False(t, hexagon.EnclosesRegularPolygon(RegPol(Pt(0.0, 0.0), Sz(1.0, 1.0), 0, 0)))
assert.False(t, RegPol(Pt(0.0, 0.0), Sz(1.0, 1.0), 0, 0).EnclosesRegularPolygon(hexagon))
})
t.Run("matches the polygon of the vertices", func(t *testing.T) {
for _, a := range regularPolygonFixtures {
for _, b := range regularPolygonFixtures {
assert.Equal(t, a.EnclosesRegularPolygon(b), a.Polygon().EnclosesRegularPolygon(b), fmt.Sprintf("%s → %s: ", a, b))
}
}
})
}
func TestRegularPolygon_EnclosesBox(t *testing.T) {
t.Run("matches the polygon of the vertices", func(t *testing.T) {
for _, rp := range regularPolygonFixtures {
for _, b := range boxFixtures {
assert.Equal(t, rp.EnclosesBox(b), rp.Polygon().EnclosesBox(b), fmt.Sprintf("%s → %s: ", rp, b))
}
}
})
}
func TestRegularPolygon_IntersectsCircle(t *testing.T) {
t.Run("mirrors Circle.IntersectsRegularPolygon", func(t *testing.T) {
for _, rp := range regularPolygonFixtures {
for _, c := range circleFixtures {
assert.Equal(t, rp.IntersectsCircle(c), c.IntersectsRegularPolygon(rp), fmt.Sprintf("%s → %s: ", rp, c))
}
}
})
}
func TestRegularPolygon_IntersectsSegment(t *testing.T) {
t.Run("mirrors Segment.IntersectsRegularPolygon", func(t *testing.T) {
for _, rp := range regularPolygonFixtures {
for _, s := range segmentFixtures {
assert.Equal(t, rp.IntersectsSegment(s), s.IntersectsRegularPolygon(rp), fmt.Sprintf("%s → %s: ", rp, s))
}
}
})
}
func TestRegularPolygon_IntersectionSegment(t *testing.T) {
t.Run("mirrors Segment.IntersectionRegularPolygon", func(t *testing.T) {
for _, rp := range regularPolygonFixtures {
for _, s := range segmentFixtures {
AssertVertices(t, rp.IntersectionSegment(s), s.IntersectionRegularPolygon(rp), fmt.Sprintf("%s → %s: ", rp, s))
}
}
})
}
func TestRegularPolygon_IntersectsPolygon(t *testing.T) {
t.Run("mirrors Polygon.IntersectsRegularPolygon", func(t *testing.T) {
for _, rp := range regularPolygonFixtures {
for _, p := range polygonFixtures() {
assert.Equal(t, rp.IntersectsPolygon(p), p.IntersectsRegularPolygon(rp), fmt.Sprintf("%s → %s: ", rp, p))
}
}
})
}
func TestRegularPolygon_IntersectsRectangle(t *testing.T) {
t.Run("mirrors Rectangle.IntersectsRegularPolygon", func(t *testing.T) {
for _, rp := range regularPolygonFixtures {
for _, r := range rectFixtures {
assert.Equal(t, rp.IntersectsRectangle(r), r.IntersectsRegularPolygon(rp), fmt.Sprintf("%s → %s: ", rp, r))
}
}
})
}
func TestRegularPolygon_IntersectsRegularPolygon(t *testing.T) {
diamond := RegPol(Pt(0, 0), Sz(2, 2), 4, 0)
t.Run("overlapping", func(t *testing.T) {
assert.True(t, diamond.IntersectsRegularPolygon(diamond.Translate(Vec(1, 1))))
})
t.Run("apart", func(t *testing.T) {
assert.False(t, diamond.IntersectsRegularPolygon(diamond.Translate(Vec(5, 0))))
})
t.Run("a shared vertex counts", func(t *testing.T) {
assert.True(t, diamond.IntersectsRegularPolygon(diamond.Translate(Vec(4, 0))))
})
t.Run("one contained in the other", func(t *testing.T) {
assert.True(t, diamond.IntersectsRegularPolygon(RegPol(Pt(0, 0), Sz(1, 1), 3, 0)))
assert.True(t, RegPol(Pt(0, 0), Sz(1, 1), 3, 0).IntersectsRegularPolygon(diamond))
})
t.Run("edges crossing without a vertex inside", func(t *testing.T) {
assert.True(t, RegPol(Pt(0.0, 0.0), Sz(2.0, 2.0), 4, 0).IntersectsRegularPolygon(RegPol(Pt(0.0, 0.0), Sz(2.0, 2.0), 4, Pi/4)))
})
t.Run("an empty polygon intersects nothing", func(t *testing.T) {
assert.False(t, diamond.IntersectsRegularPolygon(RegPol(Pt(0, 0), Sz(2, 2), 0, 0)))
assert.False(t, RegPol(Pt(0, 0), Sz(2, 2), 0, 0).IntersectsRegularPolygon(diamond))
})
t.Run("symmetric", func(t *testing.T) {
for _, a := range regularPolygonFixtures {
for _, b := range regularPolygonFixtures {
assert.Equal(t, a.IntersectsRegularPolygon(b), b.IntersectsRegularPolygon(a), fmt.Sprintf("%s → %s: ", a, b))
}
}
})
t.Run("matches the polygons of the vertices", func(t *testing.T) {
for _, a := range regularPolygonFixtures {
for _, b := range regularPolygonFixtures {
assert.Equal(t, a.IntersectsRegularPolygon(b), a.Polygon().IntersectsPolygon(b.Polygon()), fmt.Sprintf("%s → %s: ", a, b))
assert.Equal(t, a.IntersectsRegularPolygon(b.Translate(Vec(3.0, -2.0))), a.Polygon().IntersectsPolygon(b.Translate(Vec(3.0, -2.0)).Polygon()), fmt.Sprintf("%s → %s: ", a, b))
}
}
})
}
func TestRegularPolygon_IntersectsBox(t *testing.T) {
diamond := RegPol(Pt(0, 0), Sz(2, 2), 4, 0)
t.Run("overlapping", func(t *testing.T) {
assert.True(t, diamond.IntersectsBox(BoxFromMinMax(Pt(0, 0), Pt(2, 2))))
})
t.Run("apart within overlapping bounds", func(t *testing.T) {
assert.False(t, diamond.IntersectsBox(BoxFromMinMax(Pt(2, 2), Pt(3, 3))))
})
t.Run("an empty polygon intersects nothing", func(t *testing.T) {
assert.False(t, RegPol(Pt(0, 0), Sz(2, 2), 0, 0).IntersectsBox(BoxFromMinMax(Pt(-1, -1), Pt(1, 1))))
})
t.Run("matches the polygon of the vertices", func(t *testing.T) {
for _, rp := range regularPolygonFixtures {
for _, b := range boxFixtures {
assert.Equal(t, rp.IntersectsBox(b), rp.Polygon().IntersectsBox(b), fmt.Sprintf("%s → %s: ", rp, b))
}
}
})
}
func TestRegularPolygon_Equal(t *testing.T) {
t.Run("same polygon", func(t *testing.T) {
assert.True(t, RegPol(Pt(1, 2), Sz(2, 2), 4, 0).Equal(RegPol(Pt(1, 2), Sz(2, 2), 4, 0)))
})
t.Run("different polygon", func(t *testing.T) {
assert.False(t, RegPol(Pt(1, 2), Sz(2, 2), 4, 0).Equal(RegPol(Pt(0, 0), Sz(2, 2), 6, 0)))
assert.False(t, RegPol(Pt(1, 2), Sz(2, 2), 4, 0).Equal(RegPol(Pt(1, 2), Sz(3, 3), 4, 0)))
})
t.Run("the angle is compared", func(t *testing.T) {
assert.False(t, RegPol(Pt(1, 2), Sz(2, 2), 4, 0).Equal(RegPol(Pt(1, 2), Sz(2, 2), 4, Pi)))
assert.True(t, RegPol(Pt(1, 2), Sz(2, 2), 4, 0.5).Equal(RegPol(Pt(1, 2), Sz(2, 2), 4, 0.5)))
})
t.Run("the angle is compared normalized", func(t *testing.T) {
hexagon := Hexagon(Pt(0.0, 0.0), SzU(10.0), OrientationPointyTop)
assert.True(t, hexagon.Equal(hexagon.Rotate(0)))
assert.True(t, hexagon.Equal(hexagon.Rotate(2*Pi)))
assert.True(t, RegPol(Pt(1, 2), Sz(2, 2), 4, -Pi/2).Equal(RegPol(Pt(1, 2), Sz(2, 2), 4, 3*Pi/2)))
})
t.Run("the angle is compared across the seam", func(t *testing.T) {
hexagon := Hexagon(Pt(0.0, 0.0), SzU(10.0), OrientationPointyTop)
assert.True(t, hexagon.Equal(hexagon.Rotate(-1e-9)))
assert.True(t, RegPol(Pt(1, 2), Sz(2, 2), 4, 0).Equal(RegPol(Pt(1, 2), Sz(2, 2), 4, 2*Pi-1e-9)))
})
}
func TestRegularPolygon_IsZero(t *testing.T) {
t.Run("zero polygon", func(t *testing.T) {
assert.True(t, RegularPolygon[int]{}.IsZero())
assert.True(t, RegularPolygon[float64]{}.IsZero())
})
t.Run("a full turn is a zero angle, like Equal", func(t *testing.T) {
assert.True(t, RegPol(Pt(0, 0), Sz(0, 0), 0, 2*Pi).IsZero())
})
t.Run("only one field is zero", func(t *testing.T) {
assert.False(t, RegPol(Pt(0, 0), Sz(0, 0), 4, 0).IsZero())
assert.False(t, RegPol(Pt(1, 2), Sz(0, 0), 0, 0).IsZero())
assert.False(t, RegPol(Pt(0, 0), Sz(2, 2), 0, 0).IsZero())
assert.False(t, RegPol(Pt(0, 0), Sz(0, 0), 0, 1).IsZero())
})
t.Run("non-zero polygon", func(t *testing.T) {
assert.False(t, RegPol(Pt(1, 2), Sz(2, 2), 4, 0).IsZero())
})
}
func TestRegularPolygon_IsEmpty(t *testing.T) {
t.Run("no sides", func(t *testing.T) {
assert.True(t, RegularPolygon[int]{}.IsEmpty())
assert.True(t, RegPol(Pt(1, 2), Sz(2, 2), 0, 0).IsEmpty())
assert.True(t, RegPol(Pt(1, 2), Sz(2, 2), -1, 0).IsEmpty())
})
t.Run("with sides", func(t *testing.T) {
assert.False(t, RegPol(Pt(1, 2), Sz(2, 2), 4, 0).IsEmpty())
})
}
func TestRegularPolygon_IsAligned(t *testing.T) {
t.Run("no turn", func(t *testing.T) {
assert.True(t, RegPol(Pt(1, 2), Sz(3, 4), 6, 0).IsAligned())
})