From edf2eb31fbc983d37afd0368a0b2fe40f67ad9d6 Mon Sep 17 00:00:00 2001 From: Fay Carsons Date: Fri, 8 Mar 2024 10:55:36 -0500 Subject: [PATCH 1/2] optimize polygon rendering & fix polygon fill --- examples/dune | 5 +++++ examples/fill_rect.ml | 7 +++++++ lib/render.ml | 13 ++++--------- 3 files changed, 16 insertions(+), 9 deletions(-) create mode 100644 examples/fill_rect.ml diff --git a/examples/dune b/examples/dune index 658b600..10752c1 100644 --- a/examples/dune +++ b/examples/dune @@ -117,3 +117,8 @@ (name color) (modules color) (libraries joy)) + +(executable + (name fill_rect) + (modules fill_rect) + (libraries joy)) diff --git a/examples/fill_rect.ml b/examples/fill_rect.ml new file mode 100644 index 0000000..5eb37d3 --- /dev/null +++ b/examples/fill_rect.ml @@ -0,0 +1,7 @@ +open Joy + +let () = + init (); + let r = rectangle 500 500 |> no_stroke |> with_fill (255, 0, 0) in + show [ r ]; + write ~filename:"fill-rect.png" () diff --git a/lib/render.ml b/lib/render.ml index 68bedc7..0ac16d2 100644 --- a/lib/render.ml +++ b/lib/render.ml @@ -71,15 +71,10 @@ let draw_polygon ctx { vertices; stroke; fill } = set_color fill; Cairo.fill_preserve ctx.ctx in - let points = partition 2 ~step:1 (vertices @ [ List.hd vertices ]) in - List.iter - (fun pair -> - let { x = x1; y = y1 }, { x = x2; y = y2 } = - (List.nth pair 0, List.nth pair 1) - in - Cairo.move_to ctx.ctx x1 (Float.neg y1); - Cairo.line_to ctx.ctx x2 (Float.neg y2)) - points; + let { x; y }, t = (List.hd vertices, List.tl vertices) in + Cairo.move_to ctx.ctx x y; + List.iter (fun { x = x'; y = y' } -> Cairo.line_to ctx.ctx x' y') t; + Cairo.Path.close ctx.ctx; Option.iter stroke_rect stroke; Option.iter fill_rect fill; Cairo.Path.clear ctx.ctx From cca0c7de9366b903578c34ec9e127b2c9931a76b Mon Sep 17 00:00:00 2001 From: Fay Carsons Date: Fri, 8 Mar 2024 10:58:56 -0500 Subject: [PATCH 2/2] refactor fill-rect example --- examples/fill_rect.ml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/fill_rect.ml b/examples/fill_rect.ml index 5eb37d3..18d5b28 100644 --- a/examples/fill_rect.ml +++ b/examples/fill_rect.ml @@ -2,6 +2,6 @@ open Joy let () = init (); - let r = rectangle 500 500 |> no_stroke |> with_fill (255, 0, 0) in + let r = rectangle 200 200 |> with_fill (255, 0, 0) in show [ r ]; write ~filename:"fill-rect.png" ()