CS 151: Project 9

Title image Project 9
Fall 2018

Project 9: Unique Trees and Shapes

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


  1. 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 we draw a tree, we first must build a string using an L-system. Then it can use the Shape draw method.

    Because we use an L-system to generate the string to draw, a Tree object must contain an L-system, which means it must have a field that holds an L-system object.

    Because a Tree is a Shape, it must 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 our Tree class, start by creating a file called tree.py. Import your lsystem and shape modules. The Tree class should be derived from the Shape class, but you'll 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, store the iterations number in an iterations field, then create an Lsystem object (passing in the filename) and store it in an lsystem field.
    • Create a setIterations(self, iterations) mutator method for the iterations field of the Tree object.
    • Create a read(self, filename) method that calls 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.
    • Override the draw method--but keep the same parameter list--so it uses the Lsystem to build the string, assigns the string to the string field of self, and then calls the parent draw method. You may want to change the default orientation for this function to 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, create a Tree object, and then use the Tree object's draw method to draw at least 3 trees. 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. 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. In a file named home.py, Create a new scene representing a scene near your home (or favorite place that isn't Colby). 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. Only the TurtleInterpreter class should execute turtle commands.

    The home scene is required image 3.

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

    Then make 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 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.

Extension Examples

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.

Hand-in and Report

  1. Put the python files you wrote on the Courses server in your private directory in a folder named Project9.
  2. Make a new wiki page for your assignment. Put the label cs151f18project9 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 cs151f18project9