# Python - Prevent randomly generated points from overlapping each other

Hi, I am new with python and I am still learning so my problem might be very trivial but I just cannot get the right solution for this problem.
So, I have randomly generated points inside a square. Each point has a radius. But the problem with generated points is that they are overlapping each other. How can I resolve this?

roof with holes.gh (22.9 KB)

import rhinoscriptsyntax as rs
from math import sin, cos, pi
import random

#square
a = 50
b = 50

V1 = 0, 0, 0
V2 = 0, b, 0
V3 = a, b, 0
V4 = a, 0, 0
vogali = [V1,V2,V3,V4]

#draw edges
edges =
for i in range(4):

#randomly generated points
points_d =
points_u =
x = 0
y = 0

for i in range(n_points):
delta_x = 0.025 * a
delta_y = 0.025 * b

``````x = random.random() * a
y = random.random() * b
z = 0

#check for each point if it ovelapps the other
if  x != y  and y * delta_y  != x * delta_x   :

For each new point first check the distance to each existing point?
I think there is a function something like rs.PointCoordinates(id) to get the coordinates of an existing pt

Yes, but that only checks point coordinates and it still generates them. I would like to draw them if they are outside the radius of the previos points, otherwise not but I don’t know how to write the right pseudo code.

Something like:?

``````too_close = True
while too_close:
x = random...
....
for pt in points_u:
x1, y1, z1 = rs.PointCoordinates(pt)
distance = math.sqrt((x1-x)**2 + (y1-y)**2)
if distance < radius * 2:
too_close = True
break
else:
too_close = False``````

I tried it but it doesn’t work. It only generates one point. Weird because there are not any errors. But I have probably missed something because your code seems right. I’ll try again, thank you.

works for me :

``````import rhinoscriptsyntax as rs
from math import sin, cos, pi, sqrt
import random

#square
a = 50
b = 50

V1 = 0, 0, 0
V2 = 0, b, 0
V3 = a, b, 0
V4 = a, 0, 0
vogali = [V1,V2,V3,V4]

#draw edges
edges = []
for i in range(4):

#randomly generated points
points_d = []
points_u = []
x = 0
y = 0

for i in range(10):
delta_x = 0.025 * a
delta_y = 0.025 * b

too_close = True
while too_close:
x = random.random() * a
y = random.random() * b
z = 0
for pt in points_u:
x1, y1, z1 = rs.PointCoordinates(pt)
distance = sqrt((x1-x)**2 + (y1-y)**2)
if distance < radius * 2:
too_close = True
break
else:
too_close = False