This is a similar question to this one here.
Given a list of 3D coordinates that define the surface( Point3D1
, Point3D2
, Point3D3
, and so on), how to calculate the centroid of the surface?
In 2D the computation is given by the following formula:
What about the 3D analogue?
If it's a planar surface, you can transform to a coordinate system local to the plane, calculate the centroid using the formulas you presented, and then transform back to get its coordinates in 3D space.
Just use the equations that you have twice, but the second time swap in z for y.
That is, calculate the centroids of the two projections, one onto the x-y plane, and the other onto the x-z plane. The centroids of the projections will be projections of the actual centroid, so the answer will be the x, y, and z values you find from these two calculations.
Stated more explicitly: If your points are (x1, y1, z1), (x2, y2, z2),... , to get the x-y centroid, (Cx, Cy), do a calculation using (x1, y1), (x2, y2),... and to get the x-z centroid, (Cx, Cz) use the points (x1, z1), (x2, z2),.... -- just do the second calculation with your same 2D formula, treating the z values as the y in the equation. Then your 3D centroid will be (Cx, Cy, Cz). This will work as long as your surface is flat and isn't parallel to the x-y, x-z, or y-z planes (but if it is parallel it's just the 2D equation).
Let the points be v0, v1, ..., vN in counterclockwise, where vi = (xi, yi, zi).
Then the triplets (v0, v1, v2), (v0, v2, v3), ..., (v0, vi, vi+1), ..., (v0, vN-1, vN) forms N-1 triangles that create the polygon.
The area of each triangle is | (vi − v0) × (vi+1 − v0) | ÷ 2, where × is the cross product and | · | is vector length.
You may need to make the area negative to compensate for concave parts. A simple check is to compute (vi − v0) × (vi+1 − v0) · (v1 − v0) × (v2 − v0). The area should have the same sign as the result.
Since ratio of area of 2D figures is constant under parallel projection, you may want to choose a unit vector (e.g. z) not parallel to the plane, the treat (vi − v0) × (vi+1 − v0) · z as the area. With this, you don't need to perform the expensive square root, and the sign check is automatically taken care of.
The centroid of each triangle is (v0 + vi + vi+1) ÷ 3.
Hence, the centroid of the whole polygon is, assuming uniform density,
1 N-1
centroid = —————————— ∑ ( centroid-of-triangle-i × area-of-triangle-i )
total-area i=1
(For dimensions ≥ 4D, the area needs to be computed with Ai = ½ |vi−v0| |vi+1−v0| sin θi, where cos θi = (vi−v0) · (vi+1−v0). )