Skip to content

Commit

Permalink
Updated with main
Browse files Browse the repository at this point in the history
  • Loading branch information
Nangah Amandine Chi authored and Nangah Amandine Chi committed Oct 27, 2023
2 parents 509cb37 + b6f9054 commit 7a27280
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 27 deletions.
7 changes: 4 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
name: main

on:
pull_request:
push:
branches:
- main
- main


jobs:
build:
runs-on: ${{matrix.os}}
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macOS-latest]
ocaml-compiler: ["ocaml.5.0.0", "ocaml-option-mingw"]

steps:
- name: Checkout code
Expand All @@ -22,7 +23,7 @@ jobs:
with:
opam-pin: false
opam-depext: false
ocaml-compiler: ${{ matrix.ocaml-compiler }}
ocaml-compiler: ocaml.5.0.0,ocaml-option-mingw
opam-repositories: |
dra27: https://github.com/dra27/opam-repository.git#windows-5.0
default: https://github.com/ocaml-opam/opam-repository-mingw.git#sunset
Expand Down
34 changes: 34 additions & 0 deletions examples/complex.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
open Joy.Shape

(*
Complex shapes can also be created from lists of shapes.
This allows us to apply any of the libraries' transformations to a group of
shapes, like we're doing here with translate
*)

(* creates a list containing numbers between a and b *)
let rec range a b = if a > b then [] else a :: range (a + 1) b

(* creates a list of (int * int) tuples,
containing every combination of the elements of the two passsed lists *)
let cartesian_product l l' =
List.concat (List.map (fun e -> List.map (fun e' -> (e, e')) l') l)

let () =
init ();
(* radius which also acts as grid spacing *)
let radius = 50 in
let half_radius = radius / 2 in
(* creating a grid with cartesian_product *)
let coords = cartesian_product (range (-5) 5) (range (-5) 5) in
(* using map to turn that into a complex shape that is a grid of circles *)
let complex_shape =
complex
(List.map
(fun (x, y) -> circle ~x:(x * radius) ~y:(y * radius) radius)
coords)
in
(* translating that complex shape by radius / 2 *)
let complex_transformed = translate half_radius half_radius complex_shape in
show [ complex_shape; complex_transformed ];
close ()
5 changes: 5 additions & 0 deletions examples/dune
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,8 @@
(name ellipse_rotate)
(modules ellipse_rotate)
(libraries joy))

(executable
(name complex)
(modules complex)
(libraries joy))
21 changes: 10 additions & 11 deletions examples/higher_transforms.ml
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
open Joy.Shape

(* Higher order transformations can be composed with `comp`,
(* Higher order transformations can be composed with `comp`,
which applies its function args right-to-left.
This allows us to create complex series transformations,
that can be applied iteratively *)
let transform = compose (translate 10 10) (scale 0.9)

let rec range a b = if a > b then [] else a :: range (a + 1) b

let () =
let () =
init ();
let initial = rectangle ~x:(-250) ~y:(-250) 100 100 in
let match_list l =
match l with
| [] -> [(transform initial)]
| last :: _ -> (transform last) :: l
in
let shapes = List.fold_right (fun _ acc -> (match_list acc)) (range 0 32) [] in
let initial = rectangle ~x:(-250) ~y:(-250) 100 100 in
let match_list l =
match l with
| [] -> [ transform initial ]
| last :: _ -> transform last :: l
in
let shapes = List.fold_right (fun _ acc -> match_list acc) (range 0 32) [] in
show shapes;
close ()
close ()
30 changes: 19 additions & 11 deletions lib/shape.ml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type shape =
| Rectangle of rectangle
| Ellipse of ellipse
| Line of line
| Complex of shape list

type shapes = shape list

Expand All @@ -27,7 +28,7 @@ let draw_line x1 y1 x2 y2 = draw_poly_line [| (x1, y1); (x2, y2) |]
let denormalize point =
{ x = point.x + canvas_mid.x; y = point.y + canvas_mid.y }

let deg_to_rad degrees = degrees *. (Float.pi /. 180.) (* Define deg_to_rad *)
let deg_to_rad degrees = degrees *. (Float.pi /. 180.)

let rotate_point point radians =
let cartesian_to_polar { x; y } =
Expand All @@ -48,7 +49,7 @@ let rotate_point point radians =
let { x; y } = polar_to_cartesian (r, theta +. radians) in
{ x = x + x'; y = y + y' }

let render_shape s =
let rec render_shape s =
match s with
| Circle circle ->
let rotated_center = rotate_point circle.c (deg_to_rad 30.0) in
Expand All @@ -65,6 +66,7 @@ let render_shape s =
let a = denormalize (rotate_point line.a (deg_to_rad 30.0)) in
let b = denormalize (rotate_point line.b (deg_to_rad 30.0)) in
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
Expand All @@ -86,7 +88,10 @@ let line ?x1 ?y1 x2 y2 =
| 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 translate dx dy shape =
let complex shapes =
match shapes with _ :: _ -> Complex shapes | [] -> Complex []

let rec translate dx dy shape =
match shape with
| Circle circle ->
Circle { circle with c = { x = circle.c.x + dx; y = circle.c.y + dy } }
Expand All @@ -100,13 +105,14 @@ let translate dx dy shape =
Ellipse
{ ellipse with c = { x = ellipse.c.x + dx; y = ellipse.c.y + dy } }
| Line line ->
Line
{
a = { x = line.a.x + dx; y = line.a.y + dy };
b = { x = line.b.x + dx; y = line.b.y + dy };
}

let scale factor s =
Line
{
a = { x = line.a.x + dx; y = line.a.y + dy };
b = { x = line.b.x + dx; y = line.b.y + dy };
}
| Complex shapes -> Complex (List.map (translate dx dy) shapes)

let rec scale factor s =
let round x = int_of_float (x +. 0.5) in
let scale_length len fact = round (float_of_int len *. sqrt fact) in
match s with
Expand All @@ -121,12 +127,13 @@ let scale factor s =
(scale_length ellipse'.rx factor)
(scale_length ellipse'.ry factor)
| Line _line' -> failwith "Not Implemented"
| Complex shapes -> Complex (List.map (scale factor) shapes)

let show shapes = List.iter render_shape shapes

let deg_to_rad degrees = degrees *. (Stdlib.Float.pi /. 180.)

let rotate degrees shape =
let rec rotate degrees shape =
match shape with
| Circle circle ->
let rotated_center = rotate_point circle.c (deg_to_rad (float_of_int degrees)) in
Expand All @@ -148,6 +155,7 @@ let rotate degrees shape =
ry = ellipse.ry;
}
| Line _line -> failwith "Not Implemented"
| Complex shapes -> Complex (List.map (rotate degrees) shapes)

let compose f g x = g (f x)

Expand Down
3 changes: 1 addition & 2 deletions lib/shape.mli
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ 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 complex : shape list -> shape
val line : ?x1:int -> ?y1:int -> int -> int -> shape
val translate : int -> int -> shape -> shape
val show : shape list -> unit
val scale : float -> shape -> shape
val rotate : int -> shape -> shape

val compose : ('a -> 'b) -> ('b -> 'c) -> 'a -> 'c

val draw_axes : bool -> unit
val set_dimensions : int -> int -> unit
val init : unit -> unit
Expand Down

0 comments on commit 7a27280

Please sign in to comment.