diff --git a/examples/dune b/examples/dune index a88c9b8..f716550 100644 --- a/examples/dune +++ b/examples/dune @@ -48,6 +48,11 @@ (modules rotate) (libraries joy)) +(executable + (name rotate_ellipse) + (modules rotate_ellipse) + (libraries joy)) + (executable (name line) (modules line) @@ -127,3 +132,8 @@ (name fill_rect) (modules fill_rect) (libraries joy)) + +(executable + (name smile) + (modules smile) + (libraries joy)) diff --git a/examples/rotate_ellipse.ml b/examples/rotate_ellipse.ml new file mode 100644 index 0000000..a4877d7 --- /dev/null +++ b/examples/rotate_ellipse.ml @@ -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" () diff --git a/examples/smile.ml b/examples/smile.ml new file mode 100644 index 0000000..89ca1d3 --- /dev/null +++ b/examples/smile.ml @@ -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 () + diff --git a/lib/backend_cairo.ml b/lib/backend_cairo.ml index f592f3b..2912856 100644 --- a/lib/backend_cairo.ml +++ b/lib/backend_cairo.ml @@ -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); @@ -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 -> diff --git a/lib/shape.ml b/lib/shape.ml index c804238..50451b5 100644 --- a/lib/shape.ml +++ b/lib/shape.ml @@ -7,6 +7,7 @@ type ellipse = { c : float point; rx : float; ry : float; + rotation : float; stroke : color; fill : color; } @@ -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 } diff --git a/lib/shape.mli b/lib/shape.mli index d0a01a5..aae48a3 100644 --- a/lib/shape.mli +++ b/lib/shape.mli @@ -6,6 +6,7 @@ type ellipse = { c : float point; rx : float; ry : float; + rotation : float; stroke : color; fill : color; } diff --git a/lib/transform.ml b/lib/transform.ml index fa71aa2..22dec76 100644 --- a/lib/transform.ml +++ b/lib/transform.ml @@ -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 { diff --git a/lib/util.ml b/lib/util.ml index ec23ded..ffd8e19 100644 --- a/lib/util.ml +++ b/lib/util.ml @@ -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. diff --git a/lib/util.mli b/lib/util.mli index 74cadbd..c892687 100644 --- a/lib/util.mli +++ b/lib/util.mli @@ -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