1

I am having some difficulties trying to produce circles that do not overlap in python. I have tried both pylab and matplotlib but to no avail. I have a feeling its something to do with my logic/algorithm and I need some assistance in looking through whats wrong. (P.S Forgive me if I am formatting this problem wrongly. Its my first time using StackOverFlow.) Thanks in advance.

Python Pygame randomly draw non overlapping circles

I have seen this link, but I'm unable to see whats wrong!

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()

Circles still overlap!

Axois
  • 1,961
  • 2
  • 11
  • 22

1 Answers1

0

The issue is the variable in your code shouldprint. It becomes whatever the last tested circles are, when it should be False if it finds one circle that overlaps.

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

You also will need to make shouldprint start as True before you test a single circle, so you want it inside the while loop to reset it.

# 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

And last, you can just return the value of a boolean result. You don't need to say "if true, return true".

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

Can be converted to

return dist >= (r1 + r2)
Jack Cole
  • 1,528
  • 2
  • 19
  • 41
  • 1
    Wow! It worked! Thanks a lot :). I think the problem with my issue is that i have added the "else" statement which caused it to check based on the last iteration of my list. Nevertheless, omitting the else statement solved it. Thank you so much! – Axois Jun 08 '19 at 03:52