Skip to content

Commit

Permalink
Merge pull request #71 from FayCarsons/repeat
Browse files Browse the repository at this point in the history
Added `repeat` function from original Python library
  • Loading branch information
Sudha247 authored Dec 22, 2023
2 parents 91123cf + ebd1410 commit dcb902f
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 11 deletions.
28 changes: 28 additions & 0 deletions ellipse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import numpy as np
from joy import *


class Ellipse:
def __init__(self, center, radius_x, radius_y):
self.center = np.array(center)
self.radius_x = radius_x
self.radius_y = radius_y

def rotate(self, angle):
# Translate to origin
translated_center = np.array([0, 0])
translated_center[0] = self.center[0] - self.radius_x
translated_center[1] = self.center[1] - self.radius_y

# Rotate around the origin
rotated_center = self.rotate_point(translated_center, angle)

# Translate back to the original center
self.center[0] = rotated_center[0] + self.radius_x
self.center[1] = rotated_center[1] + self.radius_y

@staticmethod
def rotate_point(point, angle):
x_rot = point[0] * np.cos(angle) - point[1] * np.sin(angle)
y_rot = point[0] * np.sin(angle) + point[1] * np.cos(angle)
return np.array([x_rot, y_rot])
8 changes: 8 additions & 0 deletions examples/dune
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
(libraries joy))

(executable
<<<<<<< HEAD
(name polygon)
(modules polygon)
(libraries joy))
Expand All @@ -107,3 +108,10 @@
(name circle_packing)
(modules circle_packing)
(libraries joy))

(executable
=======
>>>>>>> 66df79e (formatting)
(name repeat)
(modules repeat)
(libraries joy))
16 changes: 5 additions & 11 deletions examples/higher_transforms.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,13 @@ open Joy.Shape

(* 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 *)
This allows us to create complex series of transformations,
that can be applied iteratively with the repeat function *)
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 () =
init ();
let initial = rectangle ~point:(point (-250) (-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;
let initial = rectangle ~point:{x = (-250); y = (-250)} 100 100 in
let shapes = repeat 32 transform initial in
render_shape shapes;
close ()
21 changes: 21 additions & 0 deletions examples/repeat.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
open Joy.Shape

(*
demonstration of the repeat function
takes n, an operation, and an initial shape, and applies the operation
iteratively to the initial shape n times
adapted from the original Joy python library's examples
*)

let () =
init ();
<<<<<<< HEAD
let circle = circle ~point:{x = (-100); y = 0} 50 in
let shapes = repeat 10 (translate 10 0) circle in
=======
let circle = circle ~x:(-100) ~y:0 50 in
let shapes = repeat 10 (translate 10 0) circle in
>>>>>>> 66df79e (formatting)
render_shape shapes;
close ()
8 changes: 8 additions & 0 deletions lib/shape.ml
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,14 @@ let rec rotate degrees shape =
| Complex shapes -> Complex (List.map (rotate degrees) shapes)

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

let repeat n op shape =
let match_list l =
match l with [] -> [ op shape ] | last :: _ -> op last :: l
in
let shapes = List.fold_right (fun _ acc -> match_list acc) (range 0 n) [] in
complex shapes

let render_axes () =
set_color (rgb 192 192 192);
Expand Down
1 change: 1 addition & 0 deletions lib/shape.mli
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ val show : shape list -> unit
val scale : float -> shape -> shape
val rotate : int -> shape -> shape
val compose : ('a -> 'b) -> ('b -> 'c) -> 'a -> 'c
val repeat : int -> (shape -> shape) -> shape -> shape
val draw_axes : bool -> unit
val set_dimensions : int -> int -> unit
val init : unit -> unit
Expand Down

0 comments on commit dcb902f

Please sign in to comment.