From 074c3f9ed461b6bb01644490a789e3f3681ddcf3 Mon Sep 17 00:00:00 2001 From: Sebastien Binet Date: Tue, 2 Jan 2024 22:33:18 +0100 Subject: [PATCH] p5: better handling of closed paths 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 --- proc_test.go | 24 ++++++++++++++++++++++++ shapes.go | 9 +++++++++ testdata/issue-063_golden.png | Bin 0 -> 2164 bytes 3 files changed, 33 insertions(+) create mode 100644 testdata/issue-063_golden.png diff --git a/proc_test.go b/proc_test.go index 78be2e2..d17456f 100644 --- a/proc_test.go +++ b/proc_test.go @@ -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) +} diff --git a/shapes.go b/shapes.go index 0e6a65d..1f4638b 100644 --- a/shapes.go +++ b/shapes.go @@ -289,6 +289,12 @@ 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) @@ -296,6 +302,9 @@ func (p *Proc) poly(ps ...f32.Point) { for _, p := range ps[1:] { path.Line(p.Sub(path.Pos())) } + if doClose { + path.Close() + } return path.End() } diff --git a/testdata/issue-063_golden.png b/testdata/issue-063_golden.png new file mode 100644 index 0000000000000000000000000000000000000000..d8f51c5c5f67b05ca0f83fcebf67d3d9151cab8b GIT binary patch literal 2164 zcmeAS@N?(olHy`uVBq!ia0y~yVEh8Y985qFmn|N585lUMJzX3_D(1YoY3mmgF4O*y zhf#UwMw6n~TD`i8N2@)A^$%*kIF@>=B;u5ifGBsTxZ^?h+UhsFJq8nIdU%SaXq}kz z;%Q~rhs(Nge@oAQGx5F8cwv)i6$3-R{~gArBeDt}1gLH9{0#Hve66ajtJ@c&=gzobxZrr~wzxR>j5!oACu4aApzxlu7 zd)}96yZ!9y|5a2~RaaYAopA27HaPI^+`0Gp_y2u)b@hc}`QHzZjvl{!`J%R5BYS9r zLLobcQ29uq7QDU4+_3z`>$7L`)z$6y)mT=Yh*gi@*E8>f1t<{Lu3dXK{rtYT`2T-i zUVfo_f7|BG##M)$&o0Z9+RIoGb?x=nn>TM3?Ub>75|@8(&&>1B7l+$)>|kc83}78W zfjHQb%q= z|LLda9^W3Jv+dt>vF@WU@|mTLW-rV9!j=WhRXJwTdzt2$Ot