B2.4.4–5 · HL recursion
Fractal tree · HL application
Python · step by step
Fractal tree · HL application
The same drawing rule repeats at a smaller scale until depth reaches zero.
Python implementation
1import math2segments = []34def fractal(x, y, length, angle, depth):5 if depth == 0:6 return7 end_x = x + length * math.cos(angle)8 end_y = y - length * math.sin(angle)9 segments.append([x, y, end_x, end_y])10 fractal(end_x, end_y, length * 0.65, angle + 0.5, depth - 1)11 fractal(end_x, end_y, length * 0.65, angle - 0.5, depth - 1) B2.4.4 application: focus on self-similarity, smaller subproblems and the stopping condition. Branching code is illustrative, not a B2.4.5 tracing requirement. math is Python’s standard maths module; angles are in radians. segments records endpoints for the drawing. Every branch creates two smaller branches.
Keep exploring
Return to the recursion lesson →
All B2.4 animations →