Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python和pygame中的随机非重叠圆(圈数受控)_Python_Random_Pygame_Circle Pack - Fatal编程技术网

python和pygame中的随机非重叠圆(圈数受控)

python和pygame中的随机非重叠圆(圈数受控),python,random,pygame,circle-pack,Python,Random,Pygame,Circle Pack,我在为不同半径的非重叠随机圆编写代码。我得到了应得的结果,但我检查重叠或非重叠的“if”语句排除了一些圆。所以,我得到的圆的数目较少。代码如下: import pygame import numpy as np pygame.init() display_width = 800 display_height = 500 black = [0, 0, 0] white = [255, 255, 255] red = [255, 0, 0] display_surface = pygame.d

我在为不同半径的非重叠随机圆编写代码。我得到了应得的结果,但我检查重叠或非重叠的“if”语句排除了一些圆。所以,我得到的圆的数目较少。代码如下:

import pygame
import numpy as np

pygame.init()
display_width = 800
display_height = 500

black = [0, 0, 0]
white = [255, 255, 255]
red = [255, 0, 0]

display_surface = pygame.display.set_mode((display_width, display_height))
clock = pygame.time.Clock()
pygame.display.set_caption("Random Circle")


def circle(x, y, r):
    pygame.draw.circle(display_surface, red, (int(x), int(y)), int(r), 2)


def distance(x1, y1, x2, y2):
    dsq = (x1 - x2) ** 2 + (y1 - y2) ** 2
    d = np.sqrt(dsq)
    return d


n = 100

r = np.random.randint(10, 20, size=n)
x = np.random.randint(r, display_width - r, size=n)
y = np.random.randint(r, display_height - r, size=n)

display_surface.fill(black)

for i in range(len(r)):
    valid = True
    for j in range(len(r)):
        if i != j:
            d = distance(x[i], y[i], x[j], y[j])
            if d < r[i] + r[j]:
                valid = False

    if valid:

        circle(x[i], y[i], r[i])

    pygame.display.update()
    clock.tick(100)

exit = False
while not exit:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()
导入pygame
将numpy作为np导入
pygame.init()
显示宽度=800
显示高度=500
黑色=[0,0,0]
白色=[255,255,255]
红色=[255,0,0]
display\u surface=pygame.display.set\u模式((显示宽度、显示高度))
clock=pygame.time.clock()
pygame.display.set_标题(“随机圈”)
def圆(x、y、r):
pygame.draw.circle(显示曲面,红色,(int(x),int(y)),int(r),2)
def距离(x1、y1、x2、y2):
dsq=(x1-x2)**2+(y1-y2)**2
d=np.sqrt(dsq)
返回d
n=100
r=np.random.randint(10,20,size=n)
x=np.random.randint(r,显示宽度-r,大小=n)
y=np.random.randint(r,显示高度-r,大小=n)
显示曲面填充(黑色)
对于范围内的i(len(r)):
有效=真
对于范围内的j(len(r)):
如果我j:
d=距离(x[i],y[i],x[j],y[j])
如果d
我看到了一个问题的答案,这个答案与代码的功能完全相同(除了它使用了一类我没有使用的圆)。
我感觉到我必须使用某种while循环,直到我得到我想要的圈数为止。但是我发现在实际编写代码时有困难。有人能帮忙吗

创建一个圆列表每个圆是一个元组,包含3个组件,x和y坐标以及半径。只要列表长度小于
n:

circle_list=[]
而len(圆圈列表)
创建一个随机位置和半径:

r=random.randint(10,20)
x=random.randint(r,显示宽度-r)
y=random.randint(r,显示高度-r)
评估该圆是否与列表中的其他圆相交:

collide=False
对于圆圈列表中的x2、y2、r2:
d=距离(x,y,x2,y2)
如果d
如果圆没有碰撞,则附加圆:

如果不发生碰撞:
圈_列表。追加((x,y,r))

最简单的例子:

随机导入
输入数学
导入pygame
def欧氏距离(x1,y1,x2,y2):
#返回数学sqrt((x1-x2)**2+(y1-y2)**2)
返回数学正态分布((x1-x2),(y1-y2))
pygame.init()
window=pygame.display.set_模式((400400))
font50=pygame.font.SysFont(无,50)
clock=pygame.time.clock()
pygame.时间延迟(10000)
运行=真
圈_列表=[]
运行时:
时钟滴答(100)
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
运行=错误
r=random.randint(10,20)
x=random.randint(10+r,window.get_width()-r-10)
y=random.randint(10+r,window.get_height()-r-10)
如果欧几里德距离(x,y,x2,y2)