First time here? Check out the FAQ!

Ask Your Question
2

Finding area center of rectangle

asked Jun 12 '13

Papercut gravatar image

updated Jun 12 '13

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?

Preview: (hide)

2 answers

Sort by » oldest newest most voted
1

answered Jun 12 '13

unxnut gravatar image

updated Jun 12 '13

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;
        num_nonzero_pxl++;
    }
cgix=xsum/num_nonzero_pxl;
cgiy=ysum/num_nonzero_pxl;
Preview: (hide)

Comments

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 (Jun 12 '13)edit

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

Papercut gravatar imagePapercut (Jun 12 '13)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 (Jun 12 '13)edit

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

Papercut gravatar imagePapercut (Jun 12 '13)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 (Jun 12 '13)edit

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

unxnut gravatar imageunxnut (Jun 12 '13)edit
0

answered Jun 12 '13

Hansg91 gravatar image

Perhaps this is what you are looking for :

http://en.wikipedia.org/wiki/Centroid#Centroid_of_polygon

Preview: (hide)

Comments

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 (Jun 13 '13)edit

Question Tools

Stats

Asked: Jun 12 '13

Seen: 3,743 times

Last updated: Jun 12 '13