I was writing the code for non overlapping random circles with different radii.I kind of got the deserved, but my 'if' statement that checks overlap or non overlap excludes a number of circles. So, I get lesser number of circles. Here's the code:
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()
I saw an answer to a question here which does exactly what this code does( except it uses a class of circles which I didn't use). I sense that I have to use some kind of while loop which doesn't end until I get my desired number o circles. But I find difficulty in actually writing the code. Can anyone help?
Aucun commentaire:
Enregistrer un commentaire