MCS-177 L-Systems activity (Spring 2017)

Thursday, 4/20 and Friday, 4/21


Overview

In this in-class activity, you will experiment with L-systems, which use grammatical rules to generate strings that can be interpreted as instructions for drawing fractals. The required portions of this project include only a very modest amount of programming; the emphasis is instead on thinking about what grows and what stays the same as the string of instructions is expanded.

You may choose to do this activity alone or with a parner. You're not expected to finish all problems during one class session. Start from the beginning and work your way down.

Reading and Routine exercises

  1. Download the file lsys.py containing the procedures called applyProduction, drawLS, and lsystem. Save this file in your working directory as lsys.py. Now type F5 to run the module lsys.
  2. Note that every task that you do for this activity on your local IDLE should use cTurtle which is installed in all lab machines (instead of turtle).
  3. After skimming Section 9.4.1 L-systems p. 318-320 (review), do Exercises 9.20 and 9.22 (your drawings should look like what you saw during lecture on Wednesday).

  4. Read the online textbook page 9.15. Turtles and Strings and L-Systems and run the sample code boxes. See also textbook Section 9.4.2 Automatically Expanding Production Rules p. 321-322 (review). Do Exercise 9.27 for levels 3 and 4 or Exercises 9.28-9.29.

  5. Read Section 9.4.3 More Advanced L-systems p. 323-326 (NEW). In particular, read the code for the lsystem function in Listing 9.8 p. 325. Alternatively, read online textbook page 10.15. The Return of L-Systems and run the sample code boxes (don't worry if they run for too long).

  6. After running the module lsys, try the commands from p. 326 Session 9.2. What picture do you get?
  7. Try running the following commands:
    >>> res = applyProduction("X",{"X":"F[-X]+X", "F":"FF"},4)
    >>> res
    >>> drawLS(Turtle(),res,45,10) # close the window after the drawing finishes
    
    >>> drawLS(Turtle(),res,30,10) # close the window after the drawing finishes
    
    >>> lsystem('X', {"X":"F[-X]+X", "F":"FF"}, 5, (0,-280), 90, 30, 15)
    
  8. Now produce the sprig of rosemary shown in p. 326 Fig. 9.11 using the axiom and rules given.

Before the end of class, show your instructor one of the drawings you produced locally.

Less-routine problems

  1. Use the lsystem function from the lsys module to draw a level 4 Koch curve with each line segment having a length of 6 units. You should set the initial heading to 0 (that is, horizontal, pointed to the right) and set the initial position to a point on the negative x axis that causes the curve to be centered horizontally. For example, if you were doing a level 1 curve, you would use an initial position of (−9, 0) because the curve causes a net forward motion of 18 units.

  2. Do exercise 9.30 (p. 327): Modify the drawLS function in lsys to support the G operation, which allows the turtle to go forward without drawing a line.

  3. Consider the L-system given in exercise 9.31 (p. 327).

  4. Do exercise 9.32 (p. 327): Use the L-system given there and the specified angle of 25 in order to create a drawing that looks like a plant growing upward. You will need to use some trial and error to choose appropriate values for the depth as well as the initial position and heading and the length of the individual line segments. Your goal is to produce an interesting drawing that stays within the available space.

  5. Write a new recursive function called applyProductionRecursive with the same contract (the same input arguments and output) and behavior as applyProduction. Test that applyProductionRecursive works as well as the original applyProduction. Show your work to your instructor.
Before the end of class, show one of your drawings from #1,3,4 to your instructor.

Creative problems

  1. Change the applyProduction function so that the dictionary of rules maps each symbol not to a single replacement string, but to a list of alternative strings, one of which is randomly chosen each time a replacement needs to be performed. (You can randomly choose an element of a list using the choice function within the random module.) As in the original version of applyProduction, some symbols may not be listed in the rules at all, in which case they should be left unchanged.

  2. Pick two sets of L-system rules that are not described in the textbook or this project. Write Python expressions that will generate drawings of L-systems following your rules.

Bonus fun