diff --git a/README.md b/README.md index 40655d7..295d486 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ let () = and renders this: -![circle](/tutorial-circle.png) +![circle](/tutorial/tutorial-circle.png) Let's break this down. `init` does all the behind-the-scenes render magic that allows our shape to be drawn to an image file. `circle 100` creates a circle at @@ -48,7 +48,7 @@ like this: `let c = circle ~c:(point 0 100) 100` -![offset-circle](/tutorial-circle-offset.png) +![offset-circle](/tutorial/tutorial-circle-offset.png) Notice the call to `show`, the circle is placed within a list. This is how we render shapes in Joy. Placing our shape arguments in a list allows us to draw @@ -73,7 +73,7 @@ let () = write () ``` -![rotate](/tutorial-rotate.png) +![rotate](/tutorial/tutorial-rotate.png) Transformations can also be composed together: @@ -92,7 +92,7 @@ let rect = rectangle 100 100 in let spiral = repeat 16 (rotate 45) rect ``` -![spiral](/tutorial-spiral.png) +![spiral](/tutorial/tutorial-spiral.png) This is how we build more complex shapes out of the simple primitives and transformations in the library. It may look a little intimidating if you are new @@ -131,7 +131,7 @@ let l = line (point (-250) 250) in let bunch = complex [ c; r; e; l ] ``` -![complex](/tutorial-complex.png) +![complex](/tutorial/tutorial-complex.png) # Going further diff --git a/examples/dune b/examples/dune index f716550..70ee599 100644 --- a/examples/dune +++ b/examples/dune @@ -137,3 +137,8 @@ (name smile) (modules smile) (libraries joy)) + +(executable + (name simple_rotate_ellipse) + (modules simple_rotate_ellipse) + (libraries joy)) diff --git a/examples/rotate_ellipse.ml b/examples/rotate_ellipse.ml index a4877d7..e609cd6 100644 --- a/examples/rotate_ellipse.ml +++ b/examples/rotate_ellipse.ml @@ -4,7 +4,7 @@ let max = 32. let rec range a b = if a > b then [] else a :: range (a +. 1.) b let _ = - init (); + init ~size:(500, 500) ~axes:true (); let ell = ellipse 100 50 |> translate 60 60 in let nums = range 0. max in let rotated = diff --git a/examples/simple_rotate_ellipse.ml b/examples/simple_rotate_ellipse.ml new file mode 100644 index 0000000..214a630 --- /dev/null +++ b/examples/simple_rotate_ellipse.ml @@ -0,0 +1,7 @@ +open Joy + +let _ = + init (); + let ell = ellipse 100 50 in + show [ ell; ell |> rotate 60 ]; + write ~filename:"simple_rotate_ellipse.png" () diff --git a/lib/backend_cairo.ml b/lib/backend_cairo.ml index 2912856..3d485fa 100644 --- a/lib/backend_cairo.ml +++ b/lib/backend_cairo.ml @@ -41,10 +41,15 @@ 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 + (* Apply rotation *) + let radians = Util.to_radians rotation in + Cairo.rotate ctx.cairo_ctx radians; + (* 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; + + (* Arc from 0 to 2pi is a circle *) Cairo.arc ctx.cairo_ctx 0. 0. ~r:1. ~a1:0. ~a2:(2. *. Float.pi); (* Restore the original transformation matrix *) diff --git a/lib/shape.ml b/lib/shape.ml index 7c6fc3e..4580fb4 100644 --- a/lib/shape.ml +++ b/lib/shape.ml @@ -7,7 +7,7 @@ type ellipse = { c : float point; rx : float; ry : float; - rotation : float; + rotation : int; stroke : color; fill : color; } @@ -27,7 +27,7 @@ let point x y = let x, y = (float_of_int x, float_of_int y) in { x; y } -let origin = { x = 0.; y = 0. } +let origin = point 0 0 let circle ?(c = origin) r = Circle @@ -52,8 +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; rotation = 0. } + 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 aae48a3..da3b8dc 100644 --- a/lib/shape.mli +++ b/lib/shape.mli @@ -6,7 +6,7 @@ type ellipse = { c : float point; rx : float; ry : float; - rotation : float; + rotation : int; stroke : color; fill : color; } diff --git a/lib/transform.ml b/lib/transform.ml index 22dec76..9735a77 100644 --- a/lib/transform.ml +++ b/lib/transform.ml @@ -1,4 +1,5 @@ open Shape +open Util type transformation = shape -> shape @@ -64,8 +65,6 @@ let rec scale factor = function } | Complex shapes -> Complex (List.map (scale factor) shapes) -let to_radians degrees = float_of_int degrees *. Stdlib.Float.pi /. 180. - let to_polar point = let { x; y } = point in (sqrt ((x *. x) +. (y *. y)), atan2 y x) @@ -86,7 +85,7 @@ let rec rotate degrees = function { ellipse' with c = rotate_point degrees ellipse'.c; - rotation = ellipse'.rotation +. to_radians degrees; + rotation = ellipse'.rotation + degrees; } | Line line' -> Line diff --git a/lib/util.ml b/lib/util.ml index ffd8e19..82555e9 100644 --- a/lib/util.ml +++ b/lib/util.ml @@ -36,4 +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. +let to_radians degrees = (float_of_int degrees) *. Stdlib.Float.pi /. 180. diff --git a/lib/util.mli b/lib/util.mli index c892687..767bfa4 100644 --- a/lib/util.mli +++ b/lib/util.mli @@ -10,4 +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 +val to_radians : int -> float diff --git a/tutorial-circle-offset.png b/tutorial/tutorial-circle-offset.png similarity index 100% rename from tutorial-circle-offset.png rename to tutorial/tutorial-circle-offset.png diff --git a/tutorial-circle.png b/tutorial/tutorial-circle.png similarity index 100% rename from tutorial-circle.png rename to tutorial/tutorial-circle.png diff --git a/tutorial-complex.png b/tutorial/tutorial-complex.png similarity index 100% rename from tutorial-complex.png rename to tutorial/tutorial-complex.png diff --git a/tutorial-rotate.png b/tutorial/tutorial-rotate.png similarity index 100% rename from tutorial-rotate.png rename to tutorial/tutorial-rotate.png diff --git a/tutorial-spiral.png b/tutorial/tutorial-spiral.png similarity index 100% rename from tutorial-spiral.png rename to tutorial/tutorial-spiral.png