Centering an image and other formulas

Started by
4 comments, last by Tenac 15 years, 11 months ago
What is the formula for centering an image? Or in other words, what is the formula to get the right x,y values to center an image (in pixels)? What are some other useful 2d formulas?
Advertisement
Some questions... where is (0, 0) on the image, bottom-left, top-left, etc?
Do you have the width and height of the texture?

If so, just think about it, maybe draw a little picture on paper, that definitely helps visualize it.
Of course 0,0 is the top most left. I tried something like this which I believe is the formula:

rect.x = SCREEN_WIDTH / 2;
rect.y = SCREEN_HEIGHT / 2;

However, this does not center the image...
ahhh i got it

( SCREEN_WIDTH - IMAGE_WIDTH ) / 2;
rect.x = (SCREEN_WIDTH / 2) - (rectWidth/2);
rect.y = (SCREEN_HEIGHT / 2 - (rectHeight/2);
this?
rect.x = SCREEN_WIDTH / 2 - (rect.width / 2);
rect.y = SCREEN_HEIGHT / 2 - (rect.height / 2);

EDIT: nvm, you got it and someone posted before me lol. You just didn't take into account the width and height of the image.

This topic is closed to new replies.

Advertisement