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.
applyProduction
, drawLS
, and lsystem
.
Save this file in your working directory as lsys.py.
Now type F5 to run the module lsys.
cTurtle
which is installed in all lab machines (instead of turtle
).
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).
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.
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).
>>> 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)
Before the end of class, show your instructor one of the drawings you produced locally.
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.
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.
Consider the L-system given in exercise 9.31 (p. 327).
lsystem
with your modified drawLS
function, try the L-System given in the exercise.
Note that the exercise
does not specify the angle to use.
Go ahead and try this with several different non-zero angles (such as 60, 90). Compare your graphics with your neighbor's.
F
. If the F
isn't replaced, then it would leave the turtle's heading unchanged. Therefore, it is reasonable
to assume that the right hand side of the rule should also leave the turtle's heading unchanged. (That is, the heading should wind up
the same at the end as at the beginning, even if it changes along the way.)
If this paragraph is not clear to you, ask an instructor or lab assistant, or go ahead and read the next item.
F
with F-F-F-GG
.
You will also 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.
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.
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.
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.
F
F
→ F[-F]F[+F]F
F[-F]F
F[+F]F
As in the previous problem, a reasonable angle is 25. If you try this system out repeatedly, you should get varying results, but with a family resemblance between them.
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.
sortList
that takes a list of numbers and produces the given list in ascending order. Start by thinking about base case(s). For the general case, reduce the size of the problem by taking one number from the list so that you can recurse on a smaller list.
NOTE: The only list operations you may use are indexing, slicing, concatenation and len. This means you may NOT use append
, pop
, min
, max
, remove
, insert
, etc.
>>> sortList([5,3,4,2,10]) [2,3,4,5,10]
(Hint: You can write more procedures to help sortList
. However, your helper procedures must be recursive as well.)