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.
Download the file containing the
lsystem function and the other functions it relies on.
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 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
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. 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
- 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.
applyProductionfunction 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
choicefunction within the
randommodule.) 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.
As in task 4, 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.
sortListthat 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
insert, etc. If you are not sure what can and cannot be used, please check with the instructors.
>>> 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.)
You will be submitting your code using Moodle; click on the following link for instructions on submitting code using Moodle. For this project, you will need to submit the following files:
lsystems.py, which contains all of your work, including procedures you wrote and procedure calls you made to accomplish the required tasks. You can include procedure calls in comments.
You will earn one point for each of the following accomplishments (with those after 20 being extra credit):
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
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 3, so that when
F is replaced, the replacement still leaves the turtle's heading unchanged.
Your code for task 3 is appropriate in all regards other than the turning angle.
Your description of the drawing from task 3 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 3 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 3 is written in college-level English.
Your code for task 4 produces a drawing using the specified L-system.
Your drawing for task 4 has appropriate specifics so that a plant-like form grows upward.
Your drawing for task 4 has appropriate specifics so that it stays within the window.
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.
You show appropriate code for trying out a L-system of your choice.
You show appropriate code for trying out a L-system of your choice. (a different L-system from #18)
Your procedures are not unnecessarily complex
sortListsorts the list in ascending order using recursion.