Skip to content

Commit

Permalink
Store ellipse rotation as degrees
Browse files Browse the repository at this point in the history
Stores ellipse rotation as degrees rather than radians. This is to ensure portability between backends, since radians is something specifc to the Cairo backend
  • Loading branch information
Sudha247 committed Mar 12, 2024
1 parent ee373c5 commit 1b3f202
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 9 deletions.
3 changes: 2 additions & 1 deletion lib/render.ml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ let draw_ellipse ctx { c; rx; ry; stroke; fill; rotation } =
let save_matrix = Cairo.get_matrix ctx.ctx in

(* Translate and scale to create an ellipse from a circle *)
let radians = to_radians rotation in
Cairo.translate ctx.ctx c.x (Float.neg c.y);
Cairo.rotate ctx.ctx rotation;
Cairo.rotate ctx.ctx radians;
Cairo.scale ctx.ctx rx ry;
Cairo.arc ctx.ctx 0. 0. ~r:1. ~a1:0. ~a2:(2. *. Float.pi);

Expand Down
4 changes: 2 additions & 2 deletions lib/shape.ml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type ellipse = {
c : float point;
rx : float;
ry : float;
rotation : float;
rotation : int;
stroke : color option;
fill : color option;
}
Expand Down Expand Up @@ -56,7 +56,7 @@ let rectangle ?(c = center) width height =

let ellipse ?(c = center) rx ry =
let rx, ry = (float_of_int rx, float_of_int ry) in
Ellipse { c; rx; ry; stroke = Some Color.black; fill = None; rotation = 0. }
Ellipse { c; rx; ry; stroke = Some Color.black; fill = None; rotation = 0 }

let line ?(a = center) b = Line { a; b; stroke = Color.black }

Expand Down
2 changes: 1 addition & 1 deletion lib/shape.mli
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type ellipse = {
c : float point;
rx : float;
ry : float;
rotation : float;
rotation : int;
stroke : color option;
fill : color option;
}
Expand Down
5 changes: 2 additions & 3 deletions lib/transform.ml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
open Shape
open Util

type transformation = shape -> shape

Expand Down Expand Up @@ -64,8 +65,6 @@ let rec scale factor = function
}
| Complex shapes -> Complex (List.map (scale factor) shapes)

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)
Expand All @@ -86,7 +85,7 @@ let rec rotate degrees = function
{
ellipse' with
c = rotate_point degrees ellipse'.c;
rotation = ellipse'.rotation +. to_radians degrees;
rotation = ellipse'.rotation + degrees;
}
| Line line' -> Line { line' with a = rotate_point degrees line'.a; b = rotate_point degrees line'.b }
| Polygon polygon' ->
Expand Down
2 changes: 1 addition & 1 deletion lib/util.ml
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ let rec partition n ?(step = 0) lst =

(* Misc *)
let range n = List.init n Fun.id
let to_radians degrees = degrees *. Stdlib.Float.pi /. 180.
let to_radians degrees = (float_of_int degrees) *. Stdlib.Float.pi /. 180.
2 changes: 1 addition & 1 deletion lib/util.mli
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ val ( >> ) : ('a -> 'b) -> ('b -> 'c) -> 'a -> 'c
val take : int -> 'a list -> 'a list * 'a list
val partition : int -> ?step:int -> 'a list -> 'a list list
val range : int -> int list
val to_radians : float -> float
val to_radians : int -> float

0 comments on commit 1b3f202

Please sign in to comment.