Jump to content
  • Advertisement
Sign in to follow this  
No_Germs

the center of a polygon

This topic is 4817 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
find the extents (minimum/maximum in each axis), then average the min/max.
eg, centerX = (maxX + minX) / 2.0f

Share this post


Link to post
Share on other sites
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 = 0
for triangle in triangles:
center += tirangle.getArea()*triangle.getCenter()
area += tirangle.getArea()
center /= area

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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".

Share this post


Link to post
Share on other sites
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 | | #
# | | #
# | | #
# | | #
# | | #
# * #
# #
#########################

Share this post


Link to post
Share on other sites
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".

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!