Tags: %TAGME{ tpaction="" web="Main" tag="" }% view all tags

L-Systems with Turtles

Lab 5

Due oct 29


The goal of this lab is to work with strings, and see how we can create a language to control the computer.

Or - Fun with fractals!

Step 1 - Using a string to draw

Consider the following code (with Python and Turtle):

import turtle
def drawLsystem(aTurtle, instructions, angle, distance):
    for cmd in instructions:
        if cmd == 'F':
        elif cmd == 'B':
        elif cmd == '+':
        elif cmd == '-':

def main():
    t = turtle.Turtle()            # create the turtle
    wn = turtle.Screen()

    drawLsystem(t, inst, 60, 30)
    # draw the picture
    # angle 60, segment length 5



  1. Notice how the code follows the instructions in 'F-F++F-F' to direct the drawing.
  2. What are the parameters in drawLsystem(t, inst, 60, 30)?
  3. WHat happens if we double the instructions:'F-F++F-FF-F++F-F'?
  4. What happens if we take the string and embed it in the middle: 'F-F+'F-F++F-F+F-F'?
  5. What happens if we add junk: 'F-AFX+C+DFG-F'
  6. Try this: 'F-F+BB-BB++F-F'

Step 2 - Adding commands

Could you had more commands? Say a "S" that saves the position, and "R" that returns to the last saved position?

Add them and try: SF-F++F-FR-F-F++F-FR-F-F++F-FR-F-F++F-FR-F-F++F-FR-F-F++F-F

Step 3 - Embedding new commands

What if we start with the string "F", and replace all the F's with 'F-F++F-F', getting 'F-F++F-F'?

Then we replace the F's in this string with 'F-F++F-F' again, getting 'F-F++F-F-F-F++F-F++F-F++F-F-F-F++F-F'?

What if we do it again? Now it would get challenging!

Can we write code that replaces every "F" with 'F-F++F-F' as many times as we want?

Review and try the following code:

def createLSystem(numIters,axiom):
    startString = axiom
    endString = ""
    for i in range(numIters):
        endString = processString(startString)
        startString = endString
    return endString

def processString(oldStr):
    newstr = ""
    for ch in oldStr:
        newstr = newstr + applyRules(ch)

    return newstr

def applyRules(ch):
    newstr = ""
    if ch == 'F':
        newstr = 'F-F++F-F'   # Rule 1
        newstr = ch    # no rules apply so keep the character

    return newstr

'numIters' determines how many iterations deep to go with the replacements. Try this with 1, 2, 3, 4.

Note that the output gets BIG as the depth increases. Modify the call to drawLsystem(t, inst, 60, 30) so that it divides the line size by the # of levels so the graph stays the same size.

Step 4 - Some other systems

Try: (use 90 degrees)

L -> +RF-LFL-FR+
R -> -LF+RFR+FL-

This is called the "Hilbert curve"

Try the "dragon curve": (use 90 degrees)

X -> X+YF+
Y -> -FX-Y

Try the "arrowhead curve": (use 60 degrees)

X -> YF+XF+Y
Y -> XF-YF-X

Try the "Peano-Gosper curve": (use 60 degrees)


Try the "Sierpinski Triangle": (use 60 degrees)

F -> FF
X -> --FXF++FXF++FXF--

Or try (From Judy Holdener) F F -> F- - -> -F

Step 5 - Do you own thing

Here is where things get interesting! Come up with at least 2 additional commands, and create your own L-System of rewrites. For example you could add a command to change the color, such R for red, G for green. Or you could have N for next color, and have a set of colors you cycle through. See if you can come up with a really cool drawing!

Program provides the required function (2 additional command, nice L-System code). 40
Code is properly broken up into functions, with one specific task per function. See instructions above. 30
Each function is commented at top of function 10
Program has meaningful comments, good variable names, and good formatting 10
The program has a nice user interface - easy to use. 10


Edit | Attach | Watch | Print version | History: r3 < r2 < r1 | Backlinks | Raw View | Raw edit | More topic actions...
Topic revision: r1 - 2020-01-23 - JimSkon
  • Edit
  • Attach
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2020 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback