# How to find a figure's centroid inside a polyshape?

18 ビュー (過去 30 日間)
David Franco 2020 年 3 月 11 日
コメント済み: David Franco 2021 年 10 月 10 日
I have the following problem:
I am plotting the boundaries of several municipalities and I need to list each one.
As there are many municipalities, I created a script that does this numbering automatically.
I used the centroid function to find a better position automatically, but some municipalities, like the one shown below, have a concave shape and the centroi is outside the limits of the municipalities.
My question is: there is a automatic way to find a position as central as possible within the city limits?
This is the result I get when I use the centroid of each polyshape to put the number. But for some municipalities with irregular shapes the numbers may be outside the limits (not well centralized):
Ps.: The boundaries of the municipalities are polyshapes and I have all the information regarding the location of the borders (I attached the polyshape for this example).
##### 2 件のコメント表示非表示 1 件の古いコメント
David Franco 2020 年 3 月 11 日
In fact Matt, anywhere within the limits is already viable for me.

サインインしてコメントする。

### 採用された回答

Matt J 2020 年 3 月 11 日
In fact Matt, anywhere within the limits is already viable for me.
If so, you could use nearestvertex command to project the centroid to the nearest point within city limits,
##### 5 件のコメント表示非表示 4 件の古いコメント
David Franco 2020 年 3 月 11 日
Perfect! Thank you!

サインインしてコメントする。

### その他の回答 (1 件)

Chad Greene 2021 年 10 月 5 日
I just ran into this problem when trying to place a text label in the middle of a crescent-shaped ice shelf. The centroid or the mean or median of the coordinates of the ice shelf polygon are all outside the bounds of the ice shelf. Here's the best solution I could come up with:
% Convert the outline to a polyshape:
P = polyshape(x,y);
% And get the delaunay triangulation of the polygon:
T = triangulation(P);
% Now find the center points of all the triangles:
[C,r] = circumcenter(T);
% Get the index of the centerpoint that has the largest radius from the boundary:
[~,ind] = max(r);
% These center coordinates are in the center of the fattest part of the polygon:
xc = C(ind,1);
yc = C(ind,2);
##### 5 件のコメント表示非表示 4 件の古いコメント
David Franco 2021 年 10 月 10 日

サインインしてコメントする。

R2019b

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by