How to re-scale a rectangle to a smaller area?

237 Views Asked by At

I have a rectangle of X by Y, with an area Z. I need to re-scale the rectangle so that it's area is no larger than the pre-defined AREA_MAX, preserving the original aspect ratio. How would I go about doing that?

X*Y = Z
if(Z > AREA_MAX){
  Z = AREA_MAX
  X = ?
  Y = ?
}

Thank you

1

There are 1 best solutions below

1
On BEST ANSWER

Well, you have $$ XY = Z \qquad \Rightarrow \qquad \frac{Z_{\text{max}}}{Z} XY = \frac{Z_{\text{max}}}{Z} Z = Z_{\text{max}} $$ And if you want to scale both $X$ and $Y$ equally, you can put $$ \sqrt{\frac{Z_{\text{max}}}{Z}} X \times \sqrt{\frac{Z_{\text{max}}}{Z}} Y = Z_{\text{max}} $$ Therefore, you can scale both $X$ and $Y$ by $\sqrt{\frac{Z_{\text{max}}}{Z}} $, where $Z=XY$.