Resize a rectangle so that one of the dimensions meets its max size and the other side meets the max size or is smaller
If I have a rectangle of any dimensions $a \times b$, how can I resize those dimensions so that, depending on their proportions one (or both) will meet the max desired dimension and the other will be the same or smaller than the max desired dimension?
For example, the max dimensions may be $100 \times 50$. If I have a rectangle that is $a = 200$ & $b = 76$ then the result would be $a' = 100$ and $b' = 38$. If $a = 80$ and $b = 100$ then the result would be $a' = 40$ and $b' = 50$.
$\endgroup$ 12 Answers
$\begingroup$If your rectangle is $a\times b$ and the max dimensions are $c\times d$, then resize it by a factor of the minimum of $\frac{c}{a}$ and $\frac{d}{b}$. (This assumes that the sides stay in the same order.) In your example, scale by the minimum of $\frac{100}{200}$ and $\frac{38}{50}$, which is $\frac{1}{2}$.
$\endgroup$ 3 $\begingroup$Your examples provide the answer to the general question. Find the larger of the ratios
$$ \frac{a}{\text{max } a}, \frac{b}{\text{max } b} $$
and scale both $a$ and $b$ by the reciprocal of that maximum.
That will work even if both ratios are less than $1$ and the original rectangle fits inside. It will expand rather than shrink.
$\endgroup$ 3