# MCS-177 Project 8: L-Systems

### Overview

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.

1. 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 the curve causes a net forward motion of 18 units.

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

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

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

Axiom
`F`
Alternative Rules
`F` → `F[-F]F[+F]F`
`F` → `F[-F]F`
`F` → `F[+F]F`

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.

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

### Extra Credit

• Write the contract and recursive implementation for a procedure `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. 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):

1. You produced a drawing of a level 4 Koch curve, not necessarily centered but correct in all other regards.

2. You correctly centered the level 4 Koch curve so that it is symmetrical about the y axis.

3. You modified the `drawLS` function so that it recognizes the letter `G`.

4. Your modification to the `drawLS` function includes moving the turtle forward the appropriate distance.

5. Your modification to the `drawLS` function does not do any extraneous drawing.

6. Your modification to the `drawLS` function does not prevent subsequent letters from doing their proper drawing.

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

8. Your code for task 3 is appropriate in all regards other than the turning angle.

9. 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?)

10. 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?)

11. Your description of the drawing from task 3 is written in college-level English.

12. Your code for task 4 produces a drawing using the specified L-system.

13. Your drawing for task 4 has appropriate specifics so that a plant-like form grows upward.

14. Your drawing for task 4 has appropriate specifics so that it stays within the window.

15. You modified `applyProduction` to randomly choose a replacement from a list of alternatives.

16. Your modified version of `applyProduction` still leaves alone symbols that don't have rules.

17. You show appropriate code for trying out the example stochastic L-system.

18. You show appropriate code for trying out a L-system of your choice.

19. You show appropriate code for trying out a L-system of your choice. (a different L-system from #18)

20. Your procedures are not unnecessarily complex

21. You wrote a correct contract for `sortList`
22. Your `sortList` sorts the list in ascending order using recursion.