问题描述
我知道这听起来微不足道,但我的头被拒绝给出一个算法这一点。
I know this sounds trivial, but my head is refusing to give an algorithm for this.
我有一堆散落在一个2-D平面上的点,并希望将它们存储在一个列表中,这样他们创造一个环。点不属于上一周期。
I have a bunch of points scattered on a 2-D plane and want to store them in a list such that they create a ring. The points do not belong on a cycle.
开始从该列表中的第一个点(红在这个图),并依次基于其距离其余添加
Start from the first point in the list (red in this pic) and sequentially add the rest based on their distance.
因为我不能回答我的问题,我会在这里发表一个可能的答案。
Since I cannot answer my question I will post here a possible answer.
这是似乎做这项工作的方法。V.pos保持节点和距离的位置()仅仅是一个函数,返回两个点之间的欧几里得距离。更快的方法也将其附加到环后删除next_node,这样你就不必去通过已连接点
This is an approach that seems to do the job.V.pos holds the positions of the nodes and distance() is just a function that returns the Euclidean distance between two points. A faster approach would also delete the next_node after appending it to the ring so that you don't have to go through the already connected points
圈= [节点[0] 而LEN(圈)<的len(节点): MINL = 99999 因为我在范围内(LEN(节点)): DIST =距离(V.pos [圈[-1],V.pos [节点[I]) 如果DIST< MINL和节点[我]不响: MINL = DIST next_node =节点[I] ring.append(next_node)
ring = [nodes[0]] while len(ring) < len(nodes): minl=99999 for i in range(len(nodes)): dist = distance(V.pos[ring[-1]],V.pos[nodes[i]]) if dist<minl and nodes[i] not in ring: minl = dist next_node = nodes[i] ring.append(next_node)
推荐答案
下面有一个想法,这将使好吗杂交的结果,如果你的点云已经环形喜欢你的例子:
Here's an idea that will give okay-ish results if your point cloud is already ring-shaped like your example:
- 确定中心点;这既可以是中心的所有点的重心或边界框的中心。
- 重新present径向坐标(半径,角度)参照所有点到中心
- 排序角度
这当然会,产生锯齿星为随机的云彩,但目前尚不清楚,到底什么是环是。你也许可以用这个作为一个初稿,并开始交换节点,如果说给你一个较短的总距离。也许这种简单的code是所有你需要实现短的最小距离过图的所有节点。
This will, of course, produce jagged stars for random clouds, but it is not clear, what exactly a "ring" is. You could probably use this as a first draft and start swapping nodes if that gives you a shorter overall distance. Maybe this simple code is all you need short of implementing the minimum distance over all nodes of a graph.
Anayway,这里有云:
Anayway, here goes:
import math
points = [(0, 4), (2, 2), ...] # original points in Cartesian coords
radial = [] # list of tuples(index, angle)
# find centre point (centre of gravity)
x0, y0 = 0, 0
for x, y in points:
x0 += x
y0 += y
x0 = 1.0 * x0 / len(points)
y0 = 1.0 * y0 / len(points)
# calculate angles
for i, p in enumerate(points):
x, y = p
phi = math.atan2(y - y0, x - x0)
radial += [(i, phi)]
# sort by angle
def rsort(a, b):
"""Sorting criterion for angles"""
return cmp(a[1], b[1])
radial.sort(rsort)
# extract indices
ring = [a[0] for a in radial]
这篇关于创建一个周期从散点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!