How to find the Center Coordinate of Rectangle?

35

I have drawn a rectangle. I know its (x1,y1) Top Left and (x2,y2) Bottom Right coordinates.. I also have the height h and width w of drawn rectangle.. How can I find the center coordinates (x,y) ?

I am currently using the following formula.

(x,y) = (x2 + x1)/2, (y2+y1)/2

It gives the correct y coordinate but no luck in x.

This question is tagged with math geometry formula

~ Asked on 2012-03-16 09:36:59

The Best Answer is


63

The center of rectangle is the midpoint of the diagonal end points of rectangle.

Here the midpoint is ( (x1 + x2) / 2, (y1 + y2) / 2 ).

That means:
xCenter = (x1 + x2) / 2
yCenter = (y1 + y2) / 2

Let me know your code.

~ Answered on 2012-03-16 10:12:50


25

Center x = x + 1/2 of width

Center y = y + 1/2 of height 

If you know the width and height already then you only need one set of coordinates.

~ Answered on 2013-09-21 11:29:18


Most Viewed Questions: