gpt4 book ai didi

python - 生成点不在对方范围内?

转载 作者:行者123 更新时间:2023-11-28 20:52:00 25 4
gpt4 key购买 nike

我正在尝试生成一组点,这些点在固定区域中不落在彼此的范围内。我的方法如下:

import collections
from random import uniform

X = 100.0
Y = 100.0
points = 10
radius = 10

def in_circle(c_x, c_y, radius, x, y):
dist_squared = (c_x - x)**2 + (c_y - y)**2
return dist_squared <= radius ** 2

current = collections.defaultdict(lambda: [])

threshold = 0

for point in range(1, points+1):
cX = uniform(1.0, X)
cY = uniform(1.0, Y)

for cur in current:
while in_circle(current[cur][0], current[cur][1], 2*radius, cX, cY):
cX = uniform(1.0, X)
cY = uniform(1.0, X)

threshold += 1
if threshold >= 1e+05:
print "Cannot satisfy constraints"
sys.exit(1)

threshold = 0

current[point] = [cX, cY]
print cX, cY

有没有什么好的方法可以终止这个算法而不使其进入死循环?我确实有阈值检查,但是否有更好的方法来执行此操作?

最佳答案

This article关于泊松盘采样,您可能会感兴趣。作者解释了一种选择彼此不太接近的点的策略,甚至提供了包括 Python 在内的几种语言的示例代码。

正如您所指出的,您概述的策略的问题是,如果您想选择很多点,或者您希望这些点相距很远,性能可能会变得很糟糕。我相信泊松盘方案具有更好的性能特征。

关于python - 生成点不在对方范围内?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7928204/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com