Fractal Tree: Recursion in a Calculator
One line segment, two smaller branches, two more from each – and after ten levels, a tree of 1,023 segments. REMcalc 1.1 supports functions that are called directly in an expression and can even call themselves. A fractal tree is the shortest way to see such recursion in action.
A function that calls itself
The whole tree is drawn by a single function, vetev(x, y, uhel, delka, hloubka) – Czech for
branch(x, y, angle, length, depth). It computes the end of the segment
x₂ = x + length · cos(angle), y₂ = y + length · sin(angle)
draws it with the line command and, unless it is already at the lowest level, calls itself twice: once
for a branch turned to the left and once for one turned to the right, both shorter (73% and 75% of the length). That is why
the tree has 1 + 2 + 4 + … + 512 = 2¹⁰ − 1 = 1,023 segments. The angle to the right is a fifth smaller than the angle
to the left, and the branches shrink unevenly – so the tree does not look as if drawn with a ruler, but more like a real one.
Why local
The function contains the line local x2, y2, t. Without it the tree would not come out right, and it is
worth knowing why. The function computes the end of its segment (x2, y2) and then calls the first sub-branch. But that
sub-branch computes its own end – and if x2 and y2 were ordinary shared variables, it would overwrite them. The second
sub-branch would then grow not from the end of the segment, but from the spot where the last twig of the first branch ended.
Each call of the function keeps its local variables to itself, and their previous values are restored on return.
Function parameters behave the same way. The variable pocet (count), on the other hand, is shared on purpose – it counts
the segments across all calls and also serves as the object number for line.
vetev with local variables
and recursion, line segments coloured by level and getting thinner towards the twigs.What is new here
- Functions in expressions.
t = vetev(x2, y2, …)– you call adef … enddeffunction just like a built-in function, even iniforprint. It returns its value by assigning to its own name (vetev = pocet). - Recursion. A function may call itself, up to a depth of 200 nested calls.
- Lines on the screen.
line number, x1, y1, x2, y2, colour, thicknessdraws a line segment in the object layer across the whole screen;movemoves it,removeremoves it.
Tasks from EMcalc that use the call statement work unchanged – calling a function in an expression is just another option.
Try it yourself
- Install REMcalc 1.1 (Download; the Free edition is enough).
- In the Window → Expression Library menu, choose the My Formulas library and click New… with the BASIC task box checked.
- Paste the program and run it. Try changing
rozvetveni(the branching angle), the length ratios 0.73 and 0.75, or the number of levels (the last number in the callvetev(0, 0, pi/2, 150, 10)) – and try deleting thelocalline.
Comments and variable names in the program are in Czech.
' Fraktální strom: funkce vetev kreslí úsečku
' a volá sama sebe pro dvě menší větve (rekurze)
mode rad
scene -480, 480, -40, 560
pocet = 0
def vetev(x, y, uhel, delka, hloubka)
local x2, y2, t
x2 = x + delka*cos(uhel)
y2 = y + delka*sin(uhel)
pocet = pocet + 1
line pocet, x, y, x2, y2, 125 - 11*hloubka, max(1, 1.3*hloubka)
delay 12
if hloubka > 1 then
t = vetev(x2, y2, uhel + rozvetveni, delka*0.73, hloubka - 1)
t = vetev(x2, y2, uhel - rozvetveni*0.8, delka*0.75, hloubka - 1)
endif
vetev = pocet
enddef
rozvetveni = 0.42
text 2000, -470, 545, "Fraktální strom – rekurzivní funkce vetev()", 22, white
n = vetev(0, 0, pi/2, 150, 10)
text 2001, -470, 510, "%g úseček", 18, gray; n
delay 2500