A: 

I don't have any code for this, but I have seen where the shape was tesselated (in this case using polygon triangulation) so you would have a nice set of triangles. Then compute the combined centroid based on the weighted average of the triangles' centroid.

EDIT:

There is a blog by the guys working on a product called Insight3D by AGI. In this entry, they talk about triangulation. It might help you do it since they do give some more pointers on algorithms. Depending on your use, you might be able to reuse one of their implementations. It is free for development and non-commercial use.

Erich Mirabal
I've been researching this for a couple hours, and it looks like tesselation is the way to go. But I still need a good algorithm for performing the tesselation. I haven't been able to find anything explicit yet, code OR math.
Giffyguy
+2  A: 

Tesselation or discretization of arbitrary 2D planar shapes is a common problem in finite element analysis. It's commonly done with planar triangles or quadrilaterals. Try a Google search on "2d finite element mesh generation" or quadtree or octree mesh generation. You can calculate the centroid of each simple shape and apply the (correct) formula that you cited.

Something like this. Or these. You'd have to supply the raw geometry for the body in question, of course.

You've a long row to hoe yet. You'll have to do all of the following:

  1. Find an auto meshing program and learn how to input the geometry for your 2D shape.
  2. Run the auto mesher and get a mesh output which will consist of all the 2D points in space and the connectivities of all the triangular and quadrilateral elements.
  3. Write a program to read in the mesh and calculate the area and centroid of each element.
  4. Plug those values into the formula you cited to calculate the centroid of your original 2D shape. This means looping over all the elements and accumulating the areas an the products of the (x,y) coordinates of each element centroid and its area.
  5. Once you have an answer, you need to check convergence. You do this by refining your mesh by making the elements smaller and recalculating. You know you've converged when you refine the mesh and the answers change by less than a small tolerance (5% or whatever you're willing to tolerate).

It's still a fair amount of work.

UPDATE: This one looks quite good, and it's open source.

duffymo
Interesting... I'll have to try these out and see what they can do.
Giffyguy
This looks like an aweome solution, and it's basically the road I want to take - but what I really want is to figure out how to calculate the mesh myself. Do you know where I can find this algorithm anywhere?
Giffyguy
Google for "finite element automatic mesh triangle" and pick the most detailed one you can find. Could take a while to code - it won't be trivial. You might want to just use something that exists: http://www-users.informatik.rwth-aachen.de/~roberts/software.html
duffymo