Page 1 of 1

[Solved] Center of mass of an arbitrary 2D object

Posted: Tue May 11, 2010 2:11 pm
by milan1612
So I have a list of 2D points forming an arbitrary object. What I want to do now is to calculate
the center of mass of this object. Is there a general algorithm for it?

Code: Select all

Structure Vector2D
  x.f
  y.f
EndStructure

Global NewList Edges.Vector2D() ; e.g. (2; 3), (3; 5), (4; 3)

Procedure CenterOfMass(*Result.Vector2D)
  ;...
EndProcedure
I guess what needs to be done is to let every perpendicular intersect, but I'm not sure...


EDIT:
OK, I found these equations on Wikipedia:
Image

Image

Image

Where A is the surface of the polygon, and Cx & Cy are the coordinates of the centre of mass.
Is this the correct way?

EDIT2:
I found yet another, simpler, way:
Image
Where n is the number of points I have...

Re: Calculating the center of mass of an arbitrary 2D object

Posted: Tue May 11, 2010 2:29 pm
by milan1612
Answering my own question, here's a solution that seems to work:

Code: Select all

Procedure CenterOfMass(*Result.Vector2D, List Edges.Vector2D())
  Protected x.f, y.f
  
  ForEach Edges()
    x + Edges()\x
    y + Edges()\y
  Next

  *Result\x = x * (1 / ListSize(Edges()))
  *Result\y = y * (1 / ListSize(Edges()))
EndProcedure
It implements the formula I posted in Edit2 above...


I guess this thread can be closed :lol: