CS 151: Artistic Rendering

Artistic Rendering

The purpose of this lab is to give you experience building a class and using it to enable simple modifications to a working system. In this case, we're going to give the turtle the ability to draw different kinds of lines. The end result will produce pictures that look like they've been drawn in crayon.


The basic idea is that we're going to create a class that acts like a Zelle Line but that looks like someone draw the line using small strokes of crayon. In order to make life easier, we're going to use the aggregateBase class that we went over in class when we looked at inheritance. The Crayon class should inherit the aggregateBase class, which means you get all of the code in the parent class for free.

  1. Download the file aggregateBase.py. This will be the parent class for our Crayon class. Import aggregateBase (from aggregateBase import *) into the zturtle.py file.
  2. In zturtle.py, above the ZTurtle class define a new class called Crayon that inherits aggregateBase.

    class Crayon(aggregateBase):

    The only methods you need to define in Crayon are the __init__ method and the setWidth method. The __init__ method takes self and two Point objects (e.g. p1 and p2) as arguments, just like a Line.

    One way to simulate crayon is to have the __init__ method create a pair of lines that approximate the straight line between p1 and p2. One algorithm for the __init__ method is as follows (you can vary things around).

    __init__(self, p1, p2)
        # Calculate the midpoint between the two points, put the values in midx, and midy
        # Calculate a starting Point pstart by randomly perturbing p1
        # Calculate a first mid Point pmidA by randomly perturbing midx and midy
        # Create a Line from pstart to pmidA
        # Calculate a second mid Point pmidB by randomly perturbing midx and midy
        # Calculate the end Point pend by randomly perturning p2
        # Create a Line from pmidB to pend
        # Set the field self.anchor to a Point located at (midx, midy)
        # Set the self.shapes field to a list containing the two lines
        # Set the self.scale field to 1  (to make AggregateBase happy)
        # return

    The setWidth method should be as follows:

    def setWidth(self, w):
        # for all of the objects in self.shapes
            # calculate a random value between 1 and twice w
    	# set the width of the object using the setWidth method
  3. The final step is to modify your ZTurtle class to use a Crayon instead of a Line. There are three things you need to do.
    • In the __init__ method, create a field icrayon and set it to False.
    • In the forward method you create a Line object if the pen is down. If the icrayon field is True, we want to make a Crayon object instead.
      if the pen is down
          Put the current turtle position in a Point p1
          Put the new turtle position in a Point p2
          if self.icrayon is True
              Create a Crayon object from p1 to p2 and store it in a variable like L1
              Create a Line object from p1 to p2 and store it in a variable like L1
          Set the width of L1
          Set the outline color of L1
          Append L1 to the self.shapes list
          if self.win is not None
              Draw l1 into the window stored in self.win
      Update the location of the turtle
    • Finally, create a method for your ZTurtle called crayon that takes in self and one parameter value. The method should set the field self.crayon to the given value.
  4. To test out your crayon drawing, just make a call to turtle.crayon(True) before you draw a tree and see what happens.
  5. Test out your stochastic L-system trees with Crayon art and some other shapes and objects in the scene.


The following are suggested extensions.

  1. Create more interesting crayons
  2. Try other artistic effects
  3. Create a scene out of turtle graphics that is completely done in crayon (any time you use the forward function while the crayon flag is True will draw using Crayon objects).
  4. Create other objects, like a CrayonCircle, that look like they have a crayon drawn outline instead of a perfect outline perimeter.
  5. Make interesting L-systems using your stochastic L-systems from lab.


Create a web page for your writeup. Your writeup should include the following.

Handing in

When your writeup is ready for viewing and linked to your index page, send me an email.

To hand in your python code, place it on the Academics fileserver in the private folder. Do not post it on the web. You may include snippets from your code in your writeup to demonstrate how certain aspects of the function work, but please do not include the whole thing.

To place the code on the fileserver, go to the server fileserver1 and select the Academics volume. Within that there ought to be a CS151 folder, and within that there should be a folder with your username, and within that ought to be a folder labeled private. You can read and write files to the private directory and I can read them, but no one else can access them. Put your python files in that directory to hand them in. Please organize your files into folders by project.