L-Systems with Turtles

Lab 5

Due Feb 28
moodle
Screen_Shot_2019-02-12_at_10.10.37_AM.png

Goal

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':
            aTurtle.forward(distance)
        elif cmd == 'B':
            aTurtle.backward(distance)
        elif cmd == '+':
            aTurtle.right(angle)
        elif cmd == '-':
            aTurtle.left(angle)

def main():
    t = turtle.Turtle()            # create the turtle
    wn = turtle.Screen()
    inst='F-F++F-F'
    t.up()
    t.goto(-200,0)
    t.down()

    t.speed(9)
    drawLsystem(t, inst, 60, 30)
    # draw the picture
    # angle 60, segment length 5
    wn.exitonclick()

main()

https://repl.it/@JimSkon/LSystemWithStart

  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
    else:
        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 - 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!

RequirementPointsCommentsScore
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
Total
Solution
Topic revision: r1 - 2019-02-12 - JimSkon
 
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2019 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback