Project 8: Better Trees
As with last week, the assignment is to bring together the lsystem and turtle interpreter classes to make a scene that consists of fractal shapes and trees. Your top-level program will import both the lsystem and interpreter modules. Unlike last week, however, your scene.py may not import the turtle module or make any calls to turtle commands directly. If you want to draw something, you have to pass a string to the interpreter drawString method. There will be no exceptions to that rule. (Note that you may attach arbitrary meaning to any character not used by an L-system grammar.)
We want to be able to change the color of elements in an Lsystem
without affecting the color of other elements. We would also like to
avoid setting colors, widths, or other turtle drawing
features by adding new parameters to drawString. Instead, modify
your drawString method so that it handles five additional
characters in the strings.
'<' - the left angle bracket should push the current turtle color
onto a color stack. You'll need to create a separate colorstack
variable similar to the one used for position and heading. You can
use the function turtle.color() to get the turtle's current color.
Note that this function returns a tuple of colors. You should append
just the first element of the tuple onto the color stack.
colorstack.append( turtle.color() )
- '>' - the right angle bracket should pop the current turtle color off the color stack and set the turtle's color to that value.
- 'g' - set the turtle's color to green (e.g. (0.15, 0.5, 0.2) ).
- 'y' - set the turtle's color to light yellow ( e.g. (0.8, 0.8, 0.3) ).
- 'r' - set the turtle's color to red ( e.g. (0.7, 0.2, 0.3) ).
Be sure to use the turtle.color() and turtle.width() methods properly. They are turtle functions, and the new color or new width needs to be an argument to the function.
- '<' - the left angle bracket should push the current turtle color onto a color stack. You'll need to create a separate colorstack variable similar to the one used for position and heading. You can use the function turtle.color() to get the turtle's current color. Note that this function returns a tuple of colors. You should append just the first element of the tuple onto the color stack.
Create a file arrangement.py. Have the file import lsystem and
turtle_interpreter (the new versions). Write a function that makes
an arrangement of trees or flowers (note that an arrangement is more than random placement!). The scene should include some
multi-rule L-systems with leaves or other ornaments that make use
of the color store/restore. You can use the symbol L to
indicate a leaf, which could be drawn as a semicircle or a circle
or a line. The following are variations with leaves and flowers,
all defined by strings (no special characters
except L). Try to include L-system trees with different
numbers of iterations. (Note that at least two of the L-systems
you use must have at least 2 rules.)
systemCL systemDL systemEL systemFL systemGL
This is required image 1.
Please indicate either in your write-up or in your code which L-systems you use. If you happen to use command line arguments to specify filenames and don't tell us which systems you used, then we can't run your code to reproduce your image. So either hard-code which L-systems you use or indicate which ones you used to make the pictures in your report.
Make two new L-systems. They can be variations on one of the ones
provided, a variation on an L-system from the ABOP book, or one
you create on your own. The L-systems of interest are given in the
chapter 1, pages 10, 11, and 25.
If you use a variation, include both the original and your variant in your writeup. (The difference does not have to be large.)
In the file growth.py, create a scene function that makes an image using your L-systems after 2, 3, and 4 iterations. Note, if using a different pattern of iterations is more interesting (e.g. 4, 5, 6 or 2, 4, 6) you are free to use a different set of three iteration values. Indicate what you used in your writeup.
This is required image 2.
Please indicate either in your write-up or in your code which L-systems you used (and include the txt files for your new L-systems with your code when you turn it in!).
These are just examples, not necessarily recommendations. Please feel welcome and encouraged to design your own. A great extension genuinely interests you, pushes your understanding of CS, and inspires you to learn something new.
- Have each tree exhibit some variation by modifying aspects of how it is drawn.
- Add leaves, berries, or color to your trees by adding symbols to the rules and cases to your turtle interpreter. For each new symbol, you will need another elif case in your drawString method.
In the Lsystem class create a method def __str__(self) that
returns a nicely formatted string that might look like the following
base X rule X -> F-[[X]+X]+F[+FX]-X rule F -> FF
The __str__ method gets called automatically when an object is printed out. It's also called to implement casting an object to a string using str(thing). Note that your __str__ method should not print anything out. It should build a string from the information in your L-system and return the string. Remember that '\n' is how you create a newline character.
In the test main function the line:
should print out your nicely formatted string instead of the generic class instance string when you run your lsystem.py file.
See if you can draw the base string and rule below the image of a tree from that L-system.
- Get fancy with required images 2 or 3. "Fancy" means using programming structures (functions, loops, conditionals, lists, or math) to make a more interesting scene. Describe what you did in your report.
- Make more L-systems.
- Demonstrate that you can create other kinds of shapes (like squares, triangles, etc) by passing strings to the turtle interpreter's drawString method.
Hand-in and Report
- Put the python files you wrote on the Courses server in your private directory in a folder named Project8.
- Make a new wiki page for your assignment. Put the label cs151f18project8 on the page.
In general, your report should follow the outline below.
- Title includes your name and clearly describes the project.
- Section headings are used to delineate distinct sections of the report.
- Abstract identifies key lecture concepts (e.g. code structres, data types, and libraries) relevant to the project.
- Abstract explains why key lecture concepts are important to achieving project goals.
- Abstract identifies program output(s), giving context to the project tasks.
- Solutions to tasks are described, focusing on how you used key lecture concepts to solve each task.
- Required images/outputs are present and clearly labeled.
- Reflection at the end of the report addresses how the lecture concepts mentioned in the abstract made this project possible. If you can think of a more elegant way to achieve the same results, please share!
- Sources, imported libraries, and collaborators are cited, or a note is included indicating that none were referenced.
- Don't forget to label your writeup so that it is easy for others to find. For this project, use cs151f18project8