@@ -9,55 +9,48 @@ type RegularPolygon[T Number] struct {
99 Center Point [T ]
1010 Size Size [T ]
1111 N int
12+ Angle float64
1213}
1314
14- // RP is shorthand for RegularPolygon{center, size, n}.
15- func RP [T Number ](center Point [T ], size Size [T ], n int ) RegularPolygon [T ] {
16- return RegularPolygon [T ]{center , size , n }
17- }
18-
19- // Triangle creates a RegularPolygon with 3 vertices.
20- func Triangle [T Number ](center Point [T ], size Size [T ]) RegularPolygon [T ] {
21- return RegularPolygon [T ]{center , size , 3 }
22- }
23-
24- // Square creates a RegularPolygon with 4 vertices.
25- func Square [T Number ](center Point [T ], size Size [T ]) RegularPolygon [T ] {
26- return RegularPolygon [T ]{center , size , 4 }
27- }
28-
29- // Hexagon creates a RegularPolygon with 6 vertices.
30- func Hexagon [T Number ](center Point [T ], size Size [T ]) RegularPolygon [T ] {
31- return RegularPolygon [T ]{center , size , 6 }
15+ // RP is shorthand for RegularPolygon{center, size, n, angle}.
16+ func RP [T Number ](center Point [T ], size Size [T ], n int , angle float64 ) RegularPolygon [T ] {
17+ return RegularPolygon [T ]{center , size , n , angle }
3218}
3319
3420// Translate creates a new RegularPolygon translated by the given vector.
3521func (rp RegularPolygon [T ]) Translate (change Vector [T ]) RegularPolygon [T ] {
36- return RegularPolygon [T ]{rp .Center .Add (change ), rp .Size , rp .N }
22+ return RegularPolygon [T ]{rp .Center .Add (change ), rp .Size , rp .N , rp . Angle }
3723}
3824
3925// MoveTo creates a new RegularPolygon with center at point.
4026func (rp RegularPolygon [T ]) MoveTo (point Point [T ]) RegularPolygon [T ] {
41- return RegularPolygon [T ]{point , rp .Size , rp .N }
27+ return RegularPolygon [T ]{point , rp .Size , rp .N , rp . Angle }
4228}
4329
4430// Multiple creates a new RegularPolygon with size scaled by the given factor.
4531func (rp RegularPolygon [T ]) Scale (factor float64 ) RegularPolygon [T ] {
46- return RegularPolygon [T ]{rp .Center , rp .Size .Scale (factor ), rp .N }
32+ return RegularPolygon [T ]{rp .Center , rp .Size .Scale (factor ), rp .N , rp . Angle }
4733}
4834
4935// Multiple creates a new RegularPolygon with size scaled by the given factors.
5036func (rp RegularPolygon [T ]) ScaleXY (factorX , factorY float64 ) RegularPolygon [T ] {
51- return RegularPolygon [T ]{rp .Center , rp .Size .ScaleXY (factorX , factorY ), rp .N }
37+ return RegularPolygon [T ]{rp .Center , rp .Size .ScaleXY (factorX , factorY ), rp .N , rp .Angle }
38+ }
39+
40+ // Rotate creates a new RegularPolygon rotated by the given angle (in radians).
41+ func (rp RegularPolygon [T ]) Rotate (angle float64 ) RegularPolygon [T ] {
42+ // TODO: normalize angle to [0, 2*pi]
43+ return RegularPolygon [T ]{rp .Center , rp .Size , rp .N , rp .Angle + angle }
5244}
5345
5446// Vertices returns the polygon vertices in order starting from angle 0, counter-clockwise.
5547func (rp RegularPolygon [T ]) Vertices () []Point [T ] {
56- initAngle := 0.0
57- angleStep := 2 * math .Pi / float64 (rp .N )
48+ initAngle := rp . Angle
49+ angleStep := ( 2 * math .Pi ) / float64 (rp .N )
5850
5951 vertices := make ([]Point [T ], rp .N )
6052 for i := 0 ; i < rp .N ; i ++ {
53+ // TODO: V(1,0).Rotate(angle)
6154 vertices [i ] = rp .Center .Add (VecFromAngle [T ](initAngle + float64 (i )* angleStep , 1 ).MultiplyXY (float64 (rp .Size .Width ), float64 (rp .Size .Height )))
6255 }
6356
@@ -66,7 +59,9 @@ func (rp RegularPolygon[T]) Vertices() []Point[T] {
6659
6760// Bounds returns the axis-aligned bounding rectangle.
6861func (rp RegularPolygon [T ]) Bounds () Rectangle [T ] {
69- return Rectangle [T ]{rp .Center , rp .Size }
62+ // TODO: calculate
63+ maxAbsCos , maxAbsSin := 1.0 , 1.0
64+ return Rectangle [T ]{rp .Center , rp .Size .ScaleXY (2.0 * maxAbsCos , 2.0 * maxAbsSin )}
7065}
7166
7267// ToPolygon converts the regular polygon into a generic Polygon with computed vertices.
@@ -88,3 +83,38 @@ func (rp RegularPolygon[T]) IsZero() bool {
8883func (rp RegularPolygon [T ]) Empty () bool {
8984 return rp .N == 0
9085}
86+
87+ type Orientation int
88+
89+ const (
90+ FlatTop Orientation = iota
91+ PointTop
92+ )
93+
94+ func RegularPolygonAngle (n int , orientation Orientation ) float64 {
95+ switch orientation {
96+ case FlatTop :
97+ // 90 - 180/n degrees
98+ return math .Pi * float64 (n - 2 ) / (2 * float64 (n ))
99+ case PointTop :
100+ // 90 degrees
101+ return math .Pi / 2
102+ default :
103+ return 0
104+ }
105+ }
106+
107+ // Triangle creates a RegularPolygon with 3 vertices.
108+ func Triangle [T Number ](center Point [T ], size Size [T ], orientation Orientation ) RegularPolygon [T ] {
109+ return RegularPolygon [T ]{center , size , 3 , RegularPolygonAngle (3 , orientation )}
110+ }
111+
112+ // Square creates a RegularPolygon with 4 vertices.
113+ func Square [T Number ](center Point [T ], size Size [T ], orientation Orientation ) RegularPolygon [T ] {
114+ return RegularPolygon [T ]{center , size , 4 , RegularPolygonAngle (4 , orientation )}
115+ }
116+
117+ // Hexagon creates a RegularPolygon with 6 vertices.
118+ func Hexagon [T Number ](center Point [T ], size Size [T ], orientation Orientation ) RegularPolygon [T ] {
119+ return RegularPolygon [T ]{center , size , 6 , RegularPolygonAngle (6 , orientation )}
120+ }
0 commit comments