Public Group

# Centering an image and other formulas

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

## Recommended Posts

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?

##### Share on other sites
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.

##### Share on other sites
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...

##### Share on other sites
ahhh i got it

( SCREEN_WIDTH - IMAGE_WIDTH ) / 2;

##### Share on other sites
rect.x = (SCREEN_WIDTH / 2) - (rectWidth/2);
rect.y = (SCREEN_HEIGHT / 2 - (rectHeight/2);

##### Share on other sites
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.

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 14
• 30
• 13
• 11
• 11
• ### Forum Statistics

• Total Topics
631782
• Total Posts
3002325
×