File Exchange

image thumbnail

Centroid of a Convex n-Dimensional Polyhedron

version 1.0.0.0 (883 Bytes) by Michael Kleder
Computes the center of gravity of a convex polyhedron in any number of dimensions.

10 Downloads

Updated 21 Sep 2005

No License

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.

Notes:
(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

Cite As

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 .

Comments and Ratings (11)

Tony

John

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.

Raghuram

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.

Chris Godau

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:
centroid(P(unique(convhulln(P)),:))

Charlotte B

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?

The Author

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],...
'b-',P(1),P(2),'r.');

Roja B

Hi,

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?

The Author

Answer to Fady Dawoud: If the points do not form a convex polyhedron, then the function gives you an error telling you what happened.

fady dawoud

very nice!! a question though, does the algorithm still work if the points dont form a convex surface.

Glen Lichtwark

This is a very handy function, thanx.

Birajashis Pattnaik

Ohh this is fantastic.

MATLAB Release Compatibility
Created with R14SP2
Compatible with any release
Platform Compatibility
Windows macOS Linux