L-Systems with Turtles
Lab 5
Due oct 29
|  |
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
- Notice how the code follows the instructions in 'F-F++F-F' to direct the drawing.
- What are the parameters in drawLsystem(t, inst, 60, 30)?
- WHat happens if we double the instructions:'F-F++F-FF-F++F-F'?
- What happens if we take the string and embed it in the middle: 'F-F+'F-F++F-F+F-F'?
- What happens if we add junk: 'F-AFX+C+DFG-F'
- 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 - Some other systems
Try: (use 90 degrees)
L
L -> +RF-LFL-FR+
R -> -LF+RFR+FL-
This is called the "Hilbert curve"
Try the "dragon curve": (use 90 degrees)
FX
X -> X+YF+
Y -> -FX-Y
Try the "arrowhead curve": (use 60 degrees)
YF
X -> YF+XF+Y
Y -> XF-YF-X
Try the "Peano-Gosper curve": (use 60 degrees)
FX
X -> X+YF++YF-FX--FXFX-YF+
Y -> -FX+YFYF++YF+FX--FX-Y
Try the "Sierpinski Triangle": (use 60 degrees)
FXF--FF--FF
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!
Requirement | Points | Comments | Score |
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