In this project, 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 are to do this project individually.
Suppose that each letter F moves the turtle forward 6 units. Because a level 0 Koch curve consists of a single F, this curve causes the turtle to move a total distance of 6 units and wind up 6 units forward from where it started. A level 1 Koch curve is formed by doing one replacement of F by F-F++F-F. Because this contains 4 of the letter F, the turtle moves a total distance of 24 units. However, because of the kink in the turtle's path, it winds up only 18 units forward from where it started, the equivalent of 3 of the letter F. Higher levels of Koch curves are formed by repeating the replacement of F by F-F++F-F. Each of these repeated replacements expands the turtle's total movement and its net forward motion the same way as the first replacement did. Calculate the total distance the turtle travels for a level 2 Koch curve. Do the same for level 3 and for level 4. Calculate how much forward from its starting position the turtle winds up at the end of a level 2 Koch curve. Do the same for level 3 and for level 4.
Download the file containing the lsystem function and the other functions it relies on. Use the lsystem function 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 (as explained above) the curve causes a net forward motion of 18 units.
Do exercise 9.30 (p. 327): Modify the drawLS function to support the G operation, which allows the turtle to go forward without drawing a line.
Do exercise 9.31 (p. 327): Using your modified drawLS function, try the L-system given there. Note that the exercise does not specify the angle to use. Figure out what an appropriate angle is by using the following reasoning about the rule for 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.) Figure out what an appropriate (non-zero) angle would be for each - sign, in order for F-F-F-GG to leave the heading unchanged. 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. Write a description in college-level English of what the drawing looks like. Your intended audience is other people who have read the same textbook.
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.
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.
All of your previous examples should still work, provided that you turn each rule's right-hand side into a single-element list. But you should also be able to use the following stochastic L-system (adapted from The Algorithmic Beauty of Plants by Przemyslaw Prusinkiewicz and Aristid Lindenmayer) that produces a plant-like form:
FF → F[-F]F[+F]FF → F[-F]FF → F[+F]F
As in task 5, a reasonable angle is 25. If you try this system out repeatedly, you should get varying results, but with a family resemblance between them.
For this project, send an email message to max@gustavus.edu that includes the following items:
The six distances you calculated in task 1, each identified with what it is and how you calculated it.
Your code that uses lsystem to carry out task 2. This should consist at a minimum of the actual function call, lsystem(...), but if you use any variables in that function call, you should also include the variables' definitions.
The code of your modified drawLS function from task 3.
Your code that uses lsystem to carry out task 4. This should consist at a minimum of the actual function call, lsystem(...), but if you use any variables in that function call, you should also include the variables' definitions.
Your English description of the drawing from task 4.
Your code that uses lsystem to carry out task 5. This should consist at a minimum of the actual function call, lsystem(...), but if you use any variables in that function call, you should also include the variables' definitions.
If you took the extra credit opportunity, the code of your modified applyProduction function.
If you took the extra credit opportunity, your code that uses lsystem for the example shown there. This should consist at a minimum of the actual function call, lsystem(...), but if you use any variables in that function call, you should also include the variables' definitions.
You will earn one point for each of the following accomplishments (with those after 20 being extra credit):
You correctly calculated and reported the total turtle path length for a level 2 Koch curve with moves of length 6.
You correctly calculated and reported the total turtle path length for a level 3 Koch curve with moves of length 6.
You correctly calculated and reported the total turtle path length for a level 4 Koch curve with moves of length 6.
You correctly calculated and reported the net forward motion for a level 2 Koch curve with moves of length 6.
You correctly calculated and reported the net forward motion for a level 3 Koch curve with moves of length 6.
You correctly calculated and reported the net forward motion for a level 4 Koch curve with moves of length 6.
You produced a drawing of a level 4 Koch curve, not necessarily centered but correct in all other regards.
You correctly centered the level 4 Koch curve so that it is symmetrical about the y axis.
You modified the drawLS function so that it recognizes the letter G.
Your modification to the drawLS function includes moving the turtle forward the appropriate distance.
Your modification to the drawLS function does not do any extraneous drawing.
Your modification to the drawLS function does not prevent subsequent letters from doing their proper drawing.
You chose an appropriate (non-zero) angle for task 4, so that when F is replaced, the replacement still leaves the turtle's heading unchanged.
Your code for task 4 is appropriate in all regards other than the turning angle.
Your description of the drawing from task 4 is sufficient that someone who has read our textbook would understand what the drawing looks like, at least so far as its general form goes. (That is, what sort of fractal is it?)
Your description of the drawing from task 4 provides some specific facts about the drawing's appearance that stem from the particular way you used the L-system, rather than from the L-system and turning angle. (For example, how detailed is the fractal? How large is it, perhaps described relative to the window? How is it oriented?)
Your description of the drawing from task 4 is written in college-level English.
Your code for task 5 produces a drawing using the specified L-system.
Your drawing for task 5 has appropriate specifics so that a plant-like form grows upward.
Your drawing for task 5 has appropriate specifics so that it stays within the window.
You modified applyProduction to randomly choose a replacement from a list of alternatives.
Your modified version of applyProduction still leaves alone symbols that don't have rules.
You show appropriate code for trying out the example stochastic L-system.