Agnel Kurian
Agnel Kurian

Reputation: 59516

How do I scale one rectangle to the maximum size possible within another rectangle?

I have a source rectangle and a destination rectangle. I need to find the maximum scale to which the source can be scaled while fitting within the destination rectangle and maintaining its original aspect ratio.

Google found one way to do it but I'm not sure if it works in all cases. Here is my home-brewed solution:

Looking for other possible solutions to this problem. I'm not even sure if my algorithm works in all cases!

Upvotes: 77

Views: 35606

Answers (5)

tom10
tom10

Reputation: 69242

scale = min(dst.width/src.width, dst.height/src.height)

This is your approach but written more cleanly.

To use this, the scaled rectangle would have the shape:

width = src.width * scale
height = src.height * scale

Upvotes: 159

Stephen Quan
Stephen Quan

Reputation: 26299

The other answers suffer from a risk of generating a division by zero exception when either the sourceWidth or sourceHeight becomes zero. To safeguard against this, we should rewrite the comparison into a mathematically equivalent multiple expression. Also, additional edge condition to catch the infinite scale scenario.

Apart from having the scale, I really wanted the dimensions of the target rectangle, so, here I will provide the scale calculation and the target rectangle calculation.

Because of the infinity edge condition, I think the target rectangle will be more robust / useful:

    if (sourceWidth == 0 && sourceHeight == 0) {
        // scale = Infinity;
        outputWidth = 0;
        outputHeight = 0;
        outputX = destWidth / 2;
        outputY = destHeight / 2;
    } else if (destWidth * sourceHeight > destHeight * sourceWidth) {
        scale = destHeight / sourceHeight;
        outputWidth = sourceWidth * destHeight / sourceHeight;
        outputHeight = destHeight;
        outputX = (destWidth - outputWidth) / 2;
        outputY = 0;
    } else {
        scale = destWidth / sourceWidth;
        outputWidth = destWidth;
        outputHeight = sourceHeight * destWidth / sourceWidth;
        outputX = 0;
        outputY = (destHeight - outputHeight) / 2;
    }

Upvotes: 0

Guss
Guss

Reputation: 32394

Another option might be to scale to maximum width and then check if the scaled height is greater then the maximum allowed height and if so scale by height (or vice versa):

scale = (dst.width / src.width);
if (src.height * scale > dst.height)
 scale = dst.height / src.height;

I think this solution is both shorter, faster and easier to understand.

Upvotes: 13

Daniel Brückner
Daniel Brückner

Reputation: 59705

If all dimensions are non-zero, I would use the following code (that essentially matches your code).

scaleFactor = (outerWidth / outerHeight > innerWidth / innerHeight) 
    ? outerHeight / innerHeight
    : outerWidth / innerWidth

This can also be modified to allow any dimension to be zero if required.

Upvotes: 1

AakashM
AakashM

Reputation: 63378

  1. Work out the smaller of destWidth / srcWidth and destHeight / srcHeight
  2. Scale by that

edit it's of course the same as your method, with the pieces of the formula moved around. My opinion is that this is clearer semantically, but it's only that - an opinion.

Upvotes: 2

Related Questions