centroid

Center of mass of a polyshape, treated as a uniform plate.

Category: Mathematics > Computational Geometry

Syntax

[cx, cy] = centroid(pshape)

Returns the centroid coordinates of the whole shape.

[cx, cy] = centroid(pshape, I)

Centroid of only the regions selected by the index I.

Description

The centroid is the area-weighted average position of the shape. Holes count as missing material, so a hole shifts the centroid away from itself. The centroid of a non-convex shape can fall outside the shape.

It is computed exactly from the polygon boundaries, not from a sampled grid.

Input arguments

pshape
A polyshape object.

Output arguments

cx, cy
x and y coordinates of the centroid. For an empty polyshape both are NaN.

Example

Centroid of a rectangle

P = polyshape([0 4 4 0], [0 0 3 3]);
[cx, cy] = centroid(P)
Output
cx = 2
cy = 1.5000

See also

polyshape area boundingbox polyarea