centroid
Centroid of polyshape
Description
Examples
Centroid of Polygon
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 = 2x1 polyshape array with properties: Vertices NumRegions NumHoles
[x,y] = centroid(polyarray)
x = 2×1
1
3
y = 2×1
0.3333
1.3333
Input Arguments
polyin
— Input polyshape
scalar | vector | matrix | multidimensional array
Input polyshape
, specified as a scalar, vector, matrix,
or multidimensional array.
Data Types: polyshape
I
— Boundary index
scalar integer | vector of integers
Boundary index, specified as a scalar integer or vector of integers. Each
element of I
corresponds to a single boundary of the
input polyshape
.
Data Types: double
| single
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
Output Arguments
x
— x-coordinate
scalar | vector | matrix | multidimensional array
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
— y-coordinate
scalar | vector | matrix | multidimensional array
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
.
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
Dynamic memory allocation must be enabled for code generation.
Name-value pair must be compile time constant.
Version History
Introduced in R2017b
MATLAB Command
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.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)