Skip to content

Commit

Permalink
Merge branch 'main' into refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
nikochiko committed Mar 12, 2024
2 parents 0caf3fa + 48e067e commit e7adc6c
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 4 deletions.
10 changes: 10 additions & 0 deletions examples/dune
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@
(modules rotate)
(libraries joy))

(executable
(name rotate_ellipse)
(modules rotate_ellipse)
(libraries joy))

(executable
(name line)
(modules line)
Expand Down Expand Up @@ -127,3 +132,8 @@
(name fill_rect)
(modules fill_rect)
(libraries joy))

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

let max = 32.
let rec range a b = if a > b then [] else a :: range (a +. 1.) b

let _ =
init ();
let ell = ellipse 100 50 |> translate 60 60 in
let nums = range 0. max in
let rotated =
List.map (fun i -> rotate (int_of_float (i /. max *. 360.0)) ell) nums
in
show rotated;
write ~filename:"rotate_ellipse.png" ()
27 changes: 27 additions & 0 deletions examples/smile.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
open Joy

let make_nose () =
let l = line (point 0 50)|> translate 0 (-25) in
let l1 = l in
let l2 = l |> rotate 90|> translate 0 (-25) in
let nose = complex [l1; l2] in
nose

let make_arc rx ry =
let r = rectangle ~c:(point 0 (ry / 2)) (2 * rx) ry in
let col = r |> with_fill white |> with_stroke white in
let e = ellipse ~c:(point 0 0) rx ry in
complex [e; col]

let () =
init ();
let a = circle 200 in
let d = circle ~c:(point 50 50) 20 in
let b = circle ~c:(point (-50) 50) 20 in
let nose = make_nose () in
let leb = make_arc 26 14 |> rotate 180 |> translate 50 70 in
let reb = make_arc 26 14 |> rotate 180 |> translate (-50) 70 in
let mouth = make_arc 80 40 |>translate 0 (-60) in
show [mouth;leb;reb;a;d;b;nose];
write ()

5 changes: 3 additions & 2 deletions lib/backend_cairo.ml
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,13 @@ let draw_circle ctx (cx, cy) radius stroke fill =
Cairo.fill_preserve ctx.cairo_ctx;
Cairo.Path.clear ctx.cairo_ctx

let draw_ellipse ctx (cx, cy) rx ry stroke fill =
let draw_ellipse ctx (cx, cy) rx ry rotation stroke fill =
(* Save the current transformation matrix *)
let save_matrix = Cairo.get_matrix ctx.cairo_ctx in

(* Translate and scale to create an ellipse from a circle *)
Cairo.translate ctx.cairo_ctx cx (Float.neg cy);
Cairo.rotate ctx.cairo_ctx rotation;
Cairo.scale ctx.cairo_ctx rx ry;
Cairo.arc ctx.cairo_ctx 0. 0. ~r:1. ~a1:0. ~a2:(2. *. Float.pi);

Expand Down Expand Up @@ -80,7 +81,7 @@ let show ctx shapes =
circle.fill
| Shape.Ellipse ellipse ->
draw_ellipse ctx (ellipse.c.x, ellipse.c.y) ellipse.rx ellipse.ry
ellipse.stroke ellipse.fill
ellipse.rotation ellipse.stroke ellipse.fill
| Shape.Line line ->
draw_line ctx (line.a.x, line.a.y) (line.b.x, line.b.y) line.stroke
| Shape.Polygon polygon ->
Expand Down
3 changes: 2 additions & 1 deletion lib/shape.ml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ type ellipse = {
c : float point;
rx : float;
ry : float;
rotation : float;
stroke : color;
fill : color;
}
Expand Down Expand Up @@ -51,7 +52,7 @@ let rectangle ?(c = origin) width height =

let ellipse ?(c = origin) rx ry =
let rx, ry = (float_of_int rx, float_of_int ry) in
Ellipse { c; rx; ry; stroke = Color.black; fill = Color.transparent }
Ellipse { c; rx; ry; stroke = Color.black; fill = Color.transparent; rotation = 0. }

let line ?(a = origin) b = Line { a; b; stroke = Color.black }

Expand Down
1 change: 1 addition & 0 deletions lib/shape.mli
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ type ellipse = {
c : float point;
rx : float;
ry : float;
rotation : float;
stroke : color;
fill : color;
}
Expand Down
7 changes: 6 additions & 1 deletion lib/transform.ml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,12 @@ let rotate_point degrees point =
let rec rotate degrees = function
| Circle circle' -> Circle { circle' with c = rotate_point degrees circle'.c }
| Ellipse ellipse' ->
Ellipse { ellipse' with c = rotate_point degrees ellipse'.c }
Ellipse
{
ellipse' with
c = rotate_point degrees ellipse'.c;
rotation = ellipse'.rotation +. to_radians degrees;
}
| Line line' ->
Line
{
Expand Down
1 change: 1 addition & 0 deletions lib/util.ml
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ let rec partition n ?(step = 0) lst =

(* Misc *)
let range n = List.init n Fun.id
let to_radians degrees = degrees *. Stdlib.Float.pi /. 180.
1 change: 1 addition & 0 deletions lib/util.mli
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ val ( >> ) : ('a -> 'b) -> ('b -> 'c) -> 'a -> 'c
val take : int -> 'a list -> 'a list * 'a list
val partition : int -> ?step:int -> 'a list -> 'a list list
val range : int -> int list
val to_radians : float -> float

0 comments on commit e7adc6c

Please sign in to comment.