# Better “centerpoint” than centroid of a concave polygon

35 visualizaciones (últimos 30 días)
David Franco el 18 de Abr. de 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 comentariosMostrar 8 comentarios más antiguosOcultar 8 comentarios más antiguos
Image Analyst el 21 de Abr. de 2020
Can you trim it down to a subset of shapes?
David Franco el 21 de Abr. de 2020
Done!

Iniciar sesión para comentar.

darova el 20 de Abr. de 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 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

### Más respuestas (3)

Chad Greene el 5 de Oct. de 2021
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);
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

Image Analyst el 18 de Abr. de 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 comentarioMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos
David Franco el 19 de Abr. de 2020
Thanks, but I am working with shapefiles and I need to find the skeleton of a polygon...

Iniciar sesión para comentar.

Chad Greene el 8 de Oct. de 2021
I added a polycenter function to do exactly what you need in the Climate Data Toolbox for Matlab.
If you have a shapefile, the sytax is:
[xc,yc] = polycenter(S);
Then xc,yc are the centerpoints of any entries in the shapefile structure S.
##### 3 comentariosMostrar 1 comentario más antiguoOcultar 1 comentario más antiguo
Sim el 1 de Mzo. de 2022
However, I got the following error:
>> [xc,yc] = polycenter(S);
Reference to non-existent field 'Lon'.
Error in polycenter (line 116)
x = S(k).Lon;
Any idea on how to solve it ?
Chad Greene el 1 de Mzo. de 2022
@Sim Check the field names of the shapefile and then use x,y coordinates as inputs, like this
[xc,yc] = polycenter(x,y);
which would be the same as
[lonc,latc] = polycenter(lon,lat);
if your input coordinates are geo.

Iniciar sesión para comentar.

### Categorías

Más información sobre Elementary Polygons en Help Center y File Exchange.

R2019b

### Community Treasure Hunt

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

Start Hunting!

Translated by