From ee373c55e38a4081819e302ba679ec75988d8b45 Mon Sep 17 00:00:00 2001 From: Kaustubh Maske Patil <37668193+nikochiko@users.noreply.github.com> Date: Tue, 12 Mar 2024 12:22:10 +0530 Subject: [PATCH 1/2] Move tutorial assets to tutorial/ (#130) --- README.md | 10 +++++----- .../tutorial-circle-offset.png | Bin tutorial-circle.png => tutorial/tutorial-circle.png | Bin .../tutorial-complex.png | Bin tutorial-rotate.png => tutorial/tutorial-rotate.png | Bin tutorial-spiral.png => tutorial/tutorial-spiral.png | Bin 6 files changed, 5 insertions(+), 5 deletions(-) rename tutorial-circle-offset.png => tutorial/tutorial-circle-offset.png (100%) rename tutorial-circle.png => tutorial/tutorial-circle.png (100%) rename tutorial-complex.png => tutorial/tutorial-complex.png (100%) rename tutorial-rotate.png => tutorial/tutorial-rotate.png (100%) rename tutorial-spiral.png => tutorial/tutorial-spiral.png (100%) 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/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 From 1b3f202895e0831320c2212ca184b140c8cb5a76 Mon Sep 17 00:00:00 2001 From: Sudha Parimala Date: Tue, 12 Mar 2024 14:15:51 +0530 Subject: [PATCH 2/2] Store ellipse rotation as degrees Stores ellipse rotation as degrees rather than radians. This is to ensure portability between backends, since radians is something specifc to the Cairo backend --- lib/render.ml | 3 ++- lib/shape.ml | 4 ++-- lib/shape.mli | 2 +- lib/transform.ml | 5 ++--- lib/util.ml | 2 +- lib/util.mli | 2 +- 6 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/render.ml b/lib/render.ml index fc2ab0a..d65af67 100644 --- a/lib/render.ml +++ b/lib/render.ml @@ -30,8 +30,9 @@ let draw_ellipse ctx { c; rx; ry; stroke; fill; rotation } = let save_matrix = Cairo.get_matrix ctx.ctx in (* Translate and scale to create an ellipse from a circle *) + let radians = to_radians rotation in Cairo.translate ctx.ctx c.x (Float.neg c.y); - Cairo.rotate ctx.ctx rotation; + Cairo.rotate ctx.ctx radians; Cairo.scale ctx.ctx rx ry; Cairo.arc ctx.ctx 0. 0. ~r:1. ~a1:0. ~a2:(2. *. Float.pi); diff --git a/lib/shape.ml b/lib/shape.ml index 080ac34..f40b37f 100644 --- a/lib/shape.ml +++ b/lib/shape.ml @@ -13,7 +13,7 @@ type ellipse = { c : float point; rx : float; ry : float; - rotation : float; + rotation : int; stroke : color option; fill : color option; } @@ -56,7 +56,7 @@ let rectangle ?(c = center) width height = let ellipse ?(c = center) rx ry = let rx, ry = (float_of_int rx, float_of_int ry) in - Ellipse { c; rx; ry; stroke = Some Color.black; fill = None; rotation = 0. } + Ellipse { c; rx; ry; stroke = Some Color.black; fill = None; rotation = 0 } let line ?(a = center) b = Line { a; b; stroke = Color.black } diff --git a/lib/shape.mli b/lib/shape.mli index dcd4e95..8d119a6 100644 --- a/lib/shape.mli +++ b/lib/shape.mli @@ -12,7 +12,7 @@ type ellipse = { c : float point; rx : float; ry : float; - rotation : float; + rotation : int; stroke : color option; fill : color option; } diff --git a/lib/transform.ml b/lib/transform.ml index bfc27f4..2da136d 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 { line' with a = rotate_point degrees line'.a; b = rotate_point degrees line'.b } | Polygon polygon' -> 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