Skip to content

Commit

Permalink
Int API
Browse files Browse the repository at this point in the history
  • Loading branch information
FayCarsons committed Jan 16, 2024
1 parent 26221d2 commit ddd6677
Show file tree
Hide file tree
Showing 13 changed files with 67 additions and 67 deletions.
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 (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" ()
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" ()
38 changes: 21 additions & 17 deletions examples/circle_packing.ml
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@ let shrink_factor = 0.85
let _ = Stdlib.Random.self_init ()

let palette =
[
(* 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 @@ -101,6 +101,10 @@ let () =
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 (int_of_float x) (int_of_float y)) (int_of_float 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" ()
3 changes: 1 addition & 2 deletions examples/complex.ml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ let () =
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 *)
Expand Down
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))
5 changes: 4 additions & 1 deletion examples/line.ml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ let _ =
(fun i ->
let newx = i |> inc |> ( *. ) line_interval in
line
~a:(point (int_of_float (newx -. half_size)) (int_of_float (-.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
Expand Down
2 changes: 1 addition & 1 deletion examples/repeat.ml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ let () =
init ();
background (255, 255, 255, 255);
let circle = circle ~c:(point (-100) 0) 50 in
let shapes = repeat 10 (translate 10 0) circle in
let shapes = repeat 10 (translate 10 0) circle in
set_color (0, 0, 0);
render shapes;
write ~filename:"repeat.png" ()
14 changes: 5 additions & 9 deletions lib/context.ml
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,16 @@ let init_context line_width (w, h) axes =
if Option.is_some !context then
raise (Context "Cannot initialize context twice");

let surface =
Cairo.Image.create Cairo.Image.ARGB32 ~w
~h
in
let surface = Cairo.Image.create Cairo.Image.ARGB32 ~w ~h in
let ctx = Cairo.create surface in
Cairo.scale ctx (float_of_int w ) (float_of_int h);
Cairo.scale ctx (float_of_int w) (float_of_int h);
Cairo.set_line_width ctx line_width;
context := Some { ctx; surface; size = (w, h); axes }

let resolution () = match !context with Some ctx -> ctx.size | None -> fail ()

let tmap3 f (a, b ,c) = (f a, f b, f c)
let tmap4 f (a, b , c, d) = (f a, f b, f c, f d)
let (>>) f g x = g (f x)
let tmap3 f (a, b, c) = (f a, f b, f c)
let tmap4 f (a, b, c, d) = (f a, f b, f c, f d)
let ( >> ) f g x = g (f x)
let scale_color_channel x = x /. 256.

let set_color color =
Expand Down
5 changes: 1 addition & 4 deletions lib/joy.mli
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ val context : Context.context option ref
val set_color : int * int * int -> unit
val background : int * int * int * int -> unit
val set_line_width : int -> unit

val init :
?line_width:int -> ?size:int * int -> ?axes:bool -> unit -> unit

val init : ?line_width:int -> ?size:int * int -> ?axes:bool -> unit -> unit
val render : shape -> unit
val show : shapes -> unit
val write : ?filename:string -> unit -> unit
4 changes: 2 additions & 2 deletions lib/render.ml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
open Shape
open Context

let tmap f (x, y) = (f x, f y)
let tmap f (x, y) = (f x, f y)

let denormalize point =
let x, y = Context.resolution () in
Expand All @@ -22,7 +22,7 @@ let draw_circle ctx ({ c; radius } : circle) =
Cairo.stroke ctx.ctx

let create_control_points { c; rx; ry } =
let size = resolution () |> tmap float_of_int in
let size = resolution () |> tmap float_of_int in
let x, y = scale_point size c in
let half_height = ry /. snd size in
let width_two_thirds = rx /. fst size *. (2. /. 3.) *. 2. in
Expand Down
18 changes: 7 additions & 11 deletions lib/shape.ml
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,15 @@ let ( /~ ) p1 p2 = { x = p1.x /. p2.x; y = p1.x /. p2.x }
(* point -> scalar arithmetic *)
let ( -! ) { x; y } scalar = { x = x -. scalar; y = y -. scalar }
let ( /! ) { x; y } scalar = { x = x /. scalar; y = y /. scalar }
let ( *! ) {x; y} scalar = {x = x *. scalar; y = y *. scalar}
let pmap f {x; y} = { x = f x; y = f y}
let ( *! ) { x; y } scalar = { x = x *. scalar; y = y *. scalar }
let pmap f { x; y } = { x = f x; y = f y }

let point x y =
let x, y = (float_of_int x, float_of_int y) in
{ x; y }

let center = {x = 0.; y = 0.}
{ x; y }

let circle ?(c = center) r =
Circle { c; radius = float_of_int r }
let center = { x = 0.; y = 0. }
let circle ?(c = center) r = Circle { c; radius = float_of_int r }

let rectangle ?(c = center) width height =
let width, height = (float_of_int width, float_of_int height) in
Expand All @@ -40,15 +39,12 @@ let rectangle ?(c = center) width height =
{ x = x +. width; y = y +. height };
{ x = x +. width; y };
]


let ellipse ?(c = center) rx ry =
let rx, ry = (float_of_int rx, float_of_int ry) in
Ellipse { c; rx; ry }

let line ?(a = center) b =
Line { a; b }

let line ?(a = center) b = Line { a; b }
let polygon lst_points = Polygon lst_points

let complex shapes =
Expand Down
1 change: 0 additions & 1 deletion lib/shape.mli
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,3 @@ val ellipse : ?c:float point -> int -> int -> shape
val complex : shape list -> shape
val line : ?a:float point -> float point -> shape
val polygon : float point list -> shape

22 changes: 13 additions & 9 deletions lib/transform.ml
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,15 @@ let rec scale factor s =
let scale_point fact pt = pt *! sqrt fact in
match s with
| Circle circle' ->
Circle { c = scale_point factor circle'.c; radius = scale_length factor circle'.radius }
Circle
{
c = scale_point factor circle'.c;
radius = scale_length factor circle'.radius;
}
| Ellipse ellipse' ->
Ellipse
{
c = scale_point factor ellipse'.c;
c = scale_point factor ellipse'.c;
rx = scale_length factor ellipse'.rx;
ry = scale_length factor ellipse'.ry;
}
Expand All @@ -46,32 +50,32 @@ let to_radians degrees = float_of_int degrees *. Stdlib.Float.pi /. 180.

let to_polar point =
let { x; y } = point in
( sqrt ((x *. x) +. (y *. y)),
atan2 y x )
(sqrt ((x *. x) +. (y *. y)), atan2 y x)

let from_polar polar_point =
let (r, theta) = polar_point in
let r, theta = polar_point in
{ x = r *. cos theta; y = r *. sin theta }

let rotate_point degrees point =
let radians = to_radians degrees in
let (r, theta) = to_polar point in
let r, theta = to_polar point in
from_polar (r, theta +. radians)

let rec rotate degrees shape =
match shape with
| Circle circle' -> Circle { circle' with c = rotate_point degrees circle'.c }
| Ellipse ellipse' -> Ellipse { ellipse' with c = rotate_point degrees ellipse'.c }
| Ellipse ellipse' ->
Ellipse { ellipse' with c = rotate_point degrees ellipse'.c }
| Line _line -> failwith "Not Implemented"
| Polygon polygon' -> polygon (List.map (rotate_point degrees) polygon')
| Complex shapes -> Complex (List.map (rotate degrees) shapes)

let compose f g x = g (f x)

let range n = List.init n Fun.id

let repeat n op shape =
let match_list l =
match l with [] -> [ shape ] | last :: _ -> op last :: l
match l with [] -> [ op shape ] | last :: _ -> op last :: l
in
let shapes = List.fold_right (fun _ acc -> match_list acc) (range n) [] in
complex shapes
2 changes: 1 addition & 1 deletion lib/transform.mli
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ val translate : int -> int -> Shape.shape -> Shape.shape
val scale : float -> Shape.shape -> Shape.shape
val rotate : int -> Shape.shape -> Shape.shape
val compose : ('a -> 'b) -> ('b -> 'c) -> 'a -> 'c
val repeat : int -> (Shape.shape -> Shape.shape) -> Shape.shape -> Shape.shape
val repeat : int -> (Shape.shape -> Shape.shape) -> Shape.shape -> Shape.shape

0 comments on commit ddd6677

Please sign in to comment.