Skip to content

Commit

Permalink
exposed point type and refactored constructors+examples to make use (#76
Browse files Browse the repository at this point in the history
)
  • Loading branch information
FayCarsons authored Dec 13, 2023
1 parent b6f9054 commit 9614a05
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 24 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
_build
.vscode
.vscode
.DS_Store
2 changes: 1 addition & 1 deletion examples/complex.ml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ let () =
let complex_shape =
complex
(List.map
(fun (x, y) -> circle ~x:(x * radius) ~y:(y * radius) radius)
(fun (x, y) -> circle ~point:(point (x * radius) (y * radius)) radius)
coords)
in
(* translating that complex shape by radius / 2 *)
Expand Down
2 changes: 1 addition & 1 deletion examples/higher_transforms.ml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ let rec range a b = if a > b then [] else a :: range (a + 1) b

let () =
init ();
let initial = rectangle ~x:(-250) ~y:(-250) 100 100 in
let initial = rectangle ~point:(point (-250) (-250)) 100 100 in
let match_list l =
match l with
| [] -> [ transform initial ]
Expand Down
2 changes: 1 addition & 1 deletion examples/line.ml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ let lines =
List.map
(fun i ->
let newx = i |> inc |> ( * ) line_interval in
line ~x1:newx ~y1:0 newx 500)
line ~point_a:(point newx 0) (point newx 500))
(range 0 interval)

let _ =
Expand Down
34 changes: 18 additions & 16 deletions lib/shape.ml
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,27 @@ let rec render_shape s =
draw_line a.x a.y b.x b.y
| Complex complex -> List.iter render_shape complex

let circle ?x ?y r =
match (x, y) with
| Some x, Some y -> Circle { c = { x; y }; radius = r }
let point x y = { x; y }

let circle ?point r =
match point with
| Some point -> Circle { c = point; radius = r }
| _ -> Circle { c = { x = 0; y = 0 }; radius = r }

let rectangle ?x ?y length width =
match (x, y) with
| Some x, Some y -> Rectangle { c = { x; y }; length; width }
let rectangle ?point length width =
match point with
| Some point -> Rectangle { c = point; length; width }
| _ -> Rectangle { c = { x = 0; y = 0 }; length; width }

let ellipse ?x ?y rx ry =
match (x, y) with
| Some x, Some y -> Ellipse { c = { x; y }; rx; ry }
let ellipse ?point rx ry =
match point with
| Some point -> Ellipse { c = point; rx; ry }
| _ -> Ellipse { c = { x = 0; y = 0 }; rx; ry }

let line ?x1 ?y1 x2 y2 =
match (x1, y1) with
| Some x, Some y -> Line { a = { x; y }; b = { x = x2; y = y2 } }
| _ -> Line { a = { x = 0; y = 0 }; b = { x = x2; y = y2 } }
let line ?point_a point_b =
match point_a with
| Some point_a -> Line { a = point_a; b = point_b }
| _ -> Line { a = { x = 0; y = 0 }; b = point_b }

let complex shapes =
match shapes with _ :: _ -> Complex shapes | [] -> Complex []
Expand Down Expand Up @@ -91,13 +93,13 @@ let rec scale factor s =
let scale_length len fact = round (float_of_int len *. sqrt fact) in
match s with
| Circle circle' ->
circle ~x:circle'.c.x ~y:circle'.c.y (scale_length circle'.radius factor)
circle ~point:circle'.c (scale_length circle'.radius factor)
| Rectangle rectangle' ->
rectangle ~x:rectangle'.c.x ~y:rectangle'.c.y
rectangle ~point:rectangle'.c
(scale_length rectangle'.length factor)
(scale_length rectangle'.width factor)
| Ellipse ellipse' ->
ellipse ~x:ellipse'.c.x ~y:ellipse'.c.y
ellipse ~point:ellipse'.c
(scale_length ellipse'.rx factor)
(scale_length ellipse'.ry factor)
| Line _line' -> failwith "Not Implemented"
Expand Down
10 changes: 6 additions & 4 deletions lib/shape.mli
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
type point
type shape
type shapes = shape list

val render_shape : shape -> unit
val circle : ?x:int -> ?y:int -> int -> shape
val rectangle : ?x:int -> ?y:int -> int -> int -> shape
val ellipse : ?x:int -> ?y:int -> int -> int -> shape
val point : int -> int -> point
val circle : ?point:point -> int -> shape
val rectangle : ?point:point -> int -> int -> shape
val ellipse : ?point:point -> int -> int -> shape
val complex : shape list -> shape
val line : ?x1:int -> ?y1:int -> int -> int -> shape
val line : ?point_a:point -> point -> shape
val translate : int -> int -> shape -> shape
val show : shape list -> unit
val scale : float -> shape -> shape
Expand Down

0 comments on commit 9614a05

Please sign in to comment.