Skip to content

Commit

Permalink
added repeat function and refactored relevant examples
Browse files Browse the repository at this point in the history
  • Loading branch information
FayCarsons committed Oct 27, 2023
1 parent b6f9054 commit 648dc6c
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 10 deletions.
5 changes: 5 additions & 0 deletions examples/dune
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,8 @@
(name complex)
(modules complex)
(libraries joy))

(executable
(name repeat)
(modules repeat)
(libraries joy))
14 changes: 4 additions & 10 deletions examples/higher_transforms.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,13 @@ open Joy.Shape

(* Higher order transformations can be composed with `comp`,
which applies its function args right-to-left.
This allows us to create complex series transformations,
that can be applied iteratively *)
This allows us to create complex series of transformations,
that can be applied iteratively with the repeat function *)
let transform = compose (translate 10 10) (scale 0.9)
let rec range a b = if a > b then [] else a :: range (a + 1) b

let () =
init ();
let initial = rectangle ~x:(-250) ~y:(-250) 100 100 in
let match_list l =
match l with
| [] -> [ transform initial ]
| last :: _ -> transform last :: l
in
let shapes = List.fold_right (fun _ acc -> match_list acc) (range 0 32) [] in
show shapes;
let shapes = repeat 32 transform initial in
render_shape shapes;
close ()
16 changes: 16 additions & 0 deletions examples/repeat.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
open Joy.Shape

(*
demonstration of the repeat function
takes n, an operation, and an initial shape, and applies the operation
iteratively to the initial shape n times
adapted from the original Joy python library's examples
*)

let () =
init ();
let circle = circle ~x: (-100) ~y: 0 50 in
let shapes = repeat 10 (translate 10 0) circle in
render_shape shapes;
close ();
10 changes: 10 additions & 0 deletions lib/shape.ml
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,16 @@ let rec rotate degrees shape =

let compose f g x = g (f x)

let rec range a b = if a >= b then [] else a :: range (a + 1) b
let repeat n op shape =
let match_list l =
match l with
| [] -> [ op shape ]
| last :: _ -> op last :: l
in
let shapes = List.fold_right (fun _ acc -> match_list acc) (range 0 n) [] in
complex shapes

let render_axes () =
set_color (rgb 192 192 192);
let half_x = size_x () / 2 in
Expand Down
1 change: 1 addition & 0 deletions lib/shape.mli
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ val show : shape list -> unit
val scale : float -> shape -> shape
val rotate : int -> shape -> shape
val compose : ('a -> 'b) -> ('b -> 'c) -> 'a -> 'c
val repeat : int -> (shape -> shape) -> shape -> shape
val draw_axes : bool -> unit
val set_dimensions : int -> int -> unit
val init : unit -> unit
Expand Down

0 comments on commit 648dc6c

Please sign in to comment.