|
- // Package path provides objects to define lines on a sewing pattern.
- package path
-
- import (
- "git.wtrh.nl/patterns/gopatterns/pkg/point"
- "git.wtrh.nl/patterns/gopatterns/pkg/util"
- "github.com/tdewolff/canvas"
- )
-
- // Polygon defines a set of straight lines through points.
- type Polygon struct {
- points []point.Point
- style Style
- id util.ID
- }
-
- func (p *Polygon) Through() []point.Point {
- return p.points
- }
-
- // NewPolygon returns a new [Polygon].
- func NewPolygon(points []point.Point, style Style, id util.ID) *Polygon {
- return &Polygon{points: points, style: style}
- }
-
- // WithStyle updates the style of the Polygon.
- func (p *Polygon) WithStyle(Style Style) *Polygon {
- p.style = Style
- return p
- }
-
- // Draw the path to the provided [canvas.Canvas].
- func (p *Polygon) Draw(c *canvas.Canvas) error {
- polyline := canvas.Polyline{}
- for _, next := range p.points {
- polyline.Add(next.Vector().Values())
- }
-
- c.RenderPath(polyline.ToPath(), p.style.ToCanvas(), canvas.Identity)
-
- return nil
- }
-
- func (p *Polygon) Length() (float64, error) {
- length := 0.0
-
- for i := range p.points[1:] {
- length += p.points[i].Position().Distance(p.points[i+1].Position())
- }
-
- return length, nil
- }
-
- func (p *Polygon) ID() util.ID {
- return p.id
- }
-
- type Path interface {
- Draw(c *canvas.Canvas) error
- Length() (float64, error)
- Through() []point.Point
- }
|