У меня возникли некоторые трудности при попытке создать круги, которые не пересекаются в питоне.Я пробовал и pylab и matplotlib, но безрезультатно.Я чувствую, что это как-то связано с моей логикой / алгоритмом, и мне нужна помощь в поиске того, что не так.(PS Простите, если я неправильно форматирую эту проблему. Это мой первый раз, когда я использую StackOverFlow.) Заранее спасибо.
Python Pygame случайным образом рисует непересекающиеся круги
Iвидел эту ссылку, но я не вижу, что не так!
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()
Круги все еще перекрываются!