how to order vertices in a non-convex polygon (how to find one of many solutions)

早过忘川 提交于 2019-12-17 18:22:01

问题


I have the same problem as here: how to order vertices in a simple, non-convex polygon but there is no solutions I can use.

I have coordinates of points and need to find some polygon. Does not matter that there is more solutions for one list of dots. I need some algorithm to find one of them. Does not matter which one. I really don't know how to solve this.

(I have stored coordinates in array and I want to use some algorithm in Javascript)

Thanks a lot.


回答1:


First, find the center of the bounding box that contains all of your vertices. We'll call this point C.

Sort your list of vertices based on each point's angle with respect to C. You can use atan2(point.y - C.y, point.x - C.x) to find the angle. If two or more vertices have the same angle, the one closer to C should come first.

Then, draw your points in the order they appear in the list. You will end up with a starburst pattern that is non-intersecting and probably non-convex. Example:



来源:https://stackoverflow.com/questions/19713092/how-to-order-vertices-in-a-non-convex-polygon-how-to-find-one-of-many-solutions

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