Generate and draw a fractal tree.

 Draw the trunk
 At the end of the trunk, split by some angle and draw two branches
 Repeat at the end of each branch until a sufficient level of branching is reached


Related tasks
 Pythagoras Tree


' version 17-03-2017
' compile with: fbc -s gui
 
Const As Double deg2rad = Atn(1) / 45
Dim Shared As Double scale = 0.76
Dim Shared As Double spread = 25 * deg2rad ' convert degree's to rad's
 
Sub branch(x1 As ULong, y1 As ULong, size As ULong, angle As Double, depth As ULong)
 
    Dim As ULong x2, y2
 
    x2 = x1 + size * Cos(angle)
    y2 = y1 + size * Sin(angle)
 
    Line (x1,y1) - (x2,y2), 2  ' palette color green
    If depth > 0 Then
        branch(x2, y2, size * scale, angle - spread, depth -1)
        branch(x2, y2, size * scale, angle + spread, depth -1)
    End If
 
End Sub
 
' ------=< MAIN >=-----
 
Dim As Double angle = -90 * deg2rad ' make sure that the tree grows up
Dim As ULong  SizeX = 800
Dim As ULong  SizeY = SizeX * 3 \ 4
Dim As Double  size = SizeY \ 4
Dim As ULong  depth = 11
 
ScreenRes SizeX, SizeY, 8
WindowTitle ("Fractal Tree")
 
branch(SizeX\2, SizeY, size, angle, depth)
 
' empty keyboard buffer
While InKey <> "" : Wend
windowtitle ("Fractal Tree, hit any key to end program")
Sleep
End