Sudha247 / ocaml-joy

MIT License
22 stars 14 forks source link

center -> multiply x and y coordinates by the scaling factor #61

Closed nikochiko closed 6 months ago

nikochiko commented 10 months ago

Whenever we have center of a shape, we should also multiply it by the scaling factor inside the scale operation.

As of now, we are only scaling the dimensions.

nangahamandine commented 10 months ago

Can I work on this?

nangahamandine commented 10 months ago

From what I've understood so far, to ensure that the center of a shape is also scaled when we apply the scale operation, we can modify the scale function to adjust the center of the shape based on the scaling factor. Here's what I mean in the scale function:

   let 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
   let scale_point { x; y } fact = { x = round (float_of_int x *. fact); y = round (float_of_int y *. fact) } in

   match s with
   | Circle circle' ->
       let new_radius = scale_length circle'.radius factor in
       let scaled_center = scale_point circle'.c factor in
       Circle { c = scaled_center; radius = new_radius }
   | Rectangle rectangle' ->
       let new_length = scale_length rectangle'.length factor in
       let new_width = scale_length rectangle'.width factor in
       let scaled_center = scale_point rectangle'.c factor in
       Rectangle { c = scaled_center; length = new_length; width = new_width }
   | Ellipse ellipse' ->
       let new_rx = scale_length ellipse'.rx factor in
       let new_ry = scale_length ellipse'.ry factor in
       let scaled_center = scale_point ellipse'.c factor in
       Ellipse { c = scaled_center; rx = new_rx; ry = new_ry }
   | Line _line' -> failwith "Not Implemented"

In this scale function, we calculate the new dimensions of the shape based on the scaling factor (factor). Additionally, we calculate the scaled center of the shape by applying the scaling factor to the center's x and y coordinates. This ensures that both the dimensions and the center of the shape are scaled appropriately.

These are my thoughts, hope I'm right. Whoever finally takes on this issue, hope this helps.