diff --git a/examples/axes.ml b/examples/axes.ml index d3cc4ca..1d6a467 100644 --- a/examples/axes.ml +++ b/examples/axes.ml @@ -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" () diff --git a/examples/circle_packing.ml b/examples/circle_packing.ml index 8013ead..c773060 100644 --- a/examples/circle_packing.ml +++ b/examples/circle_packing.ml @@ -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 *) @@ -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" () diff --git a/examples/complex.ml b/examples/complex.ml index 00928f3..1e26925 100644 --- a/examples/complex.ml +++ b/examples/complex.ml @@ -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 *) diff --git a/examples/dune b/examples/dune index b828d8b..07ee91d 100644 --- a/examples/dune +++ b/examples/dune @@ -82,8 +82,8 @@ (name star) (modules star) (libraries joy)) - -(executable + +(executable (name repeat) (modules repeat) (libraries joy)) diff --git a/examples/line.ml b/examples/line.ml index b10d2ae..98b9035 100644 --- a/examples/line.ml +++ b/examples/line.ml @@ -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 diff --git a/examples/repeat.ml b/examples/repeat.ml index d70e045..074a52f 100644 --- a/examples/repeat.ml +++ b/examples/repeat.ml @@ -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" () diff --git a/lib/context.ml b/lib/context.ml index a8a2a49..193767b 100644 --- a/lib/context.ml +++ b/lib/context.ml @@ -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 = diff --git a/lib/joy.mli b/lib/joy.mli index 198db34..d612149 100644 --- a/lib/joy.mli +++ b/lib/joy.mli @@ -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 diff --git a/lib/render.ml b/lib/render.ml index 47ad4ec..500a919 100644 --- a/lib/render.ml +++ b/lib/render.ml @@ -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 @@ -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 diff --git a/lib/shape.ml b/lib/shape.ml index 1794c55..348b9b9 100644 --- a/lib/shape.ml +++ b/lib/shape.ml @@ -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 @@ -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 = diff --git a/lib/shape.mli b/lib/shape.mli index 0b05511..44e4e10 100644 --- a/lib/shape.mli +++ b/lib/shape.mli @@ -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 - diff --git a/lib/transform.ml b/lib/transform.ml index c1d1f8d..2f9055f 100644 --- a/lib/transform.ml +++ b/lib/transform.ml @@ -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; } @@ -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 diff --git a/lib/transform.mli b/lib/transform.mli index a00c518..4dde434 100644 --- a/lib/transform.mli +++ b/lib/transform.mli @@ -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 \ No newline at end of file +val repeat : int -> (Shape.shape -> Shape.shape) -> Shape.shape -> Shape.shape