|
| 1 | +package geom |
| 2 | + |
| 3 | +import ( |
| 4 | + "math" |
| 5 | +) |
| 6 | + |
| 7 | +// RegularPolygon is a polygon with equally spaced transform around a center. |
| 8 | +type RegularPolygon[T Number] struct { |
| 9 | + Center Point[T] |
| 10 | + Size Size[T] |
| 11 | + N int |
| 12 | +} |
| 13 | + |
| 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 transform. |
| 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 transform. |
| 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 transform. |
| 30 | +func Hexagon[T Number](center Point[T], size Size[T]) RegularPolygon[T] { |
| 31 | + return RegularPolygon[T]{center, size, 6} |
| 32 | +} |
| 33 | + |
| 34 | +// Translate creates a new RegularPolygon translated by the given vector. |
| 35 | +func (rp RegularPolygon[T]) Translate(change Vector[T]) RegularPolygon[T] { |
| 36 | + return RegularPolygon[T]{rp.Center.Add(change), rp.Size, rp.N} |
| 37 | +} |
| 38 | + |
| 39 | +// MoveTo creates a new RegularPolygon with the same size and sides centered at point. |
| 40 | +func (rp RegularPolygon[T]) MoveTo(point Point[T]) RegularPolygon[T] { |
| 41 | + return RegularPolygon[T]{point, rp.Size, rp.N} |
| 42 | +} |
| 43 | + |
| 44 | +// Multiple creates a new RegularPolygon with size scaled by the given factor. |
| 45 | +func (rp RegularPolygon[T]) Scale(scale float64) RegularPolygon[T] { |
| 46 | + return RegularPolygon[T]{rp.Center, rp.Size.Scale(scale), rp.N} |
| 47 | +} |
| 48 | + |
| 49 | +// Multiple creates a new RegularPolygon with size scaled by the given factors. |
| 50 | +func (rp RegularPolygon[T]) ScaleXY(scaleX, scaleY float64) RegularPolygon[T] { |
| 51 | + return RegularPolygon[T]{rp.Center, rp.Size.ScaleXY(scaleX, scaleY), rp.N} |
| 52 | +} |
| 53 | + |
| 54 | +// Vertices returns the polygon transform in order starting from angle 0, counter-clockwise. |
| 55 | +func (rp RegularPolygon[T]) Vertices() []Point[T] { |
| 56 | + initAngle := 0.0 |
| 57 | + angleStep := 2 * math.Pi / float64(rp.N) |
| 58 | + |
| 59 | + vertices := make([]Point[T], rp.N) |
| 60 | + for i := 0; i < rp.N; i++ { |
| 61 | + vertices[i] = rp.Center.Add(VecFromAngle[T](initAngle+float64(i)*angleStep, 1).MultiplyXY(float64(rp.Size.Width), float64(rp.Size.Height))) |
| 62 | + } |
| 63 | + |
| 64 | + return vertices |
| 65 | +} |
| 66 | + |
| 67 | +// ToPolygon converts the regular polygon into a generic Polygon with computed vertices. |
| 68 | +func (rp RegularPolygon[T]) ToPolygon() Polygon[T] { |
| 69 | + return Polygon[T]{rp.Vertices()} |
| 70 | +} |
0 commit comments