How do you order the vertices of a non-convex polygon?
18 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
回答(2 个)
John D'Errico
2018-4-15
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 个评论
John D'Errico
2018-4-16
编辑:John D'Errico
2018-4-16
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
2020-10-30
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
Walter Roberson
2018-4-15
Have you tried boundary() and changing the alpha value to make a tighter fit?
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Elementary Polygons 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!