# CS 151: Project 9

Project 9
Spring 2020

### Project 9: Unique Trees and Mosaics

The project this week is to continue to make shape classes, making use of inheritance, starting with a tree class.

1. Make a Tree class

The first task is to make a Tree shape class, similar to the Square and Triangle classes. The difference between a Tree and a Square, though, is that the Tree generates its string dynamically using an L-system. The string for a Square is always the same, so it can set the string field once and then use the parent Shape class' draw method. However, every time the Tree class draws a tree, it should first build a new string using an L-system. Then it can call the Shape class draw method.

The Tree class needs to use an L-system to generate the string to draw, so a Tree object must contain a field that holds an L-system object.

Because a Tree is a Shape, it should be a child of the Shape class. That lets it use the parent methods for setting color, distance, and angle, among other things.

To make the Tree class, start by creating a file called tree.py. Import the lsystem and shape modules. The Tree class should be derived from the Shape class, but it will need to override some of the methods because of the special nature of a Tree: it needs more fields than a simple Shape, and it has to dynamically create the string it will draw using an L-system.

The methods you'll need to override or create for the Tree class include:

• def __init__(self, distance=5, angle=22.5, color=(0.5, 0.4, 0.3), iterations=3, filename=None):

The init method should call the parent (Shape) init method with self, distance, angle, and color. It should assign the iterations value to an iterations field of the object (self). Finally, it should create an Lsystem object (passing in the filename) and assign it to an lsystem field of self.

• def setIterations(self, N):

Given a new number of iterations in N, assign it to the iterations field of the object.

The read(self, filename) method should call the lsystem object's read method with the specified filename. Use the Lsystem object you created and stored in the lsystem field to call the read method.

• draw(self, xpos, ypos, scale=1.0, orientation=90):

Override the draw method--but keep the same parameter list. Use the draw method use the Lsystem to build a string. Then assign the string to the string field of self and call the parent class (Shape) draw method. Note the suggested default orientation for this function to be 90 so the trees grow up.

Once you've written the tree class, make a test method for the class and try it out. The test method should take in an Lsystem filename as a parameter, create a Tree object, and then use the Tree object's draw method to draw at least 3 trees in different locations. Use an L-system with multiple replacements for at least one rule (e.g. systemJ.txt and show the three trees are different.

The output of your tree.py test method is required image 1.

2. Create three more shape classes

In shapes.py, create at least three classes--other than Square and Triangle--that are derived from the Shape class and define different shapes using strings. One of them should make a filled shape using curly brackets { and } to turn on and off the fill. Make a test function for your shapes.py file that generates an image that incorporates all of the shapes you created. The function should test all of the capabilities of the different shape classes.

The output of your shapes.py test method is required image 2.

3. Create a scene of a place outdoors where you like to be

In a file named place.py, create a new scene representing a place outdoors that is meaningful to you. Give your scene a title in your report. Use your various shape classes and include at least one tree in the scene. Use only the Tree and shape classes from this assignment to create the scene, not your turtle code from prior assignments. Use the TurtleInterpreter only to call the hold method. Only the TurtleInterpreter class should execute turtle commands.

The outdoor scene is required image 3.

4. Create a grid of tiles

Loosely inspired by MC Escher or Islamic Mosaics, create a grid of 20 tiles.

In a file named mosaic.py, create a function tile(x, y, scale) that draws a set of shapes inside a square that is scale by scale in size with the lower left corner of the tile at location (x, y). If scale is 10, then the tile should be 10x10.

Define a function mosaic(x, y, scale, Nx, Ny) that draws a 2D array of tiles Nx by Ny, where each tile is of size scale by scale, and the lower left corner of the mosaic is at (x, y). So if scale is 10, Nx is 3, and Ny is 4, the function should draw twelve 10x10 tiles in a grid three across and four down.

Just like in the previous task, use only the Tree and shape classes from this assignment to create the scene, not your turtle code from prior assignments. Only the TurtleInterpreter class should execute turtle commands.

An image of at least 20 tiles (5 x 4) in a non-square arrangement is required image 4.

### Follow-up Questions

1. What is inheritance?
2. What does it mean for a child class to override a method?
3. What is a class variable or class global variable?
4. What is a field of an object?
5. Which image or artwork did you choose as inspiration? Why?

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

• Make non-square tiles. Rectangles are easy, hexagons, triangles, or n-gons are a real extension.
• Make new L-systems and add characters to the vocabulary that do interesting things.
• Modify drawString so that when drawing a tree the branches droop down like gravity is pulling at them.
• Create a sequence of images to build an animation.
• Make more tile functions and mix them around in the mosaic function.
• Make more shape classes that do interesting things. Making a fixed sequence of characters is easy. Make a shape class where the strings are the result of executing a function. L-systems are one example of a dynamically created string, but there are many other ways to do that.
• Develop your own extension. If you choose to do this, explain what you are trying to accomplish and then how you implemented your solution. Show the result.

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.

1. Is your name at the top of each code file?
2. Does every function have a comment or docstring specifying what it does?
3. Is your handin project directory inside your Private folder on Courses?

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

• 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?

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 cs151s20project9 on your wiki page.