Generating Four Sublists of Points with Adjacency Conditions in GHpython

25 Views Asked by At

O = b
▭ = a

when I want to run this code some seeds in one of the sublists can not select a point from list b because the condition (the distance between the selected point and two other points in the sublist is smaller than 2.5) is not met. How can I have four sublists correctly with this condition?

import rhinoscriptsyntax as rs
import random

# Set the seed for random number generation

random.seed(seed)

a = x  # Replace 'x' with your list of points

sublist1 = []
sublist2 = []
sublist3 = []
sublist4 = []

while True:
    # Clear sublists
    sublist1 = []
    sublist2 = []
    sublist3 = []
    sublist4 = []

    # Randomly select two unique points for sublist1
    sublist1 = random.sample(a, 2)

    # Randomly select two unique points for sublist2
    sublist2 = random.sample(a, 2)

    # Randomly select two unique points for sublist3
    sublist3 = random.sample(a, 2)

    # Randomly select two unique points for sublist4
    sublist4 = random.sample(a, 2)

    # Check if the distances between the two points in each sublist are smaller than 1.5
    if rs.Distance(sublist1[0], sublist1[1]) < 1.5 and rs.Distance(sublist2[0], sublist2[1]) < 1.5 and rs.Distance(sublist3[0], sublist3[1]) < 1.5 and rs.Distance(sublist4[0], sublist4[1]) < 1.5:
        # Check if any points are duplicated across sublists
        if len(set(sublist1 + sublist2 + sublist3 + sublist4)) == 8:
            break




# Randomly select 1 item from list b for sublist 1
selected_point1 = None
attempts = 0

while selected_point1 is None and attempts < len(b):
    random.shuffle(b)
    for point in b:
        distances = [rs.Distance(rs.coerce3dpoint(point), rs.coerce3dpoint(p)) for p in sublist1]
        if all(distance < 2.5 for distance in distances):
            selected_point1 = point
            break
    attempts += 1

if selected_point1 is not None:
    sublist1.append(selected_point1)
    
# Remove selected item from list b
if selected_point1 in b:
    b.remove(selected_point1)


# Randomly select 1 item from list b for sublist2
selected_point2 = None
attempts = 0

while selected_point2 is None and attempts < len(b):
    random.shuffle(b)
    for point in b:
        distances = [rs.Distance(rs.coerce3dpoint(point), rs.coerce3dpoint(p)) for p in sublist2]
        if all(distance < 2.5 for distance in distances):
            selected_point2 = point
            break
    attempts += 1

if selected_point2 is not None:
    sublist2.append(selected_point2)
    
# Remove selected item from list b
if selected_point2 in b:
    b.remove(selected_point2)
    

# Randomly select 1 item from list b for sublist3
selected_point3 = None
attempts = 0

while selected_point3 is None and attempts < len(b):
    random.shuffle(b)
    for point in b:
        distances = [rs.Distance(rs.coerce3dpoint(point), rs.coerce3dpoint(p)) for p in sublist3]
        if all(distance < 2.5 for distance in distances):
            selected_point3 = point
            break
    attempts += 1

if selected_point3 is not None:
    sublist3.append(selected_point3)
    
# Remove selected item from list b
if selected_point3 in b:
    b.remove(selected_point3)
    

# Randomly select 1 item from list b for sublist4
selected_point4 = None
attempts = 0

while selected_point4 is None and attempts < len(b):
    random.shuffle(b)
    for point in b:
        distances = [rs.Distance(rs.coerce3dpoint(point), rs.coerce3dpoint(p)) for p in sublist4]
        if all(distance < 2.5 for distance in distances):
            selected_point4 = point
            break
    attempts += 1

if selected_point4 is not None:
    sublist4.append(selected_point4)
    
# Remove selected item from list b
if selected_point4 in b:
    b.remove(selected_point4)

O = b                     ▭ = a

when I want to run this code some seeds in one of the sublists can not select a point from list b because the condition (the distance between the selected point and two other points in the sublist is smaller than 2.5) is not met. How can I have four sublists correctly with this condition?

0

There are 0 best solutions below