# Retaining aspect ratio when resizing images

This topic is 3881 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

This probably belongs in the "For beginners" forum, as it probaly has a rather obvious answer, however i cannot seem to get this right, so i am calling for help at GameDev :p If a image is larger than i allow, i would want to resize the image (setting the axis that is too big to the maximum for that axis) and calculate the new width/height for the second axis while still retaining the aspect ratio between the width and height. Here is the code i am using. 500 is the maximum width, and 600 is the maximum height that i am allowing.
[source langg = "cpp"]
if(TextureImage[0]->sizeX > 500 && TextureImage[0]->sizeX >= TextureImage[0]->sizeY )
{
double newheight= 500/TextureImage[0]->sizeX * TextureImage[0]->sizeY ;
vector[0].Set(500,newheight);
}
else if(TextureImage[0]->sizeY > 600 && TextureImage[0]->sizeY >= TextureImage[0]->sizeX)
{
double newwidth= 600/TextureImage[0]->sizeY * TextureImage[0]->sizeX ;
vector[0].Set(newwidth,600);
}
else
{
vector[0].Set(TextureImage[0]->sizeX, TextureImage[0]->sizeY);
}


the vector array simply holds the width and height for the individual images.

##### Share on other sites
So what's your problem? Runtime error? compiler error? Integer truncation when doing division? Computer turning pink and emitting sound waves just beyond human hearing range?

##### Share on other sites
Well, the problem is nothing is being drawn, when either the image's width is bigger 500 or the images height is greater than 600.
I highly doubt it is related, but i am drawing the following way:

[source langg = "cpp"]glBindTexture(GL_TEXTURE_2D, texture[0]);	glBegin(GL_QUADS);							glTexCoord2f(0.0f, 0.0f); glVertex3f(WIDTH/2 - vector[0].width/2, vector[0].height,  0);	// Bottom Left Of The Texture and Quad		glTexCoord2f(1.0f, 0.0f); glVertex3f( WIDTH/2 + vector[0].width/2, vector[0].height,  0);	// Bottom Right Of The Texture and Quad		glTexCoord2f(1.0f, 1.0f); glVertex3f( WIDTH/2 + vector[0].width/2,  0,  0);	// Top Right Of The Texture and Quad		glTexCoord2f(0.0f, 1.0f); glVertex3f(WIDTH/2 - vector[0].width/2,  0,  0);	  //top left of the texture and quad    glEnd();		return TRUE;

I can draw just fine if i call this function:
vector[0].Set(TextureImage[0]->sizeX, TextureImage[0]->sizeY);
But then i am not resizing, So my problem isn't related to drawing to the screen. It is the 'if' sentences above, that there is something wrong with.

##### Share on other sites
Then the problem is probably integer truncation while doing division. Assuming that your image sizes are specified with integer types, then lines like:
double newheight= 500/TextureImage[0]->sizeX * TextureImage[0]->sizeY ;

Divide an integer value 500, by another integer value TextureImage[0]->sizeX, which is strictly greater than 500, which results in a value below 1, which truncates to 0. So the multiplication by sizeY turns into a 0. Cast one or both of the division arguments to a floating point type before doing the division.

##### Share on other sites
Thanks mate! :) it is working now!

1. 1
2. 2
3. 3
4. 4
frob
15
5. 5

• 14
• 12
• 20
• 12
• 13
• ### Forum Statistics

• Total Topics
632152
• Total Posts
3004470

×