python - 如何检查 2D 图表中所有点的距离并确保没有点太近?

标签 python python-3.x procedural-generation

我想创建并返回一组随机的 2 元素元组,这些元组代表 2D 图上的点。我的问题是我希望每个点彼此之间至少有一定的距离。这应该根据下面函数中 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 ,它们可以在这种检查中获得更好的性能。 除此之外,除了检查图表中每个点到其他点的距离之外,没有其他办法。

还要确保在比较点时,不要对照点本身进行检查。

关于python - 如何检查 2D 图表中所有点的距离并确保没有点太近?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50160647/

相关文章:

python - 尝试连接到 imap 时获取 "sslv3 alert handshake failure"

python - 消除 "Game"单例中的耦合和全局状态

python - 如何使用noise.py模块选择种子

opengl - 在 OpenGL 中用三角形绘制的圆柱体

python - 在 python/pandas 中按月对每日数据进行分组,然后进行归一化

python - Matplotlib/Seaborn : how to plot a rugplot on the top edge of x-axis?

python - Pytest 的 Mock/Monkeypatch BeautifulSoup html 对象

python-3.x - gensim 词嵌入(Word2Vec 和 FastText)模型中的 alpha 值?

python-3.x - 从 SpaCy 中删除跨度中的单词?

textures - 径向渐变 - cocos2d-x v3.15+ 的程序纹理