Ask Your Question

Finding area center of rectangle

asked 2013-06-12 14:25:34 -0500

Papercut gravatar image

updated 2013-06-12 15:23:26 -0500

I am trying to find an area center of various types of rectagles.

(Center of gravity and midpoint of 4 vertices never work so please think in different way)

Please see this image:

image description I have to find the position of position of red dots

Point vertices[4];
Point areaCenter;

I have to find areaCenter so (nearly)

area(areaCenter, vertices[0], vertices[1]) = area(areaCenter, vertices[1], vertices[2]) =

area(areaCenter, vertices[2], vertices[3]) = area(areaCenter, vertices[3], vertices[0])

I tried many different ways to find the mid point but none of them covered every types of rectangles.

Can any1 give me some idea?

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2013-06-12 14:56:09 -0500

unxnut gravatar image

updated 2013-06-12 17:09:36 -0500

You need to add the positions of non-zero pixels in horizontal (vertical) directions and divide them by the number of nonzero pixels to get the center of gravity.

xsum = 0;
ysum = 0;
num_nonzero_pxl = 0;
for each pixel (p(x,y))
    if pixel p(x,y) is non-zero {
        xsum += x;
        ysum += y;
edit flag offensive delete link more


Thanks for the answer. But I don't quite understand what you are trying to say. Can you write some lines of pseudocode please?

Papercut gravatar imagePapercut ( 2013-06-12 15:15:30 -0500 )edit

that's not gonna work. Equally shaped rectangle may have different midpoint depending over its rotation.

Papercut gravatar imagePapercut ( 2013-06-12 15:55:02 -0500 )edit

It should. Remember, you are adding x and y coordinates (the locations) if the pixel is nonzero and not the actual value of pixel.

unxnut gravatar imageunxnut ( 2013-06-12 15:57:59 -0500 )edit

Shouldn't it be cgix=xsum/total_num_nonzero; cgiy=ysum/total_num_nonzero; ?

Papercut gravatar imagePapercut ( 2013-06-12 16:06:57 -0500 )edit

And I am pretty sure the midpoint will be placed outside of rectangle in the rectangle #6 in the image as well as it takes too long.

Papercut gravatar imagePapercut ( 2013-06-12 16:09:31 -0500 )edit

Yes, you are right about the denominator. It should be total_num_nonzero. My bad. Fixed the answer above.

unxnut gravatar imageunxnut ( 2013-06-12 17:05:22 -0500 )edit

answered 2013-06-12 17:27:31 -0500

Hansg91 gravatar image

Perhaps this is what you are looking for :

edit flag offensive delete link more


Thanks for the link. I have implemented the Centroid of polygon in your link and even posted my code in the page. However it also has chance to be placed outside of polygon.. :(

Papercut gravatar imagePapercut ( 2013-06-13 12:30:48 -0500 )edit
Login/Signup to Answer

Question Tools


Asked: 2013-06-12 14:25:34 -0500

Seen: 2,831 times

Last updated: Jun 12 '13