Scaling rectangle and keeping ratio when height and/or width exceed container

915 Views Asked by At

I have a 2D box with a Height of 206 and a Width of 202.

Inside of this box is a Rectangle. It fits until the height and/or width exceed the height/width of its container.

How can I scale the Rectangle and get a new height and width so whenever it exceeds the container box it shrinks back to the largest possible size with the same ratio while fitting into the container?

Example: The Rectangle has a height of 200 and a width of 100. I increase the height to 2000 and the width to 1000. I want to shrink it back down to a size that will fit in the box with a height of 206 and width of 202 while keeping the ratio of 2000:1000.

Note: The height might not always greater than the width.