Project 8: L-systems in Nature
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. If you want to move the turtle to a location, use the interpreter's place method. If you want to set the color, use the interpreter's setWidth 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.)
- Add a color stack to the TurtleInterpreter
It would be nice to be able to change the color of elements in an Lsystem without affecting the color of other elements. A good design will also avoid setting colors, widths, or other turtle drawing features by adding new parameters to drawString. Instead, modify the drawString method so that it handles five additional characters in the strings.
- Define < to push a color on the color stack
'<' - the left angle bracket should push the current turtle color onto a color stack. You need to create a separate colorstack variable similar to the stack 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() )
- Define > to pop a color off the color stack
'>' - the right angle bracket should pop the current turtle color off the color stack and set the turtle's color to that value using the turtle.color function.
- Define g as green
'g' - set the turtle's color to green (e.g. (0.15, 0.5, 0.2) ).
- Define y as yellow
'y' - set the turtle's color to light yellow ( e.g. (0.8, 0.8, 0.3) ).
- Define r as red
'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.
- Define < to push a color on the color stack
- Create a scene that is an arrangement of L-systems
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
The arrangement is required image 1.
Please indicate either in your report or in your code which L-systems you usee. 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.
- Create your own L-system inspired by "Art Forms in Nature"
Inspired by a print from "Art Forms in Nature" by Ernst Haeckel, make a new L-system of your own (base string and rule). 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 ABOP book chapter 1, pages 10, 11, and 25.
If you use a variation, include both the original and your variant in our report and explain how you changed it.
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.
The growth image is required image 2.
Please indicate either in your report 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!).
- What is a class?
- What is an object?
- What is the role of self in a class method?
- Given an object x how do you call its method y?
- Which Ernst Haeckel print did you choose as inspiration? Why?
Extensions are your opportunity to customize your project, learn something else of interest to you, and improve your grade. The following are some suggested extensions, but you are free to choose your own. Be sure to describe any extensions you complete in your report. Include pictures.
- 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 when printed.
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.
Submit your code
Turn in your code (all files ending with .py) by putting it the appropriate directory in the Courses server. You can access Courses through a browser at vpn.colby.edu. On the Courses server, you should have access to a directory called CS151, within that, a directory with your user name, and within that a directory named Private. Inside the Private directory there is a Projects directory with sub-directories for each week's project where you should submit your code. Please submit only code that you want to be graded.
When submitting your code, double check the following.
- Is your name at the top of each code file?
- Does every function have a comment or docstring specifying what it does?
- Is your handin project directory inside your Private folder on Courses?
Write Your Project Report
If you haven't already made a new page for this report on the wiki, then make one now (Log into the wiki, goto your Personal space by selecting "Personal Space" on the menu under the Person icon, then make the page using the "Create" button. Put the label
cs151s20project8 in the label field on the bottom of the page. But give the page a meaningful title.
Your intended audience for your report is your peers not in the class. From week to week you can assume your audience has read your prior reports. Your goal should be to be able to use it to explain to friends what you accomplished in this project and to give them a sense of how you did it.
Your project report should contain the following elements. Please include a header for each section.
A brief summary of the project, in your own words. This should be no more than a few sentences. Give the reader context and identify the key purpose of the assignment. Each assignment will have both a core CS purpose--such as using loops and conditionals--and an application such as making images in the style of an artist.
Writing an effective abstract is an important skill. Consider the following questions while writing it.
- Does it describe the CS concepts of the project (e.g. writing well-organized and efficient code)?
- Does it describe the specific project application (e.g. making images)?
- Does it describe your the solution or how it was developed (e.g. what code did you write)?
- Does it describe the results or outputs (e.g. did your code work as expected)?
- Is it concise?
- Are all of the terms well-defined?
- Does it read logically and in the proper order?
For each task, If you completed the task, include an image of the completed task and write a few sentences describing the image/what you did. This should be a description of the form and functionality of your final code. Note any unique computational solutions you developed or any insights you gained from your code's output.
If you didn't complete the task insert an image showing how far you got. Write a few sentences describing how much you completed, where you got stuck and what what solutions you tried.
You may want to incorporate code snippets in your description to point out relevant features. Code snippets should be small segments of code--usually less than a whole function--that demonstrate a particular concept. If you find yourself including more than 5-10 lines of code, it's probably not a snippet.
A description of any extensions you undertook, including text output or images demonstrating those extensions. If you added any modules, functions, or other design components, note their structure and the algorithms you used.
- Follow-up Questions
The answers to any follow-up questions (there will be 3-4 for each project).
A brief description (1-3 sentences) of what you learned. Think about the answer to this question in terms of the stated purpose of the project. What are some specific things you had to learn or discover in order to complete the project?
A list of people you worked with, including TAs and professors. Include in that list anyone whose code you may have seen, such as those of friends who have taken the course in a previous semester.
A list of any resources that you used. This includes textbooks, lecture notes, python documentation, library documentation or forums. If you used a specific website, please link to it.
- Put the label cs151s20project8 on your wiki page.