Skip to content

Commit

Permalink
transition to int api
Browse files Browse the repository at this point in the history
  • Loading branch information
FayCarsons committed Feb 2, 2024
1 parent a9989a8 commit c42c4a1
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 27 deletions.
7 changes: 2 additions & 5 deletions examples/dune
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,11 @@
(libraries joy))

(executable
<<<<<<< HEAD
(name donut_with_scale)
(modules donut_with_scale)
(libraries joy))

(executable
=======
>>>>>>> 00f077b (refactoring for clustering behavior + fmt)

(executable
(name quadtree)
(modules quadtree)
(libraries joy))
32 changes: 17 additions & 15 deletions examples/quadtree.ml
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
open Joy

type point = Joy.point
type point = float Joy.point

(* Constants *)
let size = 800.
Expand All @@ -12,20 +10,20 @@ let clusters = 32
let _ = Random.self_init ()

(* Point utils *)
let splat n = point n n
let splat n : point = { x = n; y = n }

let pmap2 f ({ x = x1; y = y1 } : point) ({ x = x2; y = y2 } : point) =
point (f x1 x2) (f y1 y2)
let pmap2 f ({ x = x1; y = y1 } : point) ({ x = x2; y = y2 } : point) : point =
{ x = f x1 x2; y = f y1 y2 }

let ( +~ ) (p1 : point) (p2 : point) : point =
point (p1.x +. p2.x) (p1.y +. p2.y)
{ x = p1.x +. p2.x; y = p1.y +. p2.y }

let ( /! ) ({ x; y } : point) scalar : point =
{ x = x /. scalar; y = y /. scalar }

(* Random utils for creating random clustered points *)
let rand_point () =
point (Random.float size -. half_size) (Random.float size -. half_size)
let rand_point () : point =
{ x = Random.float size -. half_size; y = Random.float size -. half_size }

let centered_point (center : point) _ : point =
let offset () = Random.float 100. -. 50. in
Expand Down Expand Up @@ -92,9 +90,11 @@ let build () =

let to_flat_shapes tree =
let rect_of_bb bb =
rectangle ~c:(midpoint bb) (bb.max.x -. bb.min.x) (bb.max.y -. bb.min.y)
Joy.rectangle ~c:(midpoint bb)
(int_of_float (bb.max.x -. bb.min.x))
(int_of_float (bb.max.y -. bb.min.y))
in
let circle_of_point pt = circle ~c:pt 1. in
let circle_of_point pt = Joy.circle ~c:pt 1 in
let rec convert xs = function
| Node children -> List.flatten (List.map (convert xs) children)
| Leaf (aabb, es) ->
Expand All @@ -106,19 +106,21 @@ let to_flat_shapes tree =
(* With color handling system this function won't be necessary as color can be
decided at construction *)
let render_color shape =
let open Joy in
match shape with
| Shape.Circle _ ->
set_color (1., 1. /. 255., 1. /. 255.);
set_color (255, 1, 1);
render shape
| _ ->
set_color (0., 0., 0.);
set_color (0, 0, 0);
render shape

let () =
let open Joy in
init ();
background (1., 1., 1., 1.);
background (255, 255, 255, 255);
let tree = build () in
let to_shapes = to_flat_shapes tree in
set_color (0., 0., 0.);
set_color (0, 0, 0);
List.iter render_color to_shapes;
write ~filename:"quadtree.png" ()
2 changes: 1 addition & 1 deletion examples/rectangle.ml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ let () =
background (255, 255, 255, 255);
set_color (0, 0, 0);
let r = rectangle 100 200 in
show [r];
show [ r ];
write ~filename:"rectangle.png" ()
3 changes: 1 addition & 2 deletions examples/square.ml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ let () =
background (255, 255, 255, 255);
let square = rectangle 100 100 in
set_color (0, 0, 0);
show [square];
show [ square ];
write ~filename:"square.png" ()

1 change: 0 additions & 1 deletion lib/joy.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ let context = Context.context
type 'a point = 'a Shape.point
type shape = Shape.shape
type shapes = Shape.shapes

type transformation = Transform.transformation

let point = Shape.point
Expand Down
1 change: 0 additions & 1 deletion lib/joy.mli
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
type 'a point = 'a Shape.point
type shape = Shape.shape
type shapes = Shape.shapes

type transformation = Transform.transformation

val point : int -> int -> float point
Expand Down
2 changes: 1 addition & 1 deletion lib/transform.ml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
open Shape

type transformation = (shape -> shape)
type transformation = shape -> shape

let rec translate dx dy shape =
match shape with
Expand Down
2 changes: 1 addition & 1 deletion lib/transform.mli
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type transformation = (Shape.shape -> Shape.shape)
type transformation = Shape.shape -> Shape.shape

val translate : int -> int -> transformation
val scale : float -> transformation
Expand Down

0 comments on commit c42c4a1

Please sign in to comment.