Calculate bounding size to fit rectangle

930 Views Asked by At

I hope I have phrased my question correctly.

Let's assume I have a rectangle with a width and height of X and Y.
Then I pick an aspect ratio of 0.56.
How can I calculate the size of the rectangle that bounds the original rectangle, without shrinking it?

In other words, I want to keep the original rectangle as is, and "place" it inside a canvas that obeys the aspect ratio, I'm trying to calculate the canvas size.

Any help would be highly appreciated.

Best regards,
Roi

1

There are 1 best solutions below

1
On BEST ANSWER

Let $x, y$ be the dimensions of the window that you want to fit into a canvas with aspect ratio $r$. Let the desired dimensions of the canvas be $x', y'$. In all cases, we must have $y'/x' = r$. Three cases:

  • if $y/x = r$, then both dimensions fit just right: let $x' = x, y' = y$.
  • if $y/x > r$, the rectangle is tall, so we fit the y-dimension: $y' = y$ and since $y'/x' = r$, we have $y/x' = r \implies x'= y/r$.
  • if $y/x < r$, then we fit the x-dimension: $x' = x$ and since $y'/x' = r$, we have $y'/x = r \implies y' = xr$.