-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcircle_test.go
801 lines (746 loc) · 22.6 KB
/
circle_test.go
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
package geom2d
import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"math"
"testing"
)
func TestCircle_Area(t *testing.T) {
tests := map[string]struct {
circle Circle[float64]
expected float64
}{
"radius 1": {circle: Circle[float64]{center: NewPoint[float64](0, 0), radius: 1}, expected: math.Pi},
"radius 2": {circle: Circle[float64]{center: NewPoint[float64](0, 0), radius: 2}, expected: 4 * math.Pi},
"radius 0": {circle: Circle[float64]{center: NewPoint[float64](0, 0), radius: 0}, expected: 0},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
assert.InDelta(t, tt.expected, tt.circle.Area(), 0.001)
})
}
}
func TestCircle_AsFloat32(t *testing.T) {
tests := map[string]struct {
circle Circle[int]
expected Circle[float32]
}{
"integer center and radius": {
circle: Circle[int]{center: NewPoint(3, 4), radius: 5},
expected: Circle[float32]{center: NewPoint[float32](3.0, 4.0), radius: 5.0},
},
"zero center and radius": {
circle: Circle[int]{center: NewPoint(0, 0), radius: 0},
expected: Circle[float32]{center: NewPoint[float32](0.0, 0.0), radius: 0.0},
},
"negative center and radius": {
circle: Circle[int]{center: NewPoint(-3, -4), radius: 5},
expected: Circle[float32]{center: NewPoint[float32](-3.0, -4.0), radius: 5.0},
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
result := tt.circle.AsFloat32()
assert.Equal(t, tt.expected.center, result.center)
assert.Equal(t, tt.expected.radius, result.radius)
})
}
}
func TestCircle_AsFloat64(t *testing.T) {
tests := map[string]struct {
circle Circle[int]
expected Circle[float64]
}{
"integer center and radius": {
circle: Circle[int]{center: NewPoint(3, 4), radius: 5},
expected: Circle[float64]{center: NewPoint[float64](3.0, 4.0), radius: 5.0},
},
"zero center and radius": {
circle: Circle[int]{center: NewPoint(0, 0), radius: 0},
expected: Circle[float64]{center: NewPoint[float64](0.0, 0.0), radius: 0.0},
},
"negative center and radius": {
circle: Circle[int]{center: NewPoint(-3, -4), radius: 5},
expected: Circle[float64]{center: NewPoint[float64](-3.0, -4.0), radius: 5.0},
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
result := tt.circle.AsFloat64()
assert.Equal(t, tt.expected.center, result.center)
assert.Equal(t, tt.expected.radius, result.radius)
})
}
}
func TestCircle_AsInt(t *testing.T) {
tests := map[string]struct {
circle Circle[float64]
expected Circle[int]
}{
"positive float center and radius": {
circle: Circle[float64]{center: NewPoint[float64](3.9, 4.5), radius: 5.8},
expected: Circle[int]{center: NewPoint(3, 4), radius: 5},
},
"zero center and radius": {
circle: Circle[float64]{center: NewPoint[float64](0.0, 0.0), radius: 0.0},
expected: Circle[int]{center: NewPoint(0, 0), radius: 0},
},
"negative float center and radius": {
circle: Circle[float64]{center: NewPoint[float64](-3.7, -4.2), radius: 5.9},
expected: Circle[int]{center: NewPoint(-3, -4), radius: 5},
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
result := tt.circle.AsInt()
assert.Equal(t, tt.expected.center, result.center)
assert.Equal(t, tt.expected.radius, result.radius)
})
}
}
func TestCircle_AsIntRounded(t *testing.T) {
tests := map[string]struct {
circle Circle[float64]
expected Circle[int]
}{
"positive float center and radius with rounding up": {
circle: Circle[float64]{center: NewPoint[float64](3.6, 4.5), radius: 5.7},
expected: Circle[int]{center: NewPoint(4, 5), radius: 6},
},
"positive float center and radius with rounding down": {
circle: Circle[float64]{center: NewPoint[float64](3.4, 4.4), radius: 5.2},
expected: Circle[int]{center: NewPoint(3, 4), radius: 5},
},
"zero center and radius": {
circle: Circle[float64]{center: NewPoint[float64](0.0, 0.0), radius: 0.0},
expected: Circle[int]{center: NewPoint(0, 0), radius: 0},
},
"negative float center and radius with rounding up": {
circle: Circle[float64]{center: NewPoint[float64](-3.6, -4.5), radius: 5.7},
expected: Circle[int]{center: NewPoint(-4, -5), radius: 6},
},
"negative float center and radius with rounding down": {
circle: Circle[float64]{center: NewPoint[float64](-3.4, -4.4), radius: 5.2},
expected: Circle[int]{center: NewPoint(-3, -4), radius: 5},
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
result := tt.circle.AsIntRounded()
assert.Equal(t, tt.expected.center, result.center)
assert.Equal(t, tt.expected.radius, result.radius)
})
}
}
func TestCircle_BoundingBox(t *testing.T) {
tests := map[string]struct {
circle Circle[int]
expected Rectangle[int]
}{
"Unit Circle": {
circle: NewCircle(NewPoint(0, 0), 1),
expected: NewRectangle([]Point[int]{
NewPoint(-1, -1),
NewPoint(1, -1),
NewPoint(1, 1),
NewPoint(-1, 1),
}),
},
"Circle at (10, 10) with radius 5": {
circle: NewCircle(NewPoint(10, 10), 5),
expected: NewRectangle([]Point[int]{
NewPoint(5, 5),
NewPoint(15, 5),
NewPoint(15, 15),
NewPoint(5, 15),
}),
},
"Circle at (-10, -10) with radius 3": {
circle: NewCircle(NewPoint(-10, -10), 3),
expected: NewRectangle([]Point[int]{
NewPoint(-13, -13),
NewPoint(-7, -13),
NewPoint(-7, -7),
NewPoint(-13, -7),
}),
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
boundingBox := tt.circle.BoundingBox()
assert.Equal(t, tt.expected, boundingBox)
})
}
}
func TestCircle_Bresenham(t *testing.T) {
tests := map[string]struct {
center Point[int]
radius int
expected []Point[int]
}{
"circle at origin, radius 2": {
center: NewPoint(0, 0),
radius: 2,
expected: []Point[int]{
NewPoint(-1, -2),
NewPoint(-1, -2),
NewPoint(-1, 2),
NewPoint(-1, 2),
NewPoint(-2, -1),
NewPoint(-2, -1),
NewPoint(-2, 0),
NewPoint(-2, 0),
NewPoint(-2, 1),
NewPoint(-2, 1),
NewPoint(0, -2),
NewPoint(0, -2),
NewPoint(0, 2),
NewPoint(0, 2),
NewPoint(1, -2),
NewPoint(1, -2),
NewPoint(1, 2),
NewPoint(1, 2),
NewPoint(2, -1),
NewPoint(2, -1),
NewPoint(2, 0),
NewPoint(2, 0),
NewPoint(2, 1),
NewPoint(2, 1),
},
},
"circle offset, radius 3": {
center: NewPoint(5, 5),
radius: 3,
expected: []Point[int]{
NewPoint(5, 8),
NewPoint(5, 8),
NewPoint(5, 2),
NewPoint(5, 2),
NewPoint(8, 5),
NewPoint(2, 5),
NewPoint(8, 5),
NewPoint(2, 5),
NewPoint(6, 8),
NewPoint(4, 8),
NewPoint(6, 2),
NewPoint(4, 2),
NewPoint(8, 6),
NewPoint(2, 6),
NewPoint(8, 4),
NewPoint(2, 4),
NewPoint(7, 7),
NewPoint(3, 7),
NewPoint(7, 3),
NewPoint(3, 3),
NewPoint(7, 7),
NewPoint(3, 7),
NewPoint(7, 3),
NewPoint(3, 3),
},
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
var actual []Point[int]
c := NewCircle(tc.center, tc.radius)
c.Bresenham(func(p Point[int]) bool {
actual = append(actual, p)
return true
})
assert.ElementsMatch(t, tc.expected, actual, "Points should match expected circle perimeter")
})
}
}
func TestCircle_Center(t *testing.T) {
tests := map[string]struct {
circle Circle[float64]
expected Point[float64]
}{
"positive center coordinates": {
circle: Circle[float64]{center: NewPoint[float64](3.5, 4.5), radius: 5.5},
expected: NewPoint[float64](3.5, 4.5),
},
"zero center coordinates": {
circle: Circle[float64]{center: NewPoint[float64](0.0, 0.0), radius: 5.5},
expected: NewPoint[float64](0.0, 0.0),
},
"negative center coordinates": {
circle: Circle[float64]{center: NewPoint[float64](-3.5, -4.5), radius: 5.5},
expected: NewPoint[float64](-3.5, -4.5),
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
result := tt.circle.Center()
assert.Equal(t, tt.expected, result)
})
}
}
func TestCircle_Circumference(t *testing.T) {
tests := map[string]struct {
circle Circle[float64]
expected float64
}{
"radius 1": {circle: Circle[float64]{center: NewPoint[float64](0, 0), radius: 1}, expected: 2 * math.Pi},
"radius 2": {circle: Circle[float64]{center: NewPoint[float64](0, 0), radius: 2}, expected: 4 * math.Pi},
"radius 0": {circle: Circle[float64]{center: NewPoint[float64](0, 0), radius: 0}, expected: 0},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
assert.InDelta(t, tt.expected, tt.circle.Circumference(), 0.001)
})
}
}
func TestCircle_Eq(t *testing.T) {
tests := map[string]struct {
circle1 Circle[float64]
circle2 Circle[float64]
expected bool
}{
"equal circles with same center and radius": {
circle1: Circle[float64]{center: NewPoint[float64](3.0, 4.0), radius: 5.0},
circle2: Circle[float64]{center: NewPoint[float64](3.0, 4.0), radius: 5.0},
expected: true,
},
"different center but same radius": {
circle1: Circle[float64]{center: NewPoint[float64](3.0, 4.0), radius: 5.0},
circle2: Circle[float64]{center: NewPoint[float64](2.0, 4.0), radius: 5.0},
expected: false,
},
"same center but different radius": {
circle1: Circle[float64]{center: NewPoint[float64](3.0, 4.0), radius: 5.0},
circle2: Circle[float64]{center: NewPoint[float64](3.0, 4.0), radius: 6.0},
expected: false,
},
"different center and different radius": {
circle1: Circle[float64]{center: NewPoint[float64](3.0, 4.0), radius: 5.0},
circle2: Circle[float64]{center: NewPoint[float64](2.0, 3.0), radius: 6.0},
expected: false,
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
result := tt.circle1.Eq(tt.circle2)
assert.Equal(t, tt.expected, result)
})
}
}
func TestCircle_Radius(t *testing.T) {
tests := map[string]struct {
circle Circle[float64]
expected float64
}{
"positive radius": {
circle: Circle[float64]{center: NewPoint[float64](3, 4), radius: 5},
expected: 5,
},
"zero radius": {
circle: Circle[float64]{center: NewPoint[float64](3, 4), radius: 0},
expected: 0,
},
"small radius": {
circle: Circle[float64]{center: NewPoint[float64](3, 4), radius: 0.001},
expected: 0.001,
},
"negative radius (edge case)": {
circle: Circle[float64]{center: NewPoint[float64](3, 4), radius: -5},
expected: -5,
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
result := tt.circle.Radius()
assert.Equal(t, tt.expected, result)
})
}
}
func TestCircle_RelationshipToCircle(t *testing.T) {
circleA := NewCircle(NewPoint[int](0, 0), 10)
circleB := NewCircle(NewPoint[int](0, 0), 10)
circleC := NewCircle(NewPoint[int](0, 0), 5)
circleD := NewCircle(NewPoint[int](20, 0), 10)
circleE := NewCircle(NewPoint[int](15, 0), 5)
circleF := NewCircle(NewPoint[int](25, 0), 5)
tests := map[string]struct {
circle1 Circle[int]
circle2 Circle[int]
expected Relationship
}{
"Equal Circles": {
circle1: circleA,
circle2: circleB,
expected: RelationshipEqual,
},
"ContainedBy Circle": {
circle1: circleC,
circle2: circleA,
expected: RelationshipContainedBy,
},
"Contains Circle": {
circle1: circleA,
circle2: circleC,
expected: RelationshipContains,
},
"Intersecting Circles": {
circle1: circleA,
circle2: circleD,
expected: RelationshipIntersection,
},
"Tangential Circles (Intersection)": {
circle1: circleA,
circle2: circleE,
expected: RelationshipIntersection,
},
"Disjoint Circles": {
circle1: circleA,
circle2: circleF,
expected: RelationshipDisjoint,
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
result := tt.circle1.RelationshipToCircle(tt.circle2, WithEpsilon(1e-10))
assert.Equal(t, tt.expected, result)
})
}
}
func TestCircle_RelationshipToLineSegment(t *testing.T) {
circle := NewCircle(NewPoint[int](0, 0), 10)
tests := map[string]struct {
line LineSegment[int]
expected Relationship
}{
"Line Segment Outside Circle": {
line: NewLineSegment(NewPoint(15, 0), NewPoint(20, 0)),
expected: RelationshipDisjoint,
},
"Line Segment Intersects Circle": {
line: NewLineSegment(NewPoint(0, -15), NewPoint(0, 15)),
expected: RelationshipIntersection,
},
"Line Segment Fully Contained in Circle": {
line: NewLineSegment(NewPoint(5, 5), NewPoint(-5, -5)),
expected: RelationshipContains,
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
result := circle.RelationshipToLineSegment(tt.line, WithEpsilon(1e-10))
assert.Equal(t, tt.expected, result)
})
}
}
func TestCircle_RelationshipToPoint(t *testing.T) {
circle := NewCircle(NewPoint[int](0, 0), 10)
tests := map[string]struct {
point Point[int]
expected Relationship
}{
"Point Outside Circle": {
point: NewPoint(15, 0),
expected: RelationshipDisjoint,
},
"Point On Circle Boundary": {
point: NewPoint(10, 0),
expected: RelationshipIntersection,
},
"Point Inside Circle": {
point: NewPoint(5, 0),
expected: RelationshipContains,
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
result := circle.RelationshipToPoint(tt.point, WithEpsilon(1e-10))
assert.Equal(t, tt.expected, result)
})
}
}
func TestCircle_RelationshipToPolyTree(t *testing.T) {
// Create a PolyTree
root, err := NewPolyTree([]Point[int]{
NewPoint(0, 0), NewPoint(0, 100), NewPoint(100, 100), NewPoint(100, 0),
}, PTSolid)
require.NoError(t, err)
hole, err := NewPolyTree([]Point[int]{
NewPoint(20, 20), NewPoint(20, 80), NewPoint(80, 80), NewPoint(80, 20),
}, PTHole)
require.NoError(t, err)
err = root.AddChild(hole)
require.NoError(t, err)
t.Run("Circle contains root polygon", func(t *testing.T) {
circle := NewCircle(NewPoint[int](50, 50), 600)
rels := circle.RelationshipToPolyTree(root)
assert.Equal(t, RelationshipContains, rels[root])
assert.Equal(t, RelationshipContains, rels[hole])
})
t.Run("Circle intersects root polygon", func(t *testing.T) {
circle := NewCircle(NewPoint[int](0, 0), 5)
rels := circle.RelationshipToPolyTree(root)
assert.Equal(t, RelationshipIntersection, rels[root])
assert.Equal(t, RelationshipDisjoint, rels[hole])
})
t.Run("Circle disjoint from root polygon", func(t *testing.T) {
circle := NewCircle(NewPoint[int](200, 200), 10)
rels := circle.RelationshipToPolyTree(root)
assert.Equal(t, RelationshipDisjoint, rels[root])
})
t.Run("Polygon contains circle", func(t *testing.T) {
circle := NewCircle(NewPoint[int](50, 50), 10)
rels := circle.RelationshipToPolyTree(root)
assert.Equal(t, RelationshipContainedBy, rels[root])
})
t.Run("Circle intersects hole", func(t *testing.T) {
circle := NewCircle(NewPoint[int](50, 50), 70)
rels := circle.RelationshipToPolyTree(root)
assert.Equal(t, RelationshipContains, rels[hole])
assert.Equal(t, RelationshipIntersection, rels[root])
})
}
func TestCircle_RelationshipToRectangle(t *testing.T) {
// Define a rectangle
rect := NewRectangle([]Point[int]{
NewPoint(0, 0),
NewPoint(100, 0),
NewPoint(100, 100),
NewPoint(0, 100),
})
t.Run("Disjoint", func(t *testing.T) {
circle := NewCircle(NewPoint(-50, -50), 10)
assert.Equal(t, RelationshipDisjoint, circle.RelationshipToRectangle(rect), "Expected RelationshipDisjoint")
})
t.Run("Intersection", func(t *testing.T) {
circle := NewCircle(NewPoint(50, 120), 30)
assert.Equal(t, RelationshipIntersection, circle.RelationshipToRectangle(rect), "Expected RelationshipIntersection")
})
t.Run("Circle Contains Rectangle", func(t *testing.T) {
circle := NewCircle(NewPoint(50, 50), 200)
assert.Equal(t, RelationshipContains, circle.RelationshipToRectangle(rect), "Expected RelationshipContains")
})
t.Run("Rectangle Contains Circle", func(t *testing.T) {
circle := NewCircle(NewPoint(50, 50), 20)
assert.Equal(t, RelationshipContainedBy, circle.RelationshipToRectangle(rect), "Expected RelationshipContainedBy")
})
}
func TestCircle_Rotate(t *testing.T) {
tests := map[string]struct {
circle Circle[float64]
pivot Point[float64]
radians float64
expected Circle[float64]
}{
"rotate 90 degrees around origin": {
circle: NewCircle(NewPoint[float64](3.0, 0.0), 5.0),
pivot: NewPoint[float64](0.0, 0.0),
radians: math.Pi / 2,
expected: NewCircle(
NewPoint[float64](0.0, 3.0),
5.0,
),
},
"rotate 180 degrees around origin": {
circle: NewCircle(NewPoint[float64](3.0, 0.0), 5.0),
pivot: NewPoint[float64](0.0, 0.0),
radians: math.Pi,
expected: NewCircle(
NewPoint[float64](-3.0, 0.0),
5.0,
),
},
"rotate 90 degrees around custom pivot": {
circle: NewCircle(NewPoint[float64](3.0, 0.0), 5.0),
pivot: NewPoint[float64](1.0, 1.0),
radians: math.Pi / 2,
expected: NewCircle(
NewPoint[float64](2.0, 3.0),
5.0,
),
},
"rotate 0 degrees around custom pivot": {
circle: NewCircle(NewPoint[float64](3.0, 0.0), 5.0),
pivot: NewPoint[float64](1.0, 1.0),
radians: 0,
expected: NewCircle(
NewPoint[float64](3.0, 0),
5.0,
),
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
result := tt.circle.Rotate(tt.pivot, tt.radians)
assert.InDelta(t, tt.expected.center.x, result.center.x, 0.0001)
assert.InDelta(t, tt.expected.center.y, result.center.y, 0.0001)
assert.Equal(t, tt.expected.radius, result.radius)
})
}
}
func TestCircle_Scale(t *testing.T) {
tests := map[string]struct {
circle Circle[float64]
factor float64
expected Circle[float64]
}{
"scale up by factor of 2": {
circle: Circle[float64]{center: NewPoint[float64](3, 4), radius: 5},
factor: 2,
expected: Circle[float64]{center: NewPoint[float64](3, 4), radius: 10},
},
"scale down by factor of 0.5": {
circle: Circle[float64]{center: NewPoint[float64](3, 4), radius: 5},
factor: 0.5,
expected: Circle[float64]{center: NewPoint[float64](3, 4), radius: 2.5},
},
"no change with factor of 1": {
circle: Circle[float64]{center: NewPoint[float64](3, 4), radius: 5},
factor: 1,
expected: Circle[float64]{center: NewPoint[float64](3, 4), radius: 5},
},
"scale to zero radius with factor of 0": {
circle: Circle[float64]{center: NewPoint[float64](3, 4), radius: 5},
factor: 0,
expected: Circle[float64]{center: NewPoint[float64](3, 4), radius: 0},
},
"scale with negative factor": {
circle: Circle[float64]{center: NewPoint[float64](3, 4), radius: 5},
factor: -2,
expected: Circle[float64]{center: NewPoint[float64](3, 4), radius: -10},
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
result := tt.circle.Scale(tt.factor)
assert.Equal(t, tt.expected.center, result.center)
assert.Equal(t, tt.expected.radius, result.radius)
})
}
}
func TestCircle_String(t *testing.T) {
tests := map[string]struct {
circle Circle[float64]
expected string
}{
"positive center and radius": {
circle: Circle[float64]{center: NewPoint[float64](3.5, 4.5), radius: 5.5},
expected: "Circle[center=(3.5, 4.5), radius=5.5]",
},
"zero center and radius": {
circle: Circle[float64]{center: NewPoint[float64](0.0, 0.0), radius: 0.0},
expected: "Circle[center=(0, 0), radius=0]",
},
"negative center and radius": {
circle: Circle[float64]{center: NewPoint[float64](-3.5, -4.5), radius: -5.5},
expected: "Circle[center=(-3.5, -4.5), radius=-5.5]",
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
result := tt.circle.String()
assert.Equal(t, tt.expected, result)
})
}
}
func TestCircle_Translate(t *testing.T) {
tests := map[string]struct {
circle Circle[float64]
vector Point[float64]
expected Circle[float64]
}{
"translate circle by positive vector": {
circle: Circle[float64]{center: NewPoint[float64](3, 4), radius: 5},
vector: NewPoint[float64](2, 3),
expected: Circle[float64]{center: NewPoint[float64](5, 7), radius: 5},
},
"translate circle by negative vector": {
circle: Circle[float64]{center: NewPoint[float64](3, 4), radius: 5},
vector: NewPoint[float64](-1, -2),
expected: Circle[float64]{center: NewPoint[float64](2, 2), radius: 5},
},
"translate circle by zero vector": {
circle: Circle[float64]{center: NewPoint[float64](3, 4), radius: 5},
vector: NewPoint[float64](0, 0),
expected: Circle[float64]{center: NewPoint[float64](3, 4), radius: 5},
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
result := tc.circle.Translate(tc.vector)
assert.Equal(t, tc.expected.center, result.center)
assert.Equal(t, tc.expected.radius, result.radius)
})
}
}
func TestNewCircle(t *testing.T) {
tests := map[string]struct {
center Point[float64]
radius float64
expected Circle[float64]
}{
"positive center and radius": {
center: NewPoint[float64](3.0, 4.0),
radius: 5.0,
expected: Circle[float64]{center: NewPoint[float64](3.0, 4.0), radius: 5.0},
},
"zero center and radius": {
center: NewPoint[float64](0.0, 0.0),
radius: 0.0,
expected: Circle[float64]{center: NewPoint[float64](0.0, 0.0), radius: 0.0},
},
"negative center and positive radius": {
center: NewPoint[float64](-3.0, -4.0),
radius: 5.0,
expected: Circle[float64]{center: NewPoint[float64](-3.0, -4.0), radius: 5.0},
},
"positive center and negative radius (edge case)": {
center: NewPoint[float64](3.0, 4.0),
radius: -5.0,
expected: Circle[float64]{center: NewPoint[float64](3.0, 4.0), radius: -5.0},
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
result := NewCircle(tt.center, tt.radius)
assert.Equal(t, tt.expected.center, result.center)
assert.Equal(t, tt.expected.radius, result.radius)
})
}
}
func TestReflectAcrossCircleOctants(t *testing.T) {
tests := map[string]struct {
xc, yc, x, y int
expected []Point[int]
}{
"center at origin, simple point": {
xc: 0, yc: 0, x: 2, y: 1,
expected: []Point[int]{
NewPoint(2, 1), // Octant 1
NewPoint(-2, 1), // Octant 2
NewPoint(2, -1), // Octant 8
NewPoint(-2, -1), // Octant 7
NewPoint(1, 2), // Octant 3
NewPoint(-1, 2), // Octant 4
NewPoint(1, -2), // Octant 6
NewPoint(-1, -2), // Octant 5
},
},
"center offset, simple point": {
xc: 3, yc: 4, x: 2, y: 1,
expected: []Point[int]{
NewPoint(5, 5), // Octant 1
NewPoint(1, 5), // Octant 2
NewPoint(5, 3), // Octant 8
NewPoint(1, 3), // Octant 7
NewPoint(4, 6), // Octant 3
NewPoint(2, 6), // Octant 4
NewPoint(4, 2), // Octant 6
NewPoint(2, 2), // Octant 5
},
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
actual := reflectAcrossCircleOctants(tc.xc, tc.yc, tc.x, tc.y)
assert.Equal(t, tc.expected, actual, "Points should match expected octant reflections")
})
}
}