[英]How can I order a list of Points so that no lines drawn intersect?
I am getting a List of Points from the user clicking on the screen.我从点击屏幕的用户那里得到了一个点列表。
And I want to draw a polygon based on this points.我想根据这些点绘制一个多边形。 The problem is that the user might not be clicking in the right order ( no intersecting segments ) to form a correct polygon, therefor I'm looking for a code snippet which would sort that List and arrange the points in the right order to form a good polygon...问题是用户可能没有按正确的顺序(没有相交的线段)单击以形成正确的多边形,因此我正在寻找一个代码片段,该代码片段将对该列表进行排序并以正确的顺序排列点以形成一个好多边形...
Thanks!谢谢!
picture = BAD POLY!图片 = BAD POLY!
- -
picture = GOOD POLY!图片 = 好聚!
如果要“包裹”多边形中的点,可以使用任意数量的凸包发现算法。
Dude i had the same problem, but i found a code that helped me. 伙计我有同样的问题,但我发现了一个帮助我的代码。 I think this resolve your problem . 我认为这可以解决你的问题。
See this link: http://jsfiddle.net/9DHSf/3/ 看到这个链接: http : //jsfiddle.net/9DHSf/3/
You could take the centroid of all the points, then taking the dot product of each point to the centre with a reference point (say the first in the list) to the centre, get the angle of each point in your list from an arbitrary reference vector. 您可以获取所有点的质心,然后将每个点的点积带到中心,并使用参考点(比如列表中的第一个)到中心,从任意参考中获取列表中每个点的角度向量。 Then order that list on the angle. 然后在角度上订购该列表。 That'll give you the points in a (eg) clockwise winding order, so your line is just p1 -> p2, p2 -> p3 etc... 这将给你(例如)顺时针缠绕顺序中的点,所以你的线只是p1 - > p2,p2 - > p3等...
If you want to find the "concave" hull (ie, clockwise sort the points around a centroid), you might try:如果你想找到“凹”壳(即顺时针排序质心周围的点),你可以尝试:
import matplotlib.pyplot as plt
import numpy as np
# List of coords
coords = np.array([7,7, 5, 0, 0, 0, 5, 10, 10, 0, 0, 5, 10, 5, 0, 10, 10, 10]).reshape(-1, 2)
centroid = np.mean(coords, axis=0)
sorted_coords = coords[np.argsort(np.arctan2(coords[:, 1] - centroid[1], coords[:, 0] - centroid[0])), :]
plt.scatter(coords[:,0],coords[:,1])
plt.plot(coords[:,0],coords[:,1])
plt.plot(sorted_coords[:,0],sorted_coords[:,1])
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.