Project 7: Fractals and L-systems
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 Video 2: Storing and restoring the turtle 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.
Note: if you have import turtle as t then you need to change all of the instances of "turtle." to "t." in the code below.
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 function. Download and run each one, making sure the output is correct.
- test7-1.py - draws a blue triangle, a red square, and a green pentagon.
- test7-2.py - draws a symmetric tree with left and right branches that each have left and right branches. The tree should be in a square pot with legs.
- test7-3.py - draws a single L-system given the L-system file, distance, and angle. It uses 3 for the number of iterations. For example run it with the following command line arguments:
python3 test7-3.py systemA1.txt 20 90
python3 test7-3.py systemA2.txt 10 90
- test7-4.py - uses your L-system class to create several complex trees. Download systemB.txt and run it with the command:
python3 test7-4.py systemB.txt 10 22.5
The result should look like this. You can also use this test function with the other L-system files.
- Create an abstract scene
Video 3: Creating 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 that includes three L-systems. The phrase "abstract image" just means it does not have to make any attempt to be realistic. The 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 visual complexity combined with order, 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.
Include at least three different L-systems in your image, 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 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. Include in your scene 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 non-abstract 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?
- Can you think of a movie where you were impressed by the computer generated trees?
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 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 cs15120project7 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.
- Abstract
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?
- Tasks
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.
- Extensions
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).
- Reflection
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?
- References/Acknowledgements
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 cs151s20project7 on your wiki page.
Project 7