防止圆重叠

2024-09-30 08:36:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我在尝试生成python中不重叠的圆时遇到了一些困难。我试过pylab和matplotlib,但都没用。我觉得这和我的逻辑/算法有关,我需要一些帮助来找出问题所在。(请原谅,如果我把这个问题格式化错了。这是我第一次使用StackOverFlow。)提前谢谢。你知道吗

Python Pygame randomly draw non overlapping circles

我看到了这个链接,但我看不出有什么问题!你知道吗

from pylab import *
from scipy.spatial import distance
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import random

fig, ax = plt.subplots()
list1 = [[0,0,0]]
def generatecircle():
    stuff1 = 0
    stuff2 = 20
    while stuff1 <stuff2:
        x = random.random()
        y = random.random()
        r = 0.05
        shouldprint = True
        for i in list1:
            print("****")
            print(x)
            print(i)
            print(list1)
            if notincircle(x,y,r , i[0],i[1],i[2]):
                shouldprint = True
            else:
                shouldprint =  False

        if shouldprint:
            list1.append([x,y,r])
            circle = Circle((x,y), radius = r, facecolor = 'red')
            ax.add_patch(circle)
            stuff1 += 1     
            try:
                list1.remove([0,0,0])
            except:
                pass


def notincircle(x1,y1,r1,x2,y2,r2):
    dist = math.sqrt( ((x2-x1)**2) + ((y2-y1)**2) )
    if dist >= (r1 + r2):
        return True
    else:
        return False

generatecircle()
plt.show()
import pygame, random, math

red = (255, 0, 0)
width = 800
height = 600
circle_num = 10
tick = 2
speed = 5

pygame.init()
screen = pygame.display.set_mode((width, height))

list1 = [[0,0,0]]
def generatecircle():
    stuff1 = 0
    stuff2 = 20
    shouldprint = True
    while stuff1 <stuff2:
        x = random.randint(0,width)
        y = random.randint(0,height)
        r = 50

        for i in list1:
            print("****")
            print(x)
            print(i)
            print(list1)
            if notincircle(x,y,r , i[0],i[1],i[2]):
                shouldprint = True
            else:
                shouldprint =  False

        if shouldprint:
            print("Print!")
            list1.append([x,y,r])
            #circle = Circle((x,y), radius = r, facecolor = 'red')
            pygame.draw.circle(screen, red, (x,y), r, tick)
            pygame.display.update()
            stuff1 += 1     
            try:
                list1.remove([0,0,0])
            except:
                pass


def notincircle(x1,y1,r1,x2,y2,r2):
    dist = math.sqrt( ((x2-x1)**2) + ((y2-y1)**2) )
    if dist >= (r1 + r2):
        return True
    else:
        return False

generatecircle()
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()

圆圈仍然重叠!你知道吗


Tags: importtrueifdefrandompygameelseprint
1条回答
网友
1楼 · 发布于 2024-09-30 08:36:04

问题是代码中的变量shouldprint。无论最后测试的圆是什么,当它发现一个重叠的圆时,它应该为假。你知道吗

for i in list1:
    print("****")
    print(x)
    print(i)
    print(list1)
    # Should print will now be false if even 1 circle is found
    if not notincircle(x,y,r , i[0],i[1],i[2]):
        shouldprint = False

在测试单个圆之前,还需要使shouldprint开始为True,因此希望它在while循环中重置。你知道吗

# shouldprint = True # This should be deleted
    while stuff1 < stuff2:
        x = random.randint(0,width)
        y = random.randint(0,height)
        r = 50
        shouldprint = True # Moved should print here

最后,您可以返回布尔结果的值。你不需要说“如果是真的,返回真的”。你知道吗

if dist >= (r1 + r2):
    return True
else:
    return False

可以转换为

return dist >= (r1 + r2)

相关问题 更多 >

    热门问题