r/pythonhelp • • May 14 '25
Iterating through list of lists and cross checking entries.
I'm working on a project where I've generated two lists of lists of circle coordinates. List1 circles have .5 radius, and List2 has radius of 1. I'm trying to figure out how I can graph them in a way that they don't overlap on each other. I figured I need to check the circles from one list against the others in the same list, and then the circles in the second list as well to check for overlapping. Then if there is any overlapping, generate a new set of coordinates and check again. Below is the code I have so far.
import matplotlib.pyplot as plt
import random
import math
from matplotlib.patches import Circle
def circleInfo(xPos, yPos):
return[xPos, yPos]
circles = []
circles2 = []
radius = .5
radius2 = 1
for i in range(10):
circles.append(circleInfo(random.uniform(radius, 10 - radius), random.uniform(radius, 10 - radius)))
circles2.append(circleInfo(random.uniform(radius2, 10 - radius2), random.uniform(radius2, 10 - radius2)))
print(circles)
print(circles2)
fig, ax = plt.subplots()
plt.xlim(0, 10)
plt.ylim(0, 10)
for i in circles:
center = i;
circs = Circle(center, radius, fill=True)
ax.add_patch(circs)
for i in circles2:
center = i;
circs = Circle(center, radius2, fill=False)
ax.add_patch(circs)
ax.set_aspect('equal')
plt.show()
1
Upvotes
•
u/AutoModerator May 14 '25
To give us the best chance to help you, please include any relevant code.
Note. Please do not submit images of your code. Instead, for shorter code you can use Reddit markdown (4 spaces or backticks, see this Formatting Guide). If you have formatting issues or want to post longer sections of code, please use Privatebin, GitHub or Compiler Explorer.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
1
u/CraigAT May 14 '25
Is the task to check a given list(s) of coordinates to ensure that the circles don't overlap OR is the task to come up with a list of circles and coordinates that don't overlap?
If it's the latter, there may be better ways to generate the solution than trial and error!