Compute the centroids associated with a multiregion polygon.
Create a polygon containing two solid regions, and then compute its centroid.
x1 = [0 1 2];
y1 = [0 1 0];
x2 = [2 3 4];
y2 = [1 2 1];
polyin = polyshape({x1,x2},{y1,y2});
[x,y] = centroid(polyin);
plot(polyin)
hold on
plot(x,y,'r*')
hold off
To compute the centroid of each region separately, specify the boundary indices of each region in the second argument.
[x,y] = centroid(polyin,[1 2]);
plot(polyin)
hold on
plot(x(1),y(1),'r*',x(2),y(2),'r*')
hold off
Alternatively, you can compute the centroid of each region by first separating the regions into separate polyshape objects. Use the regions function to create an array of polyshape objects, where each element of the array is a polyshape defining one of the triangular regions.
polyarray = regions(polyin)
polyarray =
2×1 polyshape array with properties:
Vertices
NumRegions
NumHoles
x-coordinate of the centroid of a
polyshape, returned as a scalar, vector, matrix, or
multidimensional array. If the input is an array of
polyshape objects, then x contains
the x-coordinates of the centroid of each
polyshape.
y-coordinate of the centroid of a
polyshape, returned as a scalar, vector, matrix, or
multidimensional array. If the input is an array of
polyshape objects, then y contains
the y-coordinates of the centroid of each
polyshape.
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window.
Web browsers do not support MATLAB commands.
Sélectionner un site web
Choisissez un site web pour accéder au contenu traduit dans votre langue (lorsqu'il est disponible) et voir les événements et les offres locales. D’après votre position, nous vous recommandons de sélectionner la région suivante : .
Vous pouvez également sélectionner un site web dans la liste suivante :
Comment optimiser les performances du site
Pour optimiser les performances du site, sélectionnez la région Chine (en chinois ou en anglais). Les sites de MathWorks pour les autres pays ne sont pas optimisés pour les visites provenant de votre région.