Skip to content

Commit

Permalink
Refactor Transform.compose to have more specific typing (#81)
Browse files Browse the repository at this point in the history
* Refactor Transform.compose to have more specific typing

* added 'transform' functions type and refactored trasnformations to use it

* applying suggestions

* Fix transformation typing

---------

Co-authored-by: Kaustubh Maske Patil <[email protected]>
  • Loading branch information
FayCarsons and nikochiko authored Jan 26, 2024
1 parent a18f662 commit c38d8b9
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 12 deletions.
4 changes: 2 additions & 2 deletions examples/translate_rectangle.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ open Joy
let () =
init ();
background (255, 255, 255, 255);
(* Create rectangle transform *)
(* Create rectangle transformation *)
let r1 = rectangle 200 100 in
let r2 = translate 100 0 r1 in

(* Display rectangle transform *)
(* Display rectangle transformation *)
set_color (0, 0, 0);
show [ r1; r2 ];
write ~filename:"translate_rectangle.png" ()
2 changes: 2 additions & 0 deletions lib/joy.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ type 'a point = 'a Shape.point
type shape = Shape.shape
type shapes = Shape.shapes

type transformation = Transform.transformation

let point = Shape.point
let circle = Shape.circle
let rectangle = Shape.rectangle
Expand Down
12 changes: 7 additions & 5 deletions lib/joy.mli
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@ type 'a point = 'a Shape.point
type shape = Shape.shape
type shapes = Shape.shapes

type transformation = Transform.transformation

val point : int -> int -> float point
val circle : ?c:float point -> int -> shape
val rectangle : ?c:float point -> int -> int -> shape
val ellipse : ?c:float point -> int -> int -> shape
val line : ?a:float point -> float point -> shape
val polygon : float point list -> shape
val complex : shapes -> shape
val rotate : int -> shape -> shape
val translate : int -> int -> shape -> shape
val scale : float -> shape -> shape
val compose : ('a -> 'b) -> ('b -> 'c) -> 'a -> 'c
val repeat : int -> (shape -> shape) -> shape -> shape
val rotate : int -> transformation
val translate : int -> int -> transformation
val scale : float -> transformation
val compose : transformation -> transformation -> transformation
val repeat : int -> transformation -> transformation
val context : Context.context option ref
val set_color : int * int * int -> unit
val background : int * int * int * int -> unit
Expand Down
2 changes: 2 additions & 0 deletions lib/transform.ml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
open Shape

type transformation = (shape -> shape)

let rec translate dx dy shape =
match shape with
| Circle circle ->
Expand Down
12 changes: 7 additions & 5 deletions lib/transform.mli
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
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
type transformation = (Shape.shape -> Shape.shape)

val translate : int -> int -> transformation
val scale : float -> transformation
val rotate : int -> transformation
val compose : transformation -> transformation -> transformation
val repeat : int -> transformation -> transformation

0 comments on commit c38d8b9

Please sign in to comment.