Python - Recursion error. Fractal does not want to multiply

Hello Everyone,

I am a python beginner and I am having hard time to make recursion working. I want to create rectangle fractal which will multiply itself as it goes smaller and smaller.

but for some reason it does not want to multiply itself. Can anybody give me an advise? Here is the code. Thank you for the help.

import rhinoscriptsyntax as rs
import math
import Rhino as r

Fractal1 =
pts =
Fractals =

def Fractal_split(rec,repetition,p):

explodedCurves = rs.ExplodeCurves(rec,False)
for curve in explodedCurves:
    parameter = rs.CurveParameter(curve,p)
    point_on_curve = rs.EvaluateCurve(curve, parameter)
    pts.append(point_on_curve)
A = pts[0]
B = pts[1]
C = pts[2]
D = pts[3]
Fractal1 = rs.AddPolyline([A,B,C,D,A])

if (repetition > 0):
    repetition -= 1
    Fractal_split(Fractal1,repetition,p)

else:
    Fractals.append(Fractal1)

Fractal_split(rec,repetition,p)

Hi Lelesong,

After each recursion you need to add the result to your “Fractals” list:

import rhinoscriptsyntax as rs

Fractals = []

def Fractal_split(rec,repetition,p):
    
    if (repetition > 0):
        explodedCurves = rs.ExplodeCurves(rec,False)
        pts = []
        for curve in explodedCurves:
            parameter = rs.CurveParameter(curve,p)
            point_on_curve = rs.EvaluateCurve(curve, parameter)
            pts.append(point_on_curve)
        A = pts[0]
        B = pts[1]
        C = pts[2]
        D = pts[3]
        Fractal1 = rs.AddPolyline([A,B,C,D,A])
        Fractals.append(Fractal1)
        
        
        repetition -= 1
        Fractal_split(Fractal1,repetition,p)


Fractal_split(rec,repetition,p)
1 Like

Hello djordje,

thanks a lot for your guidance!