Skip to content

Commit

Permalink
Format with dune
Browse files Browse the repository at this point in the history
  • Loading branch information
nikochiko committed Jan 16, 2024
1 parent ee59e4e commit cfa6230
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 26 deletions.
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
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 (1., 1., 1., 1.);
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" ()
18 changes: 5 additions & 13 deletions lib/shape.ml
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,10 @@ 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 ( *! ) { x; y } scalar = { x = x *. scalar; y = y *. scalar }
let point x y = { x; y }

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

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

let rectangle ?(c = center) width height =
let { x; y } = c -! ((width +. height) /. 4.) in
Expand All @@ -36,14 +33,9 @@ let rectangle ?(c = center) width height =
{ x = x +. width; y = y +. height };
{ x = x +. width; y };
]


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

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

let ellipse ?(c = center) rx ry = Ellipse { c; rx; ry }
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 @@ -24,4 +24,3 @@ val ellipse : ?c:point -> float -> float -> shape
val complex : shape list -> shape
val line : ?a:point -> point -> shape
val polygon : point list -> shape

20 changes: 12 additions & 8 deletions lib/transform.ml
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@ let rec scale factor s =
let scale_point fact pt = pt *! 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 @@ -42,29 +46,29 @@ 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 [] -> [ op shape ] | last :: _ -> op last :: l
Expand Down
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 : float -> float -> 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 cfa6230

Please sign in to comment.