Project 7: Fractals and Trees
The assignment is to build an interpreter that converts strings into turtle graphics drawings. The goal is to make a scene that consists of fractal shapes, trees, and other turtle graphics (think back to projects 1, 2 and 3). Your top-level program will include both the lsystem and turtle_interpreter modules.
Tasks
- Setup
Create a new file called turtle_interpreter.py. Put your name and date at the top in comments. The purpose of this file is to convert a string into an image using simple turtle commands.
The primary function in this file is drawString. The drawString function is an interpreter. It converts information in one form into information in another form. In this case, it converts a string of characters into a series of turtle commands.
- Write a drawString method
The drawString method takes in a string, a distance, and an angle as arguments. It should loop over the characters in the string and execute the turtle command specified by each character. The following are the actions for each character.
Character Action F Move the turtle forward by distance + Turn the turtle left by angle - Turn the turtle right by angle [ Append the turtle's heading and position to a list (stack) ] Pop the turtle's heading and position from the list (stack) and restore the turtle's state + (more detail)
The overall structure of the drawString method is given by the following algorithm.
def drawString( dstring, distance, angle ): """ Interpret the characters in string dstring as a series of turtle commands. Distance specifies the distance to travel for each forward command. Angle specifies the angle (in degrees) for each right or left command. The list of turtle supported turtle commands is: F : forward - : turn right + : turn left [ : save the turtle's heading and position ] : restore the turtle's heading and position """ # assign to stack the empty list # for each character c in dstring # if c is equal to 'F' # tell the turtle go forward by distance # else if c is equal to '-' # tell the turtle to turn right by angle # else if c is equal to '+' # tell the turtle to turn left by angle # else if c is equal to '[' # append to stack the position of the turtle (position method) # append to stack the heading of the turtle (heading method) # else if c is equal to ']' # tell the turtle to pick up pen # call the setheading method of the turtle and pass it the value popped off stack # call the goto method of the turtle and pass it the value popped off stack # tell the turtle to put down pen # call turtle.update() (not in the for loop)Use a list data structure to store the heading and position. For each [ character, add information to the end of the list using append. For each ] character, remove the position and heading information from the end of the list using pop. This makes a list behave like a data structure called a stack, so a good name for it is stack.
- Add a hold function
The function hold() is given below. It sets up the turtle window to quit if you type 'q' or click in the window. Copy it to your turtle_interpreter.py file.
def hold(): '''Holds the screen open until user clicks or presses 'q' key''' # Hide the turtle cursor and update the screen turtle.hideturtle() turtle.update() # Close the window when users presses the 'q' key turtle.onkey(turtle.bye, 'q') # Listen for the q button press event turtle.listen() # Have the turtle listen for a click turtle.exitonclick() - Test drawString
Each of the following files tests a different aspect of the drawString functin. Download and run each one, making sure the output is correct.
- makeShapes.py - this test code should draw a triangle, a square, and a pentagon.
- makeTree.py - this test code should draw a symmetric tree with left and right branches that each have left and right branches. The tree should be in a pot.
- testsimple.py - this test code draws a single L-system given the L-system file, distance, and angle. It uses 3 for the number of iterations. For example:
python3 testsimple.py systemA1.txt 20 90
python3 testsimple.py systemA2.txt 10 90
- testlsystem.py - this test code should use your L-system class to create several complex trees. You will also need systemB.txt. Run this with the command:
python3 testlsystem.py systemB.txt 10 22.5
You can also use this test function with the other L-system files.
- Create an abstract scene
Create a file called abstract.py. The file will need to import sys, turtle, lsystem, and turtle_interpreter. Write a function that creates an abstract image using L-systems. This image should be constructed to take advantage of your Python programming skills--don't rely wholly on the random package and a loop. Your goal should be complexity, yet order in your image and simplicity in your code. One idea is to make an interesting pattern by drawing the same L-system in different positions. Using a hierarchy of functions is also a powerful tool.
Your image should include at least three different L-systems, with at least one of them using brackets. Don't feel beholden to use the suggested number of iterations or angles for any L-system. You can get the filenames for the L-system files from the command line, by asking the user for them, or by hard-coding them into your code.
In your image function, you can use turtle commands to pick up the pen, move it to a new location, change colors, change pen widths, and put down the pen before drawing a new shape. Look at the testlsystem.py test file above for inspiration.
A picture with 3 different L-systems is required image 1.
- Make a grid of L-system trees
Make a new file grid.py that contains a function that draws a set of 9 trees based on the systemB L-system, or some variation of it that has brackets. Order the 9 trees as a 3x3 grid. From left to right the number of iterations of the L-system should go from 1 to 3. From top to bottom, the angle of the L-system should be 22, 46, and 60. Use a double for-loop to create the grid.
A picture with a grid of L-systems is required image 2.
- Make a scene that includes L-systems
Make a new file scene.py that makes a non-abstract scene with two or more objects generated using L-systems. The scene must include at least one new L-system with brackets (e.g. a tree) that you haven't used yet. You can use one of the L-systems from ABOP (look at pages 9, 10, and 25 for single-rule L-systems) or make up one of your own. The scene does not need to be complex, but your code should exhibit modularity and good design.
A scene that includes 2 different L-systems is required image 3.
Follow-up Questions
- What is your CS understanding of an interpreter?
- What is an L-system (keep it brief)?
- What does it mean to loop over the characters in a string?
- What is the purpose of using a stack in drawString?
Extensions
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.
- Import one of your scenes from project 2 or 3 and add trees or fractal shapes to them. It's all turtle graphics, after all.
- Make your abstract image function take in (x, y, scale) as parameters and demonstrate you can properly translate and scale the abstract image by including multiple copies, at different locations and scales, in one scene.
- Make task 2 more interesting by adding additional elements to the image that also change across the grid. For example, make the trees move from summer to fall to winter along the horizontal or vertical axis.
- Give the function for task 2 the parameters (x, y, scale) and demonstrate you can properly translate and scale the grid.
- Create an L-system of your own that draws something interesting. Start by editing existing L-systems either from the lab or from the Algorithmic Botany Plants book.
- Add leaves, berries, or color to your trees by adding new alphabet symbols to the rules and cases to your turtle_interpreter. For each new symbol you use in a rule, you will need another elif case in your drawString function.
Submit your code
Turn in your code (all files ending with .py) by putting it in a directory in the Courses server. On the Courses server, you should have access to a directory called CS151, and within that, a directory with your user name. Within this directory is a directory named private. Files that you put into that private directory you can edit, read, and write, and the professor can edit, read, and write, but no one else. To hand in your code and other materials, create a new directory, such as project1, and then copy your code into the project directory for that week. 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 cs151f19project7 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.
-
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.
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?
- Does it describe your the solution or how it was developed (e.g. what code did you write/circuits did you build)?
- 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?
- A description of your solution to the tasks, including images you created (including the required images or videos mentioned above). 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.
- 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.
- 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.
- Put the label cs151f19project7 on your wiki page.
Project 7