Example: Fractal Tree

Turtle Graphics

This is a very short and simple example for turtle graphics. We use recursion so as to draw a fractal binary tree/cauliflower.

The Source Code

# EXAMPLE FOR A FRACTAL TREE USING THE TURTLE
#
# In this program we simply paint a fractal tree
# (cauliflower) using the turtle.
#
# Jan-05-2014
#
# (c) 2014, Tobias Kohn
# http://jython.tobiaskohn.ch/
#
from gturtle import *
from tjaddons import makeRainbowColor

# Choose an angle between 10 and 90
ANGLE = 30

def makeTree(s, depth = 4):
    setPenColor( makeRainbowColor(depth) )
    if s < 64:
        penWidth(1)
    else:
        penWidth(2)
    forward(s)
    if s >= 1:
        x = getX()
        y = getY()
        hd = heading()
        left(ANGLE)
        makeTree(2*s/3, depth+1)
        setPos(x, y)
        heading(hd)
        right(ANGLE)
        makeTree(2*s/3, depth+1)
    
### MAIN ###
makeTurtle()
hideTurtle()
clear("black")
setPos(0, -300)
makeTree(200)