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?
Thank you for your help in advance.

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):
edges.append(rs.AddLine(vogali[i-1],vogali[i],))

#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
radius = min(delta_x, delta_y)

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   :
    points_u.append(rs.AddPoint(x,y,z))
    points_d.append(rs.AddPoint(x,y,max(a,b)))

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):
    edges.append(rs.AddLine(vogali[i-1],vogali[i],))

#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
    radius = min(delta_x, delta_y)

    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
    
    points_u.append(rs.AddPoint(x,y,z))
    points_d.append(rs.AddPoint(x,y,max(a,b)))

Yes it works! Thank you so much!

You’re very welcome :slight_smile:
See here for more about flow control in Python:
https://docs.python.org/2.7/tutorial/controlflow.html