Sort point list into polygon

白昼怎懂夜的黑 提交于 2019-12-02 08:01:01

I don't think there's a well-defined solution to this. Consider five points like this:

.   .
  .
.   .

What polygon would be correct here?

You have to order the points so you walk around the polygon with the interior on your left (or right) as you move from point to point. Convex or concave, this is the correct approach.

But knowing the points is not sufficient. You have to know the connectivity of each edge segment as well. Knowing that, you can start at any point and walk along the perimeter until you reach the starting point again.

I'm not sure this is the fastest way, but it seems to work.

The whole idea is to connect the points using line segments that do not intersect (except at the points, and this is a little trickier to define than you might think). So start with the original unsorted list, connect them in order -- forming a closed path that may have many crossings -- and then eliminate the crossings one by one, by means of reversing subsequences of points in the list.

Suppose we start with [a b c d e f g h], and discover that the b-c edge crosses the g-h edge. We reverse the c-g sequence to get a new list: [a b g f e d c h]. Two edges have been removed and two new ones created; the rest are undisturbed (although some have had their directions reversed).

I have not been able to find a case in which this process would run forever (that is, the list would return to a previous state), nor a proof that this cannot happen.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!