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 069d525 + 32406f4 commit e718daf
Show file tree
Hide file tree
Showing 15 changed files with 32 additions and 17 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -73,7 +73,7 @@ let () =
write ()
```

![rotate](/tutorial-rotate.png)
![rotate](/tutorial/tutorial-rotate.png)

Transformations can also be composed together:

Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down
5 changes: 5 additions & 0 deletions examples/dune
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,8 @@
(name smile)
(modules smile)
(libraries joy))

(executable
(name simple_rotate_ellipse)
(modules simple_rotate_ellipse)
(libraries joy))
2 changes: 1 addition & 1 deletion examples/rotate_ellipse.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
7 changes: 7 additions & 0 deletions examples/simple_rotate_ellipse.ml
Original file line number Diff line number Diff line change
@@ -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" ()
7 changes: 6 additions & 1 deletion lib/backend_cairo.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 *)
Expand Down
7 changes: 3 additions & 4 deletions lib/shape.ml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ type ellipse = {
c : float point;
rx : float;
ry : float;
rotation : float;
rotation : int;
stroke : color;
fill : color;
}
Expand All @@ -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
Expand All @@ -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 }

Expand Down
2 changes: 1 addition & 1 deletion lib/shape.mli
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ type ellipse = {
c : float point;
rx : float;
ry : float;
rotation : float;
rotation : int;
stroke : color;
fill : color;
}
Expand Down
5 changes: 2 additions & 3 deletions lib/transform.ml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
open Shape
open Util

type transformation = shape -> shape

Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/util.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
2 changes: 1 addition & 1 deletion lib/util.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes

0 comments on commit e718daf

Please sign in to comment.