简体   繁体   English

我怎样才能订购点列表,以便绘制的线不相交?

[英]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.

相关问题 如何从已绘制的任何点/线中清除 linerenderer 并使用标志返回点和线? - How can I clear a linerenderer from any points/lines that have beem drawn and return the points and lines back using a flag? 给定一组随机链接的随机点,如何定位它们以使它们的链接线不相交? - Given a set of random points that are randomly linked, how to position them so that their link lines do not intersect? 如何单击绘制的点并打开外部窗口? - How can I click on drawn points and make a external window opening? 如何修剪列表 <string> 所以删除前面和后面的空白行? - How can I trim a List<string> so preceding and succeeding blank lines are removed? 如何判断两个多边形是否相交? - How can I tell if two polygons intersect? 如何在所有绘制的点之间画一条线? - How do I draw a line between all the drawn points? 如何使用 Sprache 解析可以以任何顺序出现的行? - How can I parse lines that can appear in any order with Sprache? 如何对dataGridView行单元格[0]中的值进行排序,以使其与textbox.lines [i]的顺序相同? - How to sort the values in a dataGridView Row Cells[0] so that they were in the same order that textbox.lines[i]? 如何提取order by子句,以便我可以使用逻辑来设置它? - How to extract an order by clause so I can use logic to set it? 如何订购清单 <string[]> ? - How can I Order a List<string[]>?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM