[SOLVED]Grasshopper + GHpython create lines, based on relations between set of points

I’m working on this file for my senior project at architecture school I have set of points where every point represent specific space and another set that determines the relationship between the spaces using (0,1) I tried to use GHpython to check if there is a relation or no and if it is trow drow a line between the two points that represent these spaces. I think I did it right but the GHpython component is not giving me the set of lines and I don’t know why I tried different methods and no one has worked for me this my code please help me to find what is the problem?

import rhinoscriptsyntax as rs 

i=0 
j=0
k=N
start=0
end=0

curves=[]

while i<=N:
    if relations [i] == 1:
        start=  points[i/k]
        end= points [i%N]
        curves = rs.AddLine (start,end)
        i=i+1
    else:
        i=i+1
a=curves

Hi,
Have you tried debugging by adding print (start, end) within your loop?
I cant see what relations or points refers to. Also I dont understand the logic in your modular arithmetic for the indexing but I guess that might make sense seeing your data structures.
You need to share some more data (e.g. simplified version of the file with all the necessary data to make it work)

1 Like

Also isn’t start always going to equal points[0] except for the last iteration where it will equal points[1], given that k = N and the loop stops when i == N ? Is this intentional?

1 Like

Hello Graham thank you for your replay this my grasshopper definition

each point represent a space then after that I have another list (relations matrix) with 30x30 value on it
like the one in this image

the python code should take the points list and the relations list as an input
then will try to construct the relationships matrix and if the value of point [i] (where i is the index in the relation list) =1 that means there is a relation between the two spaces so the code should factor this value to get the index of the two points and draw a line between them.
I think I did the factoring part right, I test it and it’s working but the problem here is that the component is giving me “Empty generic data parameter” instead of lines or curves!

this is my grasshopper definition you can check it
relationships.gh (5.4 KB)

Wow - you’ve got a nice project there.
I think line 12 should be curves.append(rs… ) instead of curves = rs…
I also think your while statement should count up to the total number of elements in your grid (900), ie rows x columns. I think it is currently stopping too soon (at 30). …

1 Like

Thank you Graham your comments were very helpful, I found that the main problem was that the relations list is a string list not integer and now the code is working thank you very much
Capture2

1 Like

Great!
If you like you can simplify your code as follows (apologies if there any typos - I havent tested id):

import rhinoscriptsyntax as rs

curves = []

for i, relation in enumerate(relations):
	if relation == "1":
		x, y = divmod(i, N)
		start = points[x]
		end = points[y]
		id1 = rs.AddLine(start, end)
		curves.append(id1)
		
a = curves

Hello, this is also an option

for i in range(N):
     for j in range(N):
         if relation[i*N+j]==1: