| @@ -6,10 +6,9 @@ linters: | |||
| - nlreturn # covered by wsl cuddle rules | |||
| - nonamedreturns # named returns are accepted | |||
| - mnd | |||
| - gomnd | |||
| # deprecated | |||
| - exhaustruct | |||
| - execinquery | |||
| - tenv | |||
| severity: | |||
| default-severity: major | |||
| @@ -2,33 +2,25 @@ | |||
| package path | |||
| import ( | |||
| "image/color" | |||
| "git.wtrh.nl/patterns/gopatterns/pkg/pattern/point" | |||
| "github.com/tdewolff/canvas" | |||
| ) | |||
| // Path defines a set of straight lines through points. | |||
| type Path struct { | |||
| points []point.Point | |||
| thickness float64 | |||
| color color.RGBA | |||
| points []point.Point | |||
| style Style | |||
| } | |||
| // NewPath returns a new [Path]. | |||
| func NewPath(points ...point.Point) *Path { | |||
| black := canvas.Black | |||
| return &Path{points: points, color: black, thickness: 0.2} | |||
| } | |||
| // NewPathWithStyle returns a new [Path] with the specified thickness and color. | |||
| func NewPathWithStyle(thickness float64, color color.RGBA, points ...point.Point) Path { | |||
| return Path{points: points, color: color, thickness: thickness} | |||
| func NewPath(points []point.Point) *Path { | |||
| return &Path{points: points, style: NewDefaultStyle()} | |||
| } | |||
| // SetThickness updates the tickness of the line | |||
| func (p *Path) SetThickness(thickness float64) { | |||
| p.thickness = thickness | |||
| // WithStyle updates the style of the Path. | |||
| func (p *Path) WithStyle(Style Style) *Path { | |||
| p.style = Style | |||
| return p | |||
| } | |||
| // Draw the path to the provided [canvas.Canvas]. | |||
| @@ -38,17 +30,7 @@ func (p *Path) Draw(c *canvas.Canvas) error { | |||
| polyline.Add(next.Vector().Values()) | |||
| } | |||
| c.RenderPath(polyline.ToPath(), | |||
| canvas.Style{ | |||
| Fill: canvas.Paint{}, | |||
| Stroke: canvas.Paint{Color: p.color}, | |||
| StrokeWidth: p.thickness, | |||
| StrokeCapper: canvas.RoundCap, | |||
| StrokeJoiner: canvas.BevelJoin, | |||
| DashOffset: 0, | |||
| Dashes: nil, | |||
| FillRule: 0, | |||
| }, canvas.Identity) | |||
| c.RenderPath(polyline.ToPath(), p.style.ToCanvas(), canvas.Identity) | |||
| return nil | |||
| } | |||
| @@ -2,6 +2,7 @@ package path | |||
| import ( | |||
| "fmt" | |||
| "image/color" | |||
| "git.wtrh.nl/patterns/gopatterns/pkg/pattern/point" | |||
| "github.com/tdewolff/canvas" | |||
| @@ -19,9 +20,9 @@ type Spline struct { | |||
| // NewSpline returns a new spline through points. Start and end points can be provided as | |||
| // the start and stop direction of the spline. When start or end point arguments are left nil there | |||
| // are no constraints on the direction. | |||
| func NewSpline(start, end point.Point, points ...point.Point) Spline { | |||
| func NewSpline(start, end point.Point, points []point.Point) Spline { | |||
| s := Spline{ | |||
| Path: NewPath(points...), | |||
| Path: NewPath(points), | |||
| start: start, | |||
| end: end, | |||
| } | |||
| @@ -78,8 +79,11 @@ func (p Spline) Draw(c *canvas.Canvas) error { | |||
| points = append(points, p.points[len(p.points)-1]) | |||
| path := NewPath(points...) | |||
| path.SetThickness(p.thickness) | |||
| path := NewPath(points).WithStyle(Style{ | |||
| Thickness: p.Path.style.Thickness, | |||
| Color: color.RGBA{}, | |||
| Dashes: nil, | |||
| }) | |||
| if err = path.Draw(c); err != nil { | |||
| return fmt.Errorf("draw spline points to canvas: %w", err) | |||
| @@ -0,0 +1,33 @@ | |||
| package path | |||
| import ( | |||
| "image/color" | |||
| "github.com/tdewolff/canvas" | |||
| ) | |||
| type Style struct { | |||
| Thickness float64 | |||
| Color color.RGBA | |||
| Dashes []float64 | |||
| } | |||
| func (s Style) ToCanvas() canvas.Style { | |||
| return canvas.Style{ | |||
| Fill: canvas.Paint{}, | |||
| Stroke: canvas.Paint{Color: s.Color}, | |||
| StrokeWidth: s.Thickness, | |||
| StrokeCapper: canvas.RoundCap, | |||
| StrokeJoiner: canvas.BevelJoin, | |||
| DashOffset: 0, | |||
| Dashes: s.Dashes, | |||
| FillRule: 0, | |||
| } | |||
| } | |||
| func NewDefaultStyle() Style { | |||
| return Style{ | |||
| Thickness: 0.2, | |||
| Color: canvas.Black, | |||
| } | |||
| } | |||
| @@ -11,7 +11,7 @@ import ( | |||
| "gopkg.in/Knetic/govaluate.v3" | |||
| ) | |||
| // Pattern contains all the points, lines and dimensions to draw a pattern to a canvas. | |||
| // The Pattern contains all the points, lines and dimensions to draw a pattern to a canvas. | |||
| type Pattern struct { | |||
| points map[point.ID]point.Point | |||
| lines []pathDrawer | |||
| @@ -34,7 +34,7 @@ func (p *Pattern) AddLine(line pathDrawer) { | |||
| } | |||
| // GetPoints returns a slice with points for the given IDs. | |||
| func (p *Pattern) GetPoints(id ...point.ID) []point.Point { | |||
| func (p *Pattern) GetPoints(id []point.ID) []point.Point { | |||
| points := make([]point.Point, 0, len(id)) | |||
| for _, i := range id { | |||
| points = append(points, p.GetPoint(i)) | |||
| @@ -28,7 +28,7 @@ type Curve struct { | |||
| // Build adds the line to the provided [pattern.Pattern]. | |||
| func (l Line) Build(pat *pattern.Pattern) error { | |||
| points := pat.GetPoints(l.Through...) | |||
| points := pat.GetPoints(l.Through) | |||
| for _, p := range points { | |||
| p.SetDraw() | |||
| } | |||
| @@ -37,7 +37,7 @@ func (l Line) Build(pat *pattern.Pattern) error { | |||
| case l.Curve != nil: | |||
| startPoint := pat.GetPoint(l.Curve.Start) | |||
| endPoint := pat.GetPoint(l.Curve.End) | |||
| spline := path.NewSpline(startPoint, endPoint, points...) | |||
| spline := path.NewSpline(startPoint, endPoint, points).WithStyle(path.Style{}) | |||
| if l.Style != nil && l.Style.Thickness != nil { | |||
| spline.SetThickness(*l.Style.Thickness) | |||
| @@ -45,7 +45,7 @@ func (l Line) Build(pat *pattern.Pattern) error { | |||
| pat.AddLine(spline) | |||
| default: | |||
| newPath := path.NewPath(points...) | |||
| newPath := path.NewPath(points) | |||
| if l.Style != nil && l.Style.Thickness != nil { | |||
| newPath.SetThickness(*l.Style.Thickness) | |||
| } | |||