@@ -6,43 +6,40 @@ import (
66 "github.com/gravitton/assert"
77)
88
9- func TestPolygon_Center (t * testing.T ) {
10- p := Polygon [int ]{Vertices : []Point [int ]{P (0 , 0 ), P (2 , 0 ), P (2 , 2 ), P (0 , 2 )}}
11- testPoint (t , p .Center (), 1 , 1 )
9+ func TestPolygon_New (t * testing.T ) {
10+ vertices := []Point [int ]{P (0 , 0 ), P (2 , 0 ), P (2 , 2 ), P (0 , 2 )}
1211
13- pf := Polygon [float64 ]{Vertices : []Point [float64 ]{P (0.0 , 0.0 ), P (2.0 , 0.0 ), P (2.0 , 1.0 )}}
14- cx , cy := pf .Center ().XY ()
15- assert .EqualDelta (t , cx , 4.0 / 3.0 , Delta )
16- assert .EqualDelta (t , cy , 1.0 / 3.0 , Delta )
12+ testPolygon (t , Polygon [int ]{vertices }, vertices )
1713}
1814
19- func TestPolygon_Translate_MoveTo (t * testing.T ) {
20- p := Polygon [int ]{Vertices : []Point [int ]{P (0 , 0 ), P (2 , 0 )}}
21- p2 := p .Translate (V (1 , - 1 ))
22- testPoint (t , p2 .Vertices [0 ], 1 , - 1 )
23- testPoint (t , p2 .Vertices [1 ], 3 , - 1 )
24-
25- p3 := p .MoveTo (P (10 , 10 ))
26- testPoint (t , p3 .Center (), 10 , 10 )
15+ func TestPolygon_Center (t * testing.T ) {
16+ testPoint (t , Polygon [int ]{[]Point [int ]{P (0 , 0 ), P (2 , 0 ), P (2 , 2 ), P (0 , 2 )}}.Center (), 1 , 1 )
17+ testPoint (t , Polygon [float64 ]{[]Point [float64 ]{P (0.0 , 0.0 ), P (2.0 , 0.0 ), P (2.0 , 1.0 )}}.Center (), 4.0 / 3.0 , 1.0 / 3.0 )
2718}
2819
29- func TestPolygon_Scale (t * testing.T ) {
30- p := Polygon [float64 ]{Vertices : []Point [float64 ]{P (1.0 , 0.0 ), P (0.0 , 1.0 ), P (- 1.0 , 0.0 ), P (0.0 , - 1.0 )}}
31- center := p .Center ()
32- assert .True (t , center .Equal (P (0.0 , 0.0 )))
20+ func TestPolygon_Translate (t * testing.T ) {
21+ testPolygon (t , Polygon [int ]{[]Point [int ]{P (0 , 0 ), P (2 , 0 )}}.Translate (V (1 , - 1 )), []Point [int ]{
22+ P (1 , - 1 ),
23+ P (3 , - 1 ),
24+ })
25+ }
3326
34- p2 := p . Scale ( 2 )
35- // Every point should double its distance from center (0,0).
36- assert . EqualDelta ( t , p2 . Vertices [ 0 ]. X , 2.0 , Delta )
37- assert . EqualDelta ( t , p2 . Vertices [ 0 ]. Y , 0.0 , Delta )
38- assert . EqualDelta ( t , p2 . Vertices [ 1 ]. X , 0.0 , Delta )
39- assert . EqualDelta ( t , p2 . Vertices [ 1 ]. Y , 2.0 , Delta )
27+ func TestPolygon_MoveTo ( t * testing. T ) {
28+ testPolygon ( t , Polygon [ int ]{ Vertices : [] Point [ int ]{ P ( 0 , 0 ), P ( 2 , 0 )}}. MoveTo ( P ( 10 , 10 )), [] Point [ int ]{
29+ P ( 9 , 10 ),
30+ P ( 11 , 10 ),
31+ } )
32+ }
4033
41- p3 := p .ScaleXY (2 , 0.5 )
42- assert .EqualDelta (t , p3 .Vertices [0 ].X , 2.0 , Delta )
43- assert .EqualDelta (t , p3 .Vertices [0 ].Y , 0.0 , Delta )
44- assert .EqualDelta (t , p3 .Vertices [1 ].X , 0.0 , Delta )
45- assert .EqualDelta (t , p3 .Vertices [1 ].Y , 0.5 , Delta )
34+ func TestPolygon_Scale (t * testing.T ) {
35+ testPolygon (t , Polygon [int ]{Vertices : []Point [int ]{P (0 , 0 ), P (2 , 0 )}}.Scale (2 ), []Point [int ]{
36+ P (- 1 , 0 ),
37+ P (3 , 0 ),
38+ })
39+ testPolygon (t , Polygon [float64 ]{Vertices : []Point [float64 ]{P (0.0 , 0.0 ), P (2.0 , 1.0 )}}.ScaleXY (0.5 , 2.5 ), []Point [float64 ]{
40+ P (0.5 , - 0.75 ),
41+ P (1.5 , 1.75 ),
42+ })
4643}
4744
4845func TestPolygon_Immutable (t * testing.T ) {
@@ -53,7 +50,6 @@ func TestPolygon_Immutable(t *testing.T) {
5350 p .Scale (2 )
5451 p .ScaleXY (2 , 3 )
5552
56- // unchanged
5753 testPoint (t , p .Vertices [0 ], 0 , 0 )
5854 testPoint (t , p .Vertices [1 ], 2 , 0 )
5955}
0 commit comments