Skip to content

Commit

Permalink
Fix #92: centering for rectangle shape (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
nikochiko authored Jan 26, 2024
1 parent c38d8b9 commit c91cea1
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 11 deletions.
5 changes: 5 additions & 0 deletions examples/dune
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@
(modules rectangle_canvas)
(libraries joy))

(executable
(name square)
(modules square)
(libraries joy))

(executable
(name star)
(modules star)
Expand Down
7 changes: 2 additions & 5 deletions examples/rectangle.ml
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
open Joy

let size = 100

let () =
init ();
background (255, 255, 255, 255);
(* creating a rectangle from points *)
let rect = rectangle size size in
set_color (0, 0, 0);
render rect;
let r = rectangle 100 200 in
show [r];
write ~filename:"rectangle.png" ()
10 changes: 10 additions & 0 deletions examples/square.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
open Joy

let () =
init ();
background (255, 255, 255, 255);
let square = rectangle 100 100 in
set_color (0, 0, 0);
show [square];
write ~filename:"square.png" ()

13 changes: 7 additions & 6 deletions lib/shape.ml
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,15 @@ let center = { x = 0.; y = 0. }
let circle ?(c = center) r = Circle { c; radius = float_of_int r }

let rectangle ?(c = center) width height =
let width, height = (float_of_int width, float_of_int height) in
let { x; y } = c -! ((width +. height) /. 4.) in
let w, h = (float_of_int width, float_of_int height) in
let x1 = c.x -. (w /. 2.) in
let y1 = c.x -. (h /. 2.) in

This comment has been minimized.

Copy link
@FayCarsons

FayCarsons Feb 14, 2024

Collaborator

This broke rectangle rendering as the x of the center point is being used for both x and y. Fix in #100

Polygon
[
{ x; y };
{ x; y = y +. height };
{ x = x +. width; y = y +. height };
{ x = x +. width; y };
{ x = x1; y = y1 };
{ x = x1; y = y1 +. h };
{ x = x1 +. w; y = y1 +. h };
{ x = x1 +. w; y = y1 };
]

let ellipse ?(c = center) rx ry =
Expand Down

0 comments on commit c91cea1

Please sign in to comment.