Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

63 lignes
1.3KB

  1. // Package path provides objects to define lines on a sewing pattern.
  2. package path
  3. import (
  4. "git.wtrh.nl/patterns/gopatterns/pkg/point"
  5. "git.wtrh.nl/patterns/gopatterns/pkg/util"
  6. "github.com/tdewolff/canvas"
  7. )
  8. // Polygon defines a set of straight lines through points.
  9. type Polygon struct {
  10. points []point.Point
  11. style Style
  12. id util.ID
  13. }
  14. func (p *Polygon) Through() []point.Point {
  15. return p.points
  16. }
  17. // NewPolygon returns a new [Polygon].
  18. func NewPolygon(points []point.Point, style Style, id util.ID) *Polygon {
  19. return &Polygon{points: points, style: style, id: id}
  20. }
  21. // WithStyle updates the style of the Polygon.
  22. func (p *Polygon) WithStyle(Style Style) *Polygon {
  23. p.style = Style
  24. return p
  25. }
  26. // Draw the path to the provided [canvas.Canvas].
  27. func (p *Polygon) Draw(c *canvas.Canvas) error {
  28. polyline := canvas.Polyline{}
  29. for _, next := range p.points {
  30. polyline.Add(next.Vector().Values())
  31. }
  32. c.RenderPath(polyline.ToPath(), p.style.ToCanvas(), canvas.Identity)
  33. return nil
  34. }
  35. func (p *Polygon) Length() (float64, error) {
  36. length := 0.0
  37. for i := range p.points[1:] {
  38. length += p.points[i].Position().Distance(p.points[i+1].Position())
  39. }
  40. return length, nil
  41. }
  42. func (p *Polygon) ID() util.ID {
  43. return p.id
  44. }
  45. type Path interface {
  46. Draw(c *canvas.Canvas) error
  47. Length() (float64, error)
  48. Through() []point.Point
  49. }