-1

I'm working in Python and I'm trying to generate coordinates for circles with their radii being appended after coordinate generation. Once I have the list I want to check it against itself to see if any of the circles will overlap each other, and if so remove one of them. Below is what I have so far and I haven't found a way to iterate through to do what I need.

import random
import math
def circ(x, y):
    return[x, y]
list = []
rad = 1
for i in range(3):
    list.append(circ(random.uniform(rad, 10-rad), random.uniform(rad, 10-rad)))
print(list)

for x in list:
    x.append(rad)
    print(x)
print(list)
## Append list of lists with radius so each entry is x, y, r!

def checker(x, y, r, i, j, k):
    distance = math.sqrt((i-x)**2 + (j-y)**2)
    avgDiam = (.5*(r+k))
    return distance, avgDiam
4
  • 1
    Don't use list as a variable name, it's the name of a built-in class. Commented May 15 at 22:26
  • 2
    What problem are you having? You clearly know how to do for x in list:, why can't you use it to do what you want? Commented May 15 at 22:27
  • You probably need nested loops: for i, x in enumerate(list): for y in list[i+1:]: .... Then you can compare x and y to see if they overlap. Commented May 15 at 22:30
  • 2
    It might be simpler to check for overlaps as you make each new circle, and add the new circle to the list only if there are no overlaps. Commented May 15 at 23:41

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.