Skip to content

Commit

Permalink
p5: better handling of closed paths
Browse files Browse the repository at this point in the history
Detect shapes that are expected to be closed paths and properly close
the Gio path.
This fixes not quite closed paths because of float32 rounding errors.

Fixes #63.

Signed-off-by: Sebastien Binet <[email protected]>
  • Loading branch information
sbinet committed Jan 2, 2024
1 parent e5f7503 commit 074c3f9
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
24 changes: 24 additions & 0 deletions proc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,3 +356,27 @@ func TestFrameCount_Loop(t *testing.T) {
t.Fatalf("should be looping")
}
}

func TestIssue63(t *testing.T) {
const (
w = 500
h = 500
)
proc := newTestProc(t, w, h,
func(p5 *Proc) {
p5.Canvas(w, h)
p5.Background(color.Gray{Y: 220})
},
func(p5 *Proc) {
p5.StrokeWidth(2)
p5.Fill(color.RGBA{R: 255, A: 208})
p5.Rect(382.732615, 14.503678, 52, 73)

p5.Fill(color.RGBA{B: 255, A: 208})
p5.Rect(200, 200, 50, 100)
},
"testdata/issue-063.png",
imgDelta,
)
proc.Run(t)
}
9 changes: 9 additions & 0 deletions shapes.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,13 +289,22 @@ func (p *Proc) poly(ps ...f32.Point) {
return
}

doClose := false
if len(ps) > 1 && ps[0] == ps[len(ps)-1] {
doClose = true
ps = ps[:len(ps)-1]
}

path := func(o *op.Ops) clip.PathSpec {
var path clip.Path
path.Begin(o)
path.Move(ps[0])
for _, p := range ps[1:] {
path.Line(p.Sub(path.Pos()))
}
if doClose {
path.Close()
}
return path.End()
}

Expand Down
Binary file added testdata/issue-063_golden.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 074c3f9

Please sign in to comment.