USAGE: C = centroid(P)
P = Matrix of convex polyhedron vertices: each row is a vertex, and each column is a dimension.
C = Row vector of centroid coordinates. Each column is a dimension.
(1) This function computes the centroid by partitioning into simplices and determining the weighted sum of their centroids.
(2) Written in response to a question on comp.soft-sys.matlab
Michael Kleder, Sep 2005
Michael Kleder (2020). Centroid of a Convex n-Dimensional Polyhedron (https://www.mathworks.com/matlabcentral/fileexchange/8514-centroid-of-a-convex-n-dimensional-polyhedron), MATLAB Central File Exchange. Retrieved .
I don't understand why centroid.m generates a qhullmx error saying the hull is narrow. Here's my vertex set:
[.5 .5 .5;
0 0 0;
.5 0 0;
0 0 .5;
.1667 -.1667 .1667]
It finds centroids for the simplices with vertices at points 1 thru 4, and at points 2 thru 5. Also, points 1 and 5 are clearly on opposite sides of the x-z plane, so there is no overlap between the symplices.
For the following p = [0 0; 1 1; 0.6667 0.6667; 0.5 0.8333; 0.1667 0.5] it gives an error saying the polygon is not convex when in fact it is.
Very nice, thank you!
Was just about to write this myself when I found yours.
If your set of points is not convex you can of course use the convex hull:
thanks for this function! but what happens with a concave polyhedron? do you have suggestions on how to calculate the center of gravity of these shapes?
Answer to Roja B:
As an example in 2-D, try
P=centroid([0 0;1 1;2 0]);
plot([0 1 2 0],[0 1 0 0],...
thank you, this is a very useful function. my only problem is when I try to use it in 2-D. how do you think I can fix this?
Answer to Fady Dawoud: If the points do not form a convex polyhedron, then the function gives you an error telling you what happened.
very nice!! a question though, does the algorithm still work if the points dont form a convex surface.
This is a very handy function, thanx.
Ohh this is fantastic.