You could iterate the circles in a nested loop in order to visit all possible pairs of circles. Then for each pair you can check whether they overlap or not.
I would also suggest that you use a tuple to store the 3 properties of a circle, including the radius from the very start instead of appending it later -- a circle is not a circle if it doesn't have a radius. Named tuples make the code easier to read: you can then write circle.rad instead of circle[2].
Here is some code you could use:
import random
from collections import namedtuple
Circle = namedtuple("Circle", "x, y, rad")
def create_random_circles(count, rad):
return [
Circle(random.uniform(rad, 10-rad),
random.uniform(rad, 10-rad),
rad) # Immediately use rad for the circle's radius
for i in range(count)
]
def has_overlap(circle1, circle2):
distance = math.sqrt((circle1.x-circle2.x)**2 + (circle1.y-circle2.y)**2)
min_distance = circle1.rad + circle2.rad
return distance < min_distance
def get_overlapping_pairs(circles):
return [
(circle1, circle2)
for i, circle2 in enumerate(circles)
for circle1 in circles[:i]
if has_overlap(circle1, circle2)
]
# generate a list with 6 circles that each have a radius of 1
circles = create_random_circles(6, 1)
print("all circles:")
for circle in circles:
print("circle:", circle)
# See which of these circles overlap:
pairs = get_overlapping_pairs(circles)
print("pairs of circles that overlap:")
for pair in pairs:
print("pair:", *pair)
Note that there are more advanced methods to find overlapping circles when your list of circles becomes large. But that seems to go beyond the scope of this question.
listas a variable name, it's the name of a built-in class.for x in list:, why can't you use it to do what you want?for i, x in enumerate(list): for y in list[i+1:]: .... Then you can comparexandyto see if they overlap.