-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
73 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
open Joy.Shape | ||
|
||
(* | ||
Complex shapes can also be created from lists of shapes. | ||
This allows us to apply any of the libraries' transformations to a group of | ||
shapes, like we're doing here with translate | ||
*) | ||
|
||
(* creates a list containing numbers between a and b *) | ||
let rec range a b = if a > b then [] else a :: range (a + 1) b | ||
|
||
(* creates a list of (int * int) tuples, | ||
containing every combination of the elements of the two passsed lists *) | ||
let cartesian_product l l' = | ||
List.concat (List.map (fun e -> List.map (fun e' -> (e, e')) l') l) | ||
|
||
let () = | ||
init (); | ||
(* radius which also acts as grid spacing *) | ||
let radius = 50 in | ||
let half_radius = radius / 2 in | ||
(* creating a grid with cartesian_product *) | ||
let coords = cartesian_product (range (-5) 5) (range (-5) 5) in | ||
(* using map to turn that into a complex shape that is a grid of circles *) | ||
let complex_shape = | ||
complex | ||
(List.map | ||
(fun (x, y) -> circle ~x:(x * radius) ~y:(y * radius) radius) | ||
coords) | ||
in | ||
(* translating that complex shape by radius / 2 *) | ||
let complex_transformed = translate half_radius half_radius complex_shape in | ||
show [ complex_shape; complex_transformed ]; | ||
close () |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,20 @@ | ||
open Joy.Shape | ||
|
||
(* Higher order transformations can be composed with `comp`, | ||
(* 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 *) | ||
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 () = | ||
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 | ||
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; | ||
close () | ||
close () |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters