# Calculating new aspect ratio problem

This is a discussion on Calculating new aspect ratio problem within the C Programming forums, part of the General Programming Boards category; Hi Guys, I'm writing a program that must display images of any size to a screen of dimensions 320x568. I'm ...

1. ## Calculating new aspect ratio problem

Hi Guys,

I'm writing a program that must display images of any size to a
screen of dimensions 320x568.
I'm handling fixing the aspect ratio myself.
I have handled the cases where both image dimensions are smaller
than screen dimensions (just print it in the middle of the screen).
And also handled where only one image dimension is larger than
one screen dimension (resize the image making the larger
image dimension match the screen dimension).

Having some trouble deciding what to do if both image dimensions
are larger than both screen dimensions:

Code:
```if (oldx > 320 && oldy > 568) { // image is taller and wider than the screen
if (oldx-320 < oldy-568) {
newx = oldx / oldy * 568; // get new width
newy = 568;
posx = centerx - (newx/2); // get new x position
posy = centery - (newy/2);
} else {
newy = oldy / oldx * 320; // get new height
newx = 320;
posy = centery - (newy/2); // get new y position
posx = centerx - (newx/2);
}
}```
centerx and century are just coordinates for the centre of the screen,
and might as well be 160 and 284...
They are only variables because I sometimes move the image around.

Now one example actual problem is, if the input image dimensions are
640x958, the output dimensions are 379x568 (adjusted to the wrong dimension so 379 is wider than the screen),
if the input dimensions are 534x660, the output is 320x395... Correct!!

I think the solution lies in the second if/then line.
Any help appreciated!!

EDIT,,, whoops.. posx and posy are the screen coords to draw the image.
if the image is 320x568, posx and posy should both be zero because the
image dimensions match the screen size exactly.

2. Personally, I would resize based on whichever dimension has the bigger overage in dimension, then the other.

Meaning if you have dimensions of 100x50 and you have an image of size 200x100, the 200 is 100 over the max width while the 100 is only 50 over the max height so I would resize (keeping the same aspect ration of 2:1) so that the width is 100 and the height in that case would then be 50 as well (this is a perfect scenario of course where the ratio comes out perfect).

3. Actually what I said is already what you are doing and is wrong to boot....it seems like you just need to instead record both aspect ratios by dividing the target height by the height of the image and same for the width, then multiply both dimensions by the smaller of the two quotients.

A simple google search of 'resize aspect ratio algorithm'

4. Yes, it is more compacted than it seems.
You can turn the greater than sign around, and different images will come out wrong.
I'll take a look on Google... how I got the aspect ratio formula in the first place..

5. Yeah this Stack Overflow discussion seems to have some pretty good solutions:
php - Algorithm to resize image and maintain aspect ratio to fit iPhone - Stack Overflow

6. Yes, iPhone 5, good guess!

7. The main change you need to make for this sort of thing is to do the multiplication before the division.

8. Is that faster? The aspect ratio formula is correct,
Just the logic I'm applying to use it.

9. I did solve it myself, although in a way that I'm sure would be embarrassing compared
to something more thought out:

Code:
``` int checkwidthagain;
int checkheightagain;
checkwidthagain = 0;
checkheightagain = 0;

if (oldx > 320 && oldy > 568) { // image is taller and wider than the screen
if (oldx-320 < oldy-568) {
newx = oldx / oldy * 568; // get new width
newy = 568.0;
checkwidthagain = 1;
posx = centerx - (newx/2); // get new x position
posy = centery - (newy/2);
} else {
newy = oldy / oldx * 320; // get new height
newx = 320.0;
checkheightagain = 1;
posy = centery - (newy/2); // get new y position
posx = centerx - (newx/2);
}
}

if (checkwidthagain == 1 && newx > 320.0) {
newy = oldy / oldx * 320; // get new height
newx = 320.0;
posy = centery - (newy/2);
posx = centerx - (newx/2);
} // checkwidthagain

if (checkheightagain == 1 && newy > 568.0) {
newx = oldx / oldy * 568; // get new width
newy = 568.0;
posx = centerx - (newx/2);
posy = centery - (newy/2);
} // checkheightagain
```
Solves any situation where both image dimensions are larger than both screen dimensions.
I'll cheat now, since this has to happen in real time adjusting size.

10. Originally Posted by xArt
Is that faster? The aspect ratio formula is correct,
Just the logic I'm applying to use it.
It can be faster, in that it allows you to use entirely integer math and not involve floating point at all. Like this:
Code:
`newx = 568 * oldx / oldy;`
Otherwise if you try and use integer math, you get a significantly lower result than you want.

Using floating point math leaves you with a likely result that the answer is off by 0.00001 or something, which then rounds down when you poke that value into an int.

11. Thanks, I will take that on board, however, screen coords are floats for iOS quartz2D
The screen resolution is twice the coordinate dimensions (for current devices).
ie. you draw a circle the old fashioned way, and the gfx chip takes care of anti-aliasing.
or, you can draw a line from x0.5 y1.0 to x10.5 y11.0 if you like

I only copy the values to ints to display the resolution to the user,
but it's nice if they can be a little more accurate.