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 2 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.

16 changes: 9 additions & 7 deletions examples/axes.ml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ 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.);
render c;
(* Write to PNG! *)
write ~filename:"axes.png" ()
background (255, 255, 255, 255)
;;

(* set background to opaque white *)
let c = circle 50 in
set_color (0, 0, 0);
render c;
(* Write to PNG! *)
write ~filename:"axes.png" ()
FayCarsons marked this conversation as resolved.
Show resolved Hide resolved
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" ()
47 changes: 25 additions & 22 deletions examples/circle_packing.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ open Joy

(* global constants // RNG initialization *)
let resolution = (1200., 900.)
let tmap f (x, y) = (f x, f y)
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 define resolution as a tuple of ints instead?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

If it were ints I would need to add 5-6 more calls to float_of_int, as opposed to the 1 call to tmap int_of_float resolution that's present.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Where would that be required?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Line 40 in rand_circle. I think this example needs to be rewritten from scratch though, it doesn't properly pack the circles since migrating to the Cairo backend and I think it could be made a bit more concise.

let min_radius = 20.
let max_radius = 150.
let num_circles = 5_000
Expand All @@ -10,24 +11,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 Down Expand Up @@ -96,12 +95,16 @@ let make_concentric circle =

(* main fn *)
let () =
init ~size:resolution ();
background (1., 1., 1., 1.);
set_line_width 0.001;
init ~size:(tmap int_of_float resolution) ();
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" ()
15 changes: 7 additions & 8 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,22 +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" ()
4 changes: 2 additions & 2 deletions examples/dune
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@
(name star)
(modules star)
(libraries joy))
(executable

(executable
(name repeat)
(modules repeat)
(libraries joy))
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" ()
15 changes: 9 additions & 6 deletions examples/line.ml
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
open Joy

let size = 800.
let size = 800
let interval = 16.
let line_interval = 800. /. interval
FayCarsons marked this conversation as resolved.
Show resolved Hide resolved
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 = float_of_int size /. 2. in
FayCarsons marked this conversation as resolved.
Show resolved Hide resolved
background (255, 255, 255, 255);
let lines =
List.map
(fun i ->
let newx = i |> inc |> ( *. ) line_interval in
line
~a:(point (newx -. half_size) (-.half_size))
(point (newx -. half_size) half_size))
~a:
(point
(int_of_float (newx -. half_size))
(int_of_float (-.half_size)))
(point (int_of_float (newx -. half_size)) (int_of_float 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" ()
8 changes: 4 additions & 4 deletions examples/translate_circle.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ open Joy

let () =
init ();
background (1., 1., 1., 1.);
background (255, 255, 255, 255);
(* Create circle *)
let c1 = circle 100. in
let c1 = circle 100 in
(* Translate it to the right by 100 *)
let c2 = translate 100. 0. c1 in
let c2 = translate 100 0 c1 in
(* Display both circles *)
set_color (0., 0., 0.);
set_color (0, 0, 0);
show [ c1; c2 ];
write ~filename:"translate_circle.png" ()
8 changes: 4 additions & 4 deletions examples/translate_ellipse.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ open Joy

let () =
init ();
background (1., 1., 1., 1.);
background (255, 255, 255, 255);
(* Create ellipse *)
let e1 = ellipse 60. 30. in
let e1 = ellipse 60 30 in
(* Translate it to the right by 100 and up by 50 *)
let e2 = translate 100. 50. e1 in
let e2 = translate 100 50 e1 in
(* Display both ellipses *)
set_color (0., 0., 0.);
set_color (0, 0, 0);
show [ e1; e2 ];
write ~filename:"translate_ellipse.png" ()
Loading
Loading