diff --git a/cmd/gopatterns/gopatterns.go b/cmd/gopatterns/gopatterns.go index 38d3379..89913ab 100644 --- a/cmd/gopatterns/gopatterns.go +++ b/cmd/gopatterns/gopatterns.go @@ -40,6 +40,10 @@ gopatterns [-templates ] [-out ] input-file `) } + if debug { + slog.SetLogLoggerLevel(slog.LevelDebug) + } + args := flag.Args() if len(args) == 0 { slog.Error("at least one pattern is required") diff --git a/pkg/pattern/path/splines.go b/pkg/pattern/path/splines.go index 8f1f1fa..ecf1c67 100644 --- a/pkg/pattern/path/splines.go +++ b/pkg/pattern/path/splines.go @@ -5,6 +5,7 @@ import ( "git.wtrh.nl/patterns/gopatterns/pkg/pattern/point" "github.com/tdewolff/canvas" splines "gitlab.com/Achilleshiel/gosplines" + "log/slog" ) const resolution = 40 @@ -44,8 +45,41 @@ func NewSpline(opts SplineOpts) Spline { // Draw the spline to the provided [canvas.Canvas]. func (s Spline) Draw(c *canvas.Canvas) error { + points, err := s.build() + if err != nil { + return fmt.Errorf("generating spline: %w", err) + } + + path := NewPath(points, s.style) + + if err = path.Draw(c); err != nil { + return fmt.Errorf("draw spline points to canvas: %w", err) + } + + length, _ := s.Length() + slog.Debug("Draw spline", "length", length, "from", points[0].Name(), "to", points[len(points)-1].Name()) + + return nil +} + +func (s Spline) Length() (float64, error) { + points, err := s.build() + if err != nil { + return 0.0, fmt.Errorf("generating spline: %w", err) + } + + length := 0.0 + + for i := range points[1:] { + length += points[i].Position().Distance(points[i+1].Position()) + } + + return length, nil +} + +func (s Spline) build() (points []point.Point, err error) { if len(s.points) < 2 { - return nil + return s.points, nil } x := make([]float64, len(s.points)) @@ -60,15 +94,15 @@ func (s Spline) Draw(c *canvas.Canvas) error { xCoefficient, err := splines.SolveSplineWithConstraint(x, diffStart.X, diffEnd.X) if err != nil { - return fmt.Errorf("unable to calculate coefficients for x: %w", err) + return nil, fmt.Errorf("unable to calculate coefficients for x: %w", err) } yCoefficient, err := splines.SolveSplineWithConstraint(y, diffStart.Y, diffEnd.Y) if err != nil { - return fmt.Errorf("unable to calculate coefficients for y: %w", err) + return nil, fmt.Errorf("unable to calculate coefficients for y: %w", err) } - points := make([]point.Point, 0, len(x)*resolution) + points = make([]point.Point, 0, len(x)*resolution) stepSize := 1.0 / float64(resolution-1) for i := range len(s.points) - 1 { @@ -83,11 +117,5 @@ func (s Spline) Draw(c *canvas.Canvas) error { points = append(points, s.points[len(s.points)-1]) - path := NewPath(points, s.style) - - if err = path.Draw(c); err != nil { - return fmt.Errorf("draw spline points to canvas: %w", err) - } - - return nil + return points, nil }