Problem with recursive function RhinoPython


#1

Hi everybody,
I’m facing some problem while trying to apply this code .
the branching only occurs in one side, how to make it enabled in both sides like a tree ?

import rhinoscriptsyntax as rs
import random

def lines(point, iterations, scale):
    while iterations>0:
        xpos = scale*30
        ypos = scale*30
        for i in point:
            newpoint =(xpos, ypos, 0.0)
            newpoint2 =(-xpos, ypos, 0.0)
            vector = rs.VectorAdd(i, newpoint)
            vector2 = rs.VectorAdd(i, newpoint2)
            line = rs.AddLine(i, vector)
            line = rs.AddLine(i, vector2)
            newline=[]
            newline.append(vector)
            newline.append(vector2)
        return lines(newline, iterations-1, scale*0.5)

thepoint = rs.GetPoint("select the point")
rs.AddPoint(thepoint)
test = []
test.append(thepoint)
lines(test, 20, 2)

thanks a lot


#2

I have solved the problem.
the line newline=[] should go before the for loop.

<img src="/uploads/default/19183/29295e68eae2d3f0.JPG" width="626" height="500"> import rhinoscriptsyntax as rs
import random



def lines(point, iterations, scale):
    
    while iterations>0:
        newline=[]
        xpos = scale*30
        ypos = scale*30
        for i in point:
            newpoint =(xpos, ypos, 0.0)
            newpoint2 =(-xpos, ypos, 0.0)
            vector = rs.VectorAdd(i, newpoint)
            vector2 = rs.VectorAdd(i, newpoint2)
            line = rs.AddLine(i, vector)
            line = rs.AddLine(i, vector2)
            newline.append(vector)
            newline.append(vector2)
        return lines(newline, iterations-1, scale*0.5)

thepoint = rs.GetPoint("select the point")
rs.AddPoint(thepoint)
test = []
test.append(thepoint)
lines(test, 10, 2)