How do you order the vertices of a non-convex polygon?

17 visualizzazioni (ultimi 30 giorni)
I have a polygon defined by a logical cellular grid, 'polygon', and the cells that have an edge or corner touching the polygon, 'edges'. I need the vertices of the perimeter ('edges') in order going around the polygon, either cw or ccw. The way I find them now does not put them in order.
I've tried to use bwboundaries & bwtraceboundary. I attached the closest that I've come up with, but you'll see that a couple of vertices are missing.

Risposte (2)

John D'Errico
John D'Errico il 15 Apr 2018
Simplest is to use a traveling salesman solver.
theta = linspace(pi/2,-pi/2,10)';
xy = [0 0; 1 1; 0 1; 1 0;1 - cos(theta)/3,.5 + sin(theta)/3];
xy = xy(randperm(14),:); % just to make it worse.
plot(xy(:,1),xy(:,2),'o-')
Now just find a TSP solver. Anyone will do. tspo_ga (written by Joseph Kirk) just happens to be one I had downloaded from the FEX recently.
points.XY = xy;
P = tspo_ga(points);
plot(xy(P.optRoute,1),xy(P.optRoute,2),'o-')
This is an open TSP solver, so you could connect the first and last points to create a closed polygon.
  3 Commenti
John D'Errico
John D'Errico il 16 Apr 2018
Modificato: John D'Errico il 16 Apr 2018
In general, a TSP solution (once the optimal solution has been found) will not cross edges. If there were edges that would cross, then there is a better solution that does not cross. You can probably prove this. So a solution that has crossed edges should not be truly optimal.
Leo Carrera
Leo Carrera il 30 Ott 2020
Get the center of mass of all the points you have (with mean() )
Then, calculate the angle (with atan2) between each point and the center of mass (in x and y)
Sort the angles from the smallest to the greatest and get the indexes
Finally, sort your vertices with the indexes and plot normally

Accedi per commentare.


Walter Roberson
Walter Roberson il 15 Apr 2018
Have you tried boundary() and changing the alpha value to make a tighter fit?

Categorie

Scopri di più su Elementary Polygons in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by