the center of a polygon
given n coordinates, how do you calculate the center of the polygon they compose?
at first i thought you just sum up the X's, sum up the Y's, and divide by n. but that's wrong, because if i have 999 coordinates at (0,0), and one at (1,1), the center would be at (0.0001,0.0001),and not at (0.5,0.5), as i would have liked it to be...
help, please?
thanks, noam
find the extents (minimum/maximum in each axis), then average the min/max.
eg, centerX = (maxX + minX) / 2.0f
eg, centerX = (maxX + minX) / 2.0f
Quote:Original post by Anonymous Poster
find the extents (minimum/maximum in each axis), then average the min/max.
eg, centerX = (maxX + minX) / 2.0f
That is not correct.
The only way to do this is to split the polygon into trinagles. Then you do something like this:
center = Vector(0,0)area = 0for triangle in triangles: center += tirangle.getArea()*triangle.getCenter() area += tirangle.getArea()center /= area
Considering there are about a million different ways you can define the "center of the polygon", you'll need to be more precise about what kind of center you want. In-center, ortho-center, circum-center, center of mass, etc.
Quote:Original post by No_Germs
the first way, though simple, works finr for me :)
The method the AP posted finds the center of the axis aligned bounding box of the polygon not the center of the polygon.
yes, i knew it, but i gave it a chance, and it works fine for what i need, so i'll settle for that... :)
Quote:Original post by No_Germs
given n coordinates, how do you calculate the center of the polygon they compose?
at first i thought you just sum up the X's, sum up the Y's, and divide by n. but that's wrong, because if i have 999 coordinates at (0,0), and one at (1,1), the center would be at (0.0001,0.0001),and not at (0.5,0.5), as i would have liked it to be...
help, please?
thanks, noam
Your "example" of a possible polygon doesn't really work. The vertices of a polygon must be unique; if they aren't, it violates the definition of a polygon: "a closed plane figure with n sides and n vertices".
Quote:Original post by No_Germs
yes, i knew it, but i gave it a chance, and it works fine for what i need, so i'll settle for that... :)
Are you sure? Consider this "boomerang" polygon, where the vertices are marked with *'s. The bounding box is enclosed in #'s. The "x" marks the center of the bounding box. But notice that x isn't even inside the polygon!
########################## ## *-------------------* ## ----- | ## ----- | ## ----* | ## | | ## x | | ## | | ## | | ## | | ## | | ## * ## ##########################
The center of a polygon is not necessarily in the polygon. It really all depends what you mean by "center". "Center" is not really a mathematically precise term... it has lots and lots and lots of interpretations. In the case of the geometric centroid (the most common interpretation for this type of figure), the center of mass of a boomerang is indeed outside the boomerang itself. What really matters here is how you intend to USE the "center point".
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement