我想创建并返回一组随机的2元素元组,这些元组表示二维图上的点。我的问题是,我希望每个点之间至少有一定的距离。这应该根据下面函数中minDistance参数的值
我想不出一种方法来穿过一个集合,检查每个点的距离,同时替换距离不够远的点。我怎样才能做到呢
注:图长90点,宽160点
到目前为止,我的职责是:
def makeTiles(num, xBounds, yBounds, minDistance):
"""
Creates and returns a set of points.
:param num: int
The number of points to be returned.
:param xBounds: tuple of 2 ints
The first element is the minimum an x-value should be.
The second element is the maximum an x-value should be.
:param yBounds: tuple of 2 ints
The first element is the minimum an y-value should be.
The second element is the maximum an y-value should be.
:param minDistance: int
The minimum distance that should occur between points.
:return: set of tuples
The set of points that will be created.
"""
tileSet = set()
for n in range(num):
x = r.randint(xBounds[0], xBounds[1])
y = r.randint(yBounds[0], yBounds[1])
tileSet.add((x, y))
tempSet = tileSet.copy()
distances = set()
for t1 in tempSet:
for t2 in tileSet:
distances.add(m.sqrt((t1[0] - t2[0]) ** 2 + (t1[1] - t2[1]) ** 2))
for d in distances:
if d < minDistance:
您应该查看Quadtrees,它们允许在这种检查中获得更好的性能。 除此之外,除了检查图形中从每个点到每个其他点的距离之外,没有其他方法
还要确保当你比较点的时候,你不要把一个点和它自己核对
我自己想出了一个算法
对于我想要得到的每一个点,我加上所有必要的点,使一个正方形围绕着这个点。我将这些点和原始点添加到一个临时集,并添加一个新点,同时确保它不在临时集中。然后我只要循环多少次就行了
这不是最快的方法,但它是有效的
相关问题 更多 >
编程相关推荐