Lab Exercise 11:
3D Turtle

The purpose of this lab is to give you one more chance to extend your drawing system. By now you should have a reasonable understanding of how all the pieces fit together, so we will swap out the standard 2D turtle and put in a 3D turtle. All of your 2D turtle programs should continue to work just fine, but now you can make 3D shapes as well. In addition, you will be able to rotate your completed drawings with the mouse.

Tasks

This lab consists of three major components:

  1. Edit the Interpreter class to make use of the 3D turtle. Try drawing some simple shapes with the modified interpreter.
  2. Interpret the 3D symbols for L-systems to draw 3D trees.
  3. Update the Shape class and possibly some subclasses to make use of the 3rd dimension

The goal is to be able to make 3D shapes and scenes as easily as 2D. You should be able to create a Cube class, for example, as well as 3D trees and other L-system shapes that work exactly the same way as their 2D counterparts.

  1. Create a working folder for project 11. Copy your lsystem.py, interpreter.py, shape.py, and tree.py files from last week. Label them as version 5 in the comments at the top of the files. Download this file: turtleTk3D.py.
  2. One difference between the standard turtle and the 3D turtle is that the latter is implemented as a class. Therefore, you need to create a single instance of a 3D turtle object that will be used by all Interpreter objects.

    At the top of interpreter.py, import turtleTk3D instead of turtle. Just below the import, create a global variable named turtle and initialize it to None.

    Modify the Interpreter.__init__() to look like the following. You may have other style fields that you want to keep in your code.

    def __init__(self, width=800, height=800):
        '''
        Initializes an Interpreter object and sets up a turtle graphics window
        and initial turtle parameters. This method is set up so there is only
        one turtle window created for all Interpreter instances.
        
        Parameters:
        width, height - the width and height of the graphics window to create
        '''
        # drawing style information
        self.lineStyle = "normal"
        self.jitterSigma = 2
        
        # ensures the code following is only executed once
        if Interpreter.initialized:
            return
        Interpreter.initialized = True
        
        # initialize the turtle graphics window
        global turtle
        turtle = turtleTk3D.Turtle3D(width, height)
        turtle.tracer(False)
    

    Note how the code for initializing the turtle window works. The first line tells the function to use the turtle variable in the global symbol table, and the second line creates a new Turtle3D object. By putting the 3D turtle object in a variable named turtle, expressions like turtle.left(angle) or turtle.forward(distance) still work as expected.

  3. Make the following additional changes to interpreter.py.
    • Edit your hold method so that it calls only turtle.mainloop().
    • If you have not already done so, make your '[' and ']' cases in drawString store and restore the turtle width in addition to the turtle position and heading.
    • Modify the goto method by adding an optional parameter zpos with a default value of 0. Change the turtle.goto() call to include zpos as the third argument. The function declaration should look like:
      def goto( self, xpos, ypos, zpos=0 ):
      
    • Modify the orient method so it accepts two additional optional parameters named roll and pitch. The declaration should look like:
      def orient(self, angle, roll=0, pitch=0):
      

      This function should call turtle.setheading(0), then call the roll, pitch, and yaw functions with the appropriate arguments.

    • Modify the place method so its declaration looks like:
      def place( self, xpos, ypos, angle=None, roll=0, pitch=0, zpos=0 ):
      

      Note that this parameter order is chosen to not break existing code that assumes angle is the third parameter. Modify the body of the function appropriately, but keep the test on the angle parameter (in other words, to specify a roll or pitch you must also specify the yaw).

    • Create additional methods named roll, pitch, and yaw, that call the corresponding 3D turtle functions. These functions will look like the existing width or color functions.
    • The turtle.position() method now returns the tuple (x, y, z) instead of just (x, y). There are likely some places in the Interpreter.forward method that need to be updated to handle the z coordinate. In the jitter case, for example, add a random offset in z, in addition to x and y.
    • Add cases in the drawString method for pitch and roll. The & symbol means to execute the pitch method with a positive angle, and ^ should execute the pitch method with a negative angle. The backslash \ should execute the roll method with a positive angle, and the forward slash / should execute the roll with a negative angle.

      Yaw is still + and -. (Either turtle.yaw() or turtle.left() and turtle.right() work.)

      Note that you will have to use the string "\\" to represent the backward slash character because it is a special character (e.g., '\n' is a newline and '\t' is a tab).

    • The 3D turtle color function works slightly differently from the original turtle. The primary difference is that it takes as input r, g, b values, a color string, or a single tuple (r, g, b) and it returns only a single color value. You will need to edit your angle brack cases '<' and '>' to take into account that there is no pencolor/fillcolor distinction.
    • The 3D turtle class does not have as many duplicate functions with different names, such as the standard turtle functions goto(), setposition() and setpos(). You may therefore need to modify a few cases in the drawString method to get around this. You will find the documentation for Turtle 3D at the bottom of this page.

    When you are done with these changes, try the first test program: test11-1.py.

  4. Make the following changes to shape.py.
    • Add three optional parameters to the Shape.draw() method for roll, pitch, and zpos. Give them all default values of 0. The orientation parameter already holds the yaw information.
    • Add roll, pitch, and zpos to the call to the Interpreter's place() method before the call to drawString.
  5. Run the test file test11-2.py. It is a simple example of how to begin building a 3D scene.

  6. Update your tree.py file. Add roll, pitch, and zpos to the parameter list of the draw method, giving them all default values of 0. Then pass the three parameters on to the parent draw function.
  7. Run the test file test11-3.py using one of the the L-systems below.

When you are done with the lab exercises, get started on the project.

Appendix: Turtle3D Documentation

The Turtle3D class implements a 3D turtle abstraction using the Tkinter package.

The Turtle3D class includes the following methods for public use.