Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switching API to ints #83

Merged
merged 6 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 0 additions & 28 deletions ellipse.py

This file was deleted.

6 changes: 3 additions & 3 deletions examples/axes.ml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ open Joy
let _ =
(* intialize rendering context with the axes flag set to true *)
init ~axes:true ();
background (1., 1., 1., 1.);
(* set background to opaque white *)
let c = circle 50. in
set_color (0., 0., 0.);
background (255, 255, 255, 255);
let c = circle 50 in
set_color (0, 0, 0);
render c;
(* Write to PNG! *)
write ~filename:"axes.png" ()
6 changes: 3 additions & 3 deletions examples/circle.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ open Joy

let () =
init ();
background (1., 1., 1., 1.);
let c = circle 50. in
set_color (0., 0., 0.);
background (255, 255, 255, 255);
let c = circle 50 in
set_color (0, 0, 0);
render c;
write ~filename:"circle.png" ()
68 changes: 35 additions & 33 deletions examples/circle_packing.ml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
open Joy

(* global constants // RNG initialization *)
let resolution = (1200., 900.)
let w, h = (900., 900.)
let min_radius = 20.
let max_radius = 150.
let num_circles = 5_000
Expand All @@ -10,24 +10,22 @@ let shrink_factor = 0.85
let _ = Stdlib.Random.self_init ()

let palette =
List.map
(fun (r, g, b) -> (r /. 255., g /. 255., b /. 255.))
[
(* purple *)
(107., 108., 163.);
(* light blue *)
(135., 188., 189.);
(* green *)
(111., 153., 84.);
(* light purple *)
(150., 155., 199.);
(* light green *)
(137., 171., 124.);
(* dark purple *)
(67., 68., 117.);
(* darker purple *)
(44., 45., 84.);
]
[
(* purple *)
(107, 108, 163);
(* light blue *)
(135, 188, 189);
(* green *)
(111, 153, 84);
(* light purple *)
(150, 155, 199);
(* light green *)
(137, 171, 124);
(* dark purple *)
(67, 68, 117);
(* darker purple *)
(44, 45, 84);
]

(* utility Functions *)

Expand All @@ -37,15 +35,9 @@ let distance (x1, y1) (x2, y2) =
let dy = y2 -. y1 in
sqrt ((dx *. dx) +. (dy *. dy))

(* determines if two circles overlaps *)
let overlaps (p1, r1) (p2, r2) =
let dist = distance p1 p2 in
dist < r1 +. r2

(* creates a random point within screen bounds *)
let rand_point () =
( Stdlib.Random.float (fst resolution *. 2.) -. fst resolution,
Stdlib.Random.float (snd resolution *. 2.) -. snd resolution )
(Stdlib.Random.float w -. (w /. 2.), Stdlib.Random.float h -. (h /. 2.))

(* creates a circle with a random center point and radius *)
let rand_circle () =
Expand All @@ -54,6 +46,11 @@ let rand_circle () =

(* creates a lis of packed circles *)
let pack_circles () =
(* determines if two circles overlap *)
let overlaps (p1, r1) (p2, r2) =
let dist = distance p1 p2 in
dist < r1 +. r2
in
(* checks whether a circle intersects with a list of circles *)
let check_overlaps lst current =
List.fold_right (fun circle acc -> overlaps circle current || acc) lst false
Expand All @@ -67,9 +64,10 @@ let pack_circles () =
let new_circle = rand_circle () in
let does_overlap = check_overlaps lst new_circle in
let safe = List.length lst < num_circles - 1 && attempts < max_attempts in
if does_overlap && safe then pack lst (attempts + 1)
else if not safe then new_circle :: lst
else pack (new_circle :: lst) attempts
match (does_overlap, safe) with
| true, true -> pack lst (attempts + 1)
| true, false -> new_circle :: lst
| _ -> pack (new_circle :: lst) attempts
in
let attempts = 0 in
let lst = [ rand_circle () ] in
Expand All @@ -96,12 +94,16 @@ let make_concentric circle =

(* main fn *)
let () =
init ~size:resolution ();
background (1., 1., 1., 1.);
set_line_width 0.001;
init ~size:(int_of_float w, int_of_float h) ();
background (255, 255, 255, 255);
set_line_width 1;
let circles = pack_circles () in
let circles = List.flatten (List.map make_concentric circles) in
List.iter
(fun ((x, y), radius) -> draw_with_color (circle ~c:(point x y) radius))
(fun ((x, y), radius) ->
draw_with_color
(circle
~c:(point (int_of_float x) (int_of_float y))
(int_of_float radius)))
Comment on lines +106 to +107
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we instead change pack_circles and make_concentric to give us ints?

Also, instead of shrink, can you try using transform.scale to have the same effect?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't use Transform.scale because I can't test the radii of the circles as they aren't exposed in the API. Also, that would require switching from the current representation of circles as (float * float) * float tuples.

I can make make_concentric and pack_circles take and return ints but that would require a lot of conversion back into floats because the math required to check if circles overlap is done with floats.

I think that, in general, this sketch is illustrative of what an end-user writing a more complex sketch will look like.

circles;
write ~filename:"Circle packing.png" ()
9 changes: 5 additions & 4 deletions examples/circle_row_joy.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ open Joy

let () =
init ();
background (1., 1., 1., 1.);
let base_circle = circle 50. in
let circle1 = base_circle |> translate (-100.) 0. in
let circle2 = base_circle |> translate 100. 0. in
background (255, 255, 255, 255);
let base_circle = circle 50 in
let circle1 = base_circle |> translate (-100) 0 in
let circle2 = base_circle |> translate 100 0 in
set_color (0, 0, 0);
FayCarsons marked this conversation as resolved.
Show resolved Hide resolved
show [ circle1; base_circle; circle2 ];
write ~filename:"circle_row.png" ()
14 changes: 7 additions & 7 deletions examples/complex.ml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ open Joy
*)

(* creates a list containing numbers between a and b *)
let rec range a b = if a > b then [] else a :: range (a +. 1.) b
let rec range a b = if a > b then [] else a :: range (a + 1) b
FayCarsons marked this conversation as resolved.
Show resolved Hide resolved

(* creates a list of (int * int) tuples,
containing every combination of the elements of the two passsed lists *)
Expand All @@ -16,21 +16,21 @@ let cartesian_product l l' =

let () =
init ();
background (1., 1., 1., 1.);
background (255, 255, 255, 255);
(* radius which also acts as grid spacing *)
let radius = 50. in
let half_radius = radius /. 2. in
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
let coords = cartesian_product (range (-5) 5) (range (-5) 5) in
FayCarsons marked this conversation as resolved.
Show resolved Hide resolved
(* 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 ~c:(point (x *. radius) (y *. radius)) radius)
(fun (x, y) -> circle ~c:(point (x * radius) (y * radius)) radius)
coords)
in
(* translating that complex shape by radius / 2 *)
let complex_transformed = translate half_radius half_radius complex_shape in
set_color (0., 0., 0.);
set_color (0, 0, 0);
FayCarsons marked this conversation as resolved.
Show resolved Hide resolved
show [ complex_shape; complex_transformed ];
write ~filename:"complex.png" ()
6 changes: 3 additions & 3 deletions examples/concentric_circles.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ open Joy

let () =
init ();
background (1., 1., 1., 1.);
background (255, 255, 255, 255);

let init_circle = circle 200. in
let init_circle = circle 200 in
let interval = 1. -. (1. /. 20.) in
let rec make_concentric (arr : shape list) (i : int) : shape list =
match (arr, i) with
Expand All @@ -14,6 +14,6 @@ let () =
| _, _ -> arr
in
let circles = complex (make_concentric [] 21) in
set_color (0., 0., 0.);
set_color (0, 0, 0);
render circles;
write ~filename:"concentric_circles.png" ()
6 changes: 3 additions & 3 deletions examples/ellipse.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ open Joy

let () =
init ();
background (1., 1., 1., 1.);
background (255, 255, 255, 255);
(* create an ellipse *)
let e = ellipse 100. 75. in
let e = ellipse 100 75 in
(* render it *)
set_color (0., 0., 0.);
set_color (0, 0, 0);
render e;
write ~filename:"ellipse.png" ()
8 changes: 4 additions & 4 deletions examples/higher_transforms.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ open Joy
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 transform = compose (translate 10 10) (scale 0.9)

let () =
init ();
background (1., 1., 1., 1.);
let initial = rectangle ~c:(point (-250.) (-250.)) 100. 100. in
background (255, 255, 255, 255);
let initial = rectangle ~c:(point (-250) (-250)) 100 100 in
let shapes = repeat 32 transform initial in
set_color (0., 0., 0.);
set_color (0, 0, 0);
render shapes;
write ~filename:"higher_transforms.png" ()
24 changes: 12 additions & 12 deletions examples/line.ml
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
open Joy

let size = 800.
let interval = 16.
let line_interval = 800. /. interval
let rec range a b = if a > b then [] else a :: range (a +. 1.) b
let inc x = x +. 1.
let size = 800
let interval = 16
let line_interval = 800 / interval
let rec range a b = if a > b then [] else a :: range (a + 1) b
let inc x = x + 1

let _ =
init ~size:(size, size) ();
let half_size = size /. 2. in
background (1., 1., 1., 1.);
let half_size = size / 2 in
background (255, 255, 255, 255);
let lines =
List.map
(fun i ->
let newx = i |> inc |> ( *. ) line_interval in
let newx = i |> inc |> ( * ) line_interval in
line
~a:(point (newx -. half_size) (-.half_size))
(point (newx -. half_size) half_size))
(range 0. interval)
~a:(point (newx - half_size) (-half_size))
(point (newx - half_size) half_size))
(range 0 interval)
in
set_color (0., 0., 0.);
set_color (0, 0, 0);
show lines;
write ~filename:"line.png" ()
4 changes: 2 additions & 2 deletions examples/polygon.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ let size = 100.

let () =
init ();
background (1., 1., 1., 1.);
background (255, 255, 255, 255);
let poly =
polygon
[ { x = -.size; y = 0. }; { x = 0.; y = size }; { x = size; y = 0. } ]
in
set_color (0., 0., 0.);
set_color (0, 0, 0);
render poly;
write ~filename:"polygon.png" ()
6 changes: 3 additions & 3 deletions examples/rectangle.ml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
open Joy

let size = 100.
let size = 100

let () =
init ();
background (1., 1., 1., 1.);
background (255, 255, 255, 255);
(* creating a rectangle from points *)
let rect = rectangle size size in
set_color (0., 0., 0.);
set_color (0, 0, 0);
render rect;
write ~filename:"rectangle.png" ()
8 changes: 4 additions & 4 deletions examples/repeat.ml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ open Joy

let () =
init ();
background (1., 1., 1., 1.);
let circle = circle ~c:(point (-100.) 0.) 50. in
let shapes = repeat 10 (translate 10. 0.) circle in
set_color (0., 0., 0.);
background (255, 255, 255, 255);
let circle = circle ~c:(point (-100) 0) 50 in
let shapes = repeat 10 (translate 10 0) circle in
set_color (0, 0, 0);
render shapes;
write ~filename:"repeat.png" ()
6 changes: 3 additions & 3 deletions examples/rotate.ml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ let rec range a b = if a > b then [] else a :: range (a +. 1.) b

let _ =
init ();
background (1., 1., 1., 1.);
let rect = rectangle 100. 100. in
background (255, 255, 255, 255);
let rect = rectangle 100 100 in
let nums = range 0. max in
let rotated =
List.map (fun i -> rotate (int_of_float (i /. max *. 360.0)) rect) nums
in
set_color (0., 0., 0.);
set_color (0, 0, 0);
show rotated;
write ~filename:"rotation.png" ()
8 changes: 4 additions & 4 deletions examples/star.ml
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ let star_section i =
let i = float_of_int i in
let x = outer_radius *. cos (angle_step *. i)
and y = outer_radius *. sin (angle_step *. i) in
let outer_point : point = { x; y } in
let outer_point = point (int_of_float x) (int_of_float y) in
let x = inner_radius *. cos ((i +. 0.5) *. angle_step)
and y = inner_radius *. sin ((i +. 0.5) *. angle_step) in
[ outer_point; { x; y } ]

let () =
init ();
background (1., 1., 1., 1.);
set_line_width 0.0035;
background (255, 255, 255, 255);
set_line_width 3;
let star = List.init points star_section |> List.flatten |> polygon in
set_color (0., 0., 0.);
set_color (0, 0, 0);
render star;
write ~filename:"star.png" ()
Loading
Loading