## L-Systems with Turtles## Lab 5## Due oct 29## moodle |

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!

Consider the following code (with Python and Turtle):

importturtledefdrawLsystem(aTurtle, instructions, angle, distance):forcmdininstructions:ifcmd =='F': aTurtle.forward(distance)elifcmd =='B': aTurtle.backward(distance)elifcmd =='+': aTurtle.right(angle)elifcmd =='-': aTurtle.left(angle)defmain(): t = turtle.Turtle()# create the turtlewn = 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 5wn.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'

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

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:

defcreateLSystem(numIters,axiom): startString = axiom endString =""foriinrange(numIters): endString = processString(startString) startString = endStringreturnendStringdefprocessString(oldStr): newstr =""forchinoldStr: newstr = newstr + applyRules(ch)returnnewstrdefapplyRules(ch): newstr =""ifch =='F': newstr ='F-F++F-F'# Rule 1else: newstr = ch# no rules apply so keep the characterreturnnewstr

'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.

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

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 |

Copyright © 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

Ideas, requests, problems regarding TWiki? Send feedback