Skip to content

Commit

Permalink
Merge branch 'main' into fix-rectangle
Browse files Browse the repository at this point in the history
  • Loading branch information
nikochiko committed Jan 26, 2024
2 parents 2da0935 + c38d8b9 commit ecba54b
Show file tree
Hide file tree
Showing 35 changed files with 239 additions and 246 deletions.
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)))
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);
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

(* 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
(* 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);
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" ()
2 changes: 1 addition & 1 deletion examples/donut_with_scale.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ open Joy

let () =
init ();
let c = circle 100. in
let c = circle 100 in
let hole = scale 0.5 c in
show [ c; hole ];
write ~filename:"donut_with_scale.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" ()
9 changes: 4 additions & 5 deletions examples/rectangle.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ open Joy

let () =
init ();
background (1., 1., 1., 1.);
(* creating a rectangle from points *)
let rect = rectangle 100. 200. in
set_color (0., 0., 0.);
render rect;
background (255, 255, 255, 255);
set_color (0, 0, 0);
let r = rectangle 100 200 in
show [r];
write ~filename:"rectangle.png" ()
10 changes: 5 additions & 5 deletions examples/rectangle_canvas.ml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
open Joy

let () =
init ~size:(500., 300.) ();
background (1., 1., 1., 1.);
let c = circle 50. in
set_color (0., 0., 0.);
init ~size:(500, 300) ();
background (255, 255, 255, 255);
let c = circle 50 in
set_color (0, 0, 0);
show [ c ];
write ~filename:"rectangular_canvas.png" ()
write ~filename:"rectangle_canvas.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" ()
Loading

0 comments on commit ecba54b

Please sign in to comment.