Determine scale for random x and y so that aspect ratio is maintained and product is <256

781 Views Asked by At

It's entirely possible that this question already exists on here, I just simply don't have the vocabulary to search for it. That fact probably shows in the question title as well. Here's the question:

The largest size output by the third-party renderer I'm working with is 256 in^2. There are no proscriptions on width and height. So, what I need to calculate is, for all area > 256, the scale that would need to be applied to width and height, retaining the aspect ratio between the two and reducing the area to ~256.

Thanks.

3

There are 3 best solutions below

2
On BEST ANSWER

Let $w$ be width and $h$ be height. We want to find a scale factor $s$ such that $$(sw)(sh) = 256.$$ Solving for $s$, we have $$s = \sqrt{\frac{256}{wh}} = \frac{16}{\sqrt{wh}}.$$

And then the scaled width and scaled height are $sw$ and $sh$, respectively.

For example, suppose you are given $w=8.5$ and $h=11$. The formula above says $s=\frac{16}{\sqrt{8.5\cdot11}} \approx 1.655$, so you should scale the width and height by this factor, obtaining a new width and height of $8.5\cdot1.655 = 14.064$ and $11\cdot1.655 = 18.201$ respectively; note that the aspect ratio $$\frac{14.064}{18.201} = \frac{8.5}{11} \approx 77.27\%$$ and that the scaled area $$14.064\cdot 18.201 = 256$$ almost exactly.

0
On

I may be missing something but this is what I believe you are getting at:

x=cy (maintains the scale)

xy<256 as given by the renderer

using substitution:

x*x/c<256

x< sqrt(256*c)

Punch that into a calculator and you will find a max value for x and because of the ratio (first equation) you will have also found a value of y.

3
On

Let $x_1$ and $y_1$ be the width and height of the original size, and $x_2$ and $y_2$ be the width and height of the final size.

For aspect ratio to be the same, we want $x_2=c\cdot x_1$ and $y_2=c\cdot y_1$. We also want the area to be fixed:

$$x_2\cdot y_2=256$$

Thus: $$(c\cdot x_1)(c\cdot y_1) = 256$$ Rearranging: $$c^2 = \frac{256}{x_1y_1}$$

So, the scaling factor to resize the area is given by: $$c = \sqrt{\frac{256}{x_1y_1}}$$