Sudha247 / ocaml-joy

MIT License
22 stars 14 forks source link

Repeat transformation #45

Closed FayCarsons closed 7 months ago

FayCarsons commented 11 months ago

The original Joy python library has a "higher order transformation" called repeat that applies a set of transformations iteratively to a provided initial shape.

Now that we have some transformations, rotate, translate, scale(I think? Not sure if it's been merged), this seems like it would be a fun feature to have.

I have a clear idea of how to implement this declaratively with List.fold_right and would love to get started on it ASAP :)

nangahamandine commented 11 months ago

@FayCarsons this is quite nice! I like how you have the most amazing ideas and are so enthusiastic, it's motivating... :) Would love to see how this plays out!

nikochiko commented 10 months ago

@FayCarsons i feel we need a few more changes before it would make sense to start working on this:

A type for group of shapes

Reason behind a separate type instead of using list shape is that I want to be able to make a new shape from existing shapes, and then apply transformations on them as a whole like any other shape -- rather than having to handle that type specially.

e.g. let's say we have a donut

let donut = shape_sum (circle 50) (circle 100 |> color "black")

I want to be able to scale that donut the same way I scale other shapes

let () = show [donut |> scale 0.5]

Representing series of transformations

This is so that we can combine transformations and represent slightly more advanced transformations such as "scale this shape by 0.5 then rotate by 45 degrees".

We can probably recommend using currying and something like composition for this, and utilise the function signature of a transformation (which is shape -> shape).

Something like:

(* compose right-to-left -- function on right is applied first *)
let compose_rtl f g x = g (f x)

let () =
  let halve_and_rotate = compose_rtl (scale 0.5) (rotate 30) in
  let rectangles = List.init 6 (fun _ -> rectangle 50 50) in
  show (List.map halve_and_rotate rectangles)

We should create examples for such use cases if this API looks good enough.

@Sudha247 what do you think?

Sudha247 commented 10 months ago

Indeed, shape group and compose transformation are useful features to have. @FayCarsons are you interested in making PRs for those? I'd recommend making them as separate PRs. It's easier to review and merge that way.

FayCarsons commented 10 months ago

I can absolutely take these on! May need to ask more questions than usual but they both seem doable to me.

In the case of the sort of compound shape type, is there any reason we couldn't just extend the shape type to also include shape list and add cases in match blocks to handle that? Like refactoring translate, for example, to map over a shape list if passed that instead of a single shape.

Sudha247 commented 7 months ago

Fixed by #71.