How to find the Center Coordinate of Rectangle? [c

2019-01-16 15:04发布

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.

3条回答
不美不萌又怎样
2楼-- · 2019-01-16 15:08

We can calculate using mid point of line formula,

centre (x,y) =  new Point((boundRect.tl().x+boundRect.br().x)/2,(boundRect.tl().y+boundRect.br().y)/2)
查看更多
Anthone
3楼-- · 2019-01-16 15:22
The center of rectangle is the mid point 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.

查看更多
叼着烟拽天下
4楼-- · 2019-01-16 15:22

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.

查看更多
登录 后发表回答