Jump to content
  • Advertisement
Sign in to follow this  
Wizumwalt

how to compute centroid?

This topic is 4809 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

I'm trying to center an object in the middle of the screen (single object model in orthographic mode). I read in some points from a cad like file to render an object on screen and when I rotate it, the center of rotation is around the center of one of the far ends of the box so it rotates funny and not from the direct center of the box (this model is like a long rectangle). Someone then told me I had to compute the centroid of the model by averaging the positions of the points, and then translate that point to the origin. Can anyone tell me how computing this is done? I was thinking like ... as I read coordinates in from my file, I take the largest and smallest X, largest and smallest Y, and largest and smallest of Z, avg those and ... this is where I get lost, it sounds like I'd have to change all of the thousands of coordinates of the nodes which I use to draw all my meshing and move them to push the model in the center. Any help much appreciated. (sorry, I placed this in the wrong thread earlier)

Share this post


Link to post
Share on other sites
Advertisement
Well your basically right, firstly you need to calculate the centroid. One way to do this is, as you say, by finding the mean. Add up all the x co-ordinates for your points and divide by the number of points, then do the same for y.

There are other (sometimes better) approaches to centroid calculation, but the average approach should so for you situation. Be aware however, that if you have a concave shape it is possible for the center to end up outside the shape.

However, the main thing you get wrong is you shouldn't need to modify your mesh! In fact you shouldn't need to modify it when doing the rotation either. Assuming centreX and centreY hold your centroid position, you can do something like:

drawPoint(posX + centreX, posY + centreY);

note that this doesn't actually change the coordimnates of you model, just draws it in a different place. If you actually modify the co-ordinates you'll get in trouble when to want to do many transformations in a row, and then go back to the start for example.

How are you doing the drawing? If it's with something like OpenGL there are functions available for moving objects around.

Hope that helps.

Share this post


Link to post
Share on other sites
Quote:
Original post by Raymond_Porter420
the average of the points is only the centroid if the shape is convex. google will help you with the concave because i dont remember how to do it :P.


Where'd you get that from? The average of the points is always the centroid of a shape (it's how it's defined). There can be shortcuts if your object is made of composite shapes with known centroids (in that case you can take a weighted average of their centroids with respect to volume).

Share this post


Link to post
Share on other sites
i got that the avg of the points isnt the centroid from this site. Look under
"Centroid"

http://www.geog.ubc.ca/courses/klink/gis.notes/ncgia/u33.html
the website for
The National Center for Geographic Information Analysis

anyways id like to know now, because im curious what it really is.

Share this post


Link to post
Share on other sites
So that's what you meant. I was refering to an average of all of the points within a polygon/shape (or a suitable approximation thereof). If I'm not mistaken, the vertices of a polygon can be averaged to find the centroid in the case of a triangle however.

Share this post


Link to post
Share on other sites
Yea i see what i did wrong. I was thinking a square with 4 vertices at each corner. now place some vertices along an edge and resum the points and avg. them out and the centroid will be weighted towards that edge. So.... The algorithim(or one of them ) is to break the shape into triangles because 3 vertices cannot possibly have influencing vertices that dont also determine the area of the triangle ( unless the 3 points are the same point or a line ) and find the centroid of that triangle. Then sum and avg the centroids of the triangle parts to find the centroid of the mesh. I only missed 1 thing. The mesh is probably allready triangles !!! I was also wrong about the convexity part :(

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!