MATLAB Answers

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

9 views (last 30 days)
David Franco
David Franco on 11 Mar 2020
Commented: David Franco on 11 Mar 2020
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 Comments

Matt J
Matt J on 11 Mar 2020
Where would you consider the "center" of the above shape to be? Could you mark it on your posted figure?
David Franco
David Franco on 11 Mar 2020
In fact Matt, anywhere within the limits is already viable for me.

Sign in to comment.

Accepted Answer

Matt J
Matt J on 11 Mar 2020
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 Comments

Show 2 older comments
David Franco
David Franco on 11 Mar 2020
Very clever option. It's working.
Any ideas on how to set the reduction parameter as a percentage of the polyshape?
Matt J
Matt J on 11 Mar 2020
Perhaps as follows,
>> city=polyshape([0 0; 0 1; 1 0]);
>> A=area(city);
>> p = .8; %percentage
>> [d,res]=fzero(@(d)area(polybuffer(city,d))-p*A, 0)
d =
-0.0309
res =
8.0499e-09
>> innercity=polybuffer(city,d);

Sign in to comment.

More Answers (0)

Sign in to answer this question.

Products


Release

R2019b

Translated by