Skip to content

Commit

Permalink
Merge pull request #115 from FayCarsons/fix-polygons
Browse files Browse the repository at this point in the history
Optimize polygon rendering & fix polygon fill
  • Loading branch information
FayCarsons authored Mar 8, 2024
2 parents 8b32960 + cca0c7d commit 9ffa611
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
5 changes: 5 additions & 0 deletions examples/dune
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,8 @@
(name color)
(modules color)
(libraries joy))

(executable
(name fill_rect)
(modules fill_rect)
(libraries joy))
7 changes: 7 additions & 0 deletions examples/fill_rect.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
open Joy

let () =
init ();
let r = rectangle 200 200 |> with_fill (255, 0, 0) in
show [ r ];
write ~filename:"fill-rect.png" ()
13 changes: 4 additions & 9 deletions lib/render.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9ffa611

Please sign in to comment.