MATLAB Answers

Better “centerpoint” than centroid of a concave polygon

33 views (last 30 days)
David Franco
David Franco on 18 Apr 2020
Commented: David Franco on 21 Apr 2020
I'm using the centroid of polygons to attach a marker in a map application. This works definitely fine for convex polygons and quite good for many concave polygons.
However, some polygons (banana, donut) obviously don't produce the desired result: The centroid is in these cases outside the polygons area.
Does anybody know a better approach to find a suitable point within any polygons area (which may contain holes!) to attach a marker?
Thank you!

  10 Comments

Show 7 older comments
David Franco
David Franco on 21 Apr 2020
The file has 13MB. The maximum size allowed here is 5 MB.

Sign in to comment.

Accepted Answer

darova
darova on 20 Apr 2020
Here is the simplest solution for this task
x = rand(4,1);
y = rand(4,1);
x = [x; x(1)];
y = [y; y(1)];
ii = 2; % corner to place point
n = 3; % how far inside from corner
x0 = (x(ii-1)+2*n*x(ii)+x(ii+1))/(n+1)/2;
y0 = (y(ii-1)+2*n*y(ii)+y(ii+1))/(n+1)/2;
if ~inpolygon(x0,y0,x,y)
x0 = x(ii) - (x0-x(ii));
y0 = y(ii) - (y0-y(ii));
end
plot([x(ii) x0],[y(ii) y0],'o-r')
line(x,y)
axis equal

  0 Comments

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 18 Apr 2020
Well if you compute the distance transform of the shapes you'll get a "spine" that runs along the midline of the shape. I don't know if any point along there is better than any other point. Maybe you can just pick the point half way from one end to the other, if there even ARE endpoints. The only way I know how to do it is with digital images, not analytically with a set of (x,y) vertex points. And it requires the Image Processing Toolbox so you can use bwskel() or bwdist().

  1 Comment

David Franco
David Franco on 19 Apr 2020
Thanks, but I am working with shapefiles and I need to find the skeleton of a polygon...

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!