CS 151: Lab 11

Title image Project 11
Fall 2019

Lab Exercise 11: 3D Turtle

The purpose of this lab is to give you one more chance to extend your drawing system. You should have a reasonable understanding of how all the pieces fit together, so now we're going to 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'll be able to rotate your completed drawings with the mouse.


Tasks

The lab consists of three parts.

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

    Create a new working folder. Copy your lsystem.py, turtle_interpreter.py, shapes.py, and tree.py files from last week. Label them as version 5. Then download the 3D turtle file (documentation at the bottom of this page).

  2. Update the TurtleInterpreter Class to Use the 3D Turtle

    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 the turtle object that will be used by all TurtleInterpreter objects. With appropriate naming, all of the rest of your turtle code will work fine.

    At the top of turtle_interpreter.py, import turtleTk3D instead of turtle, and create a global variable called turtle (or t if you called the turtle module t) and initialize it to None.

    Second, before calling any other turtle functions in your __init__ method, but after the test of TurtleInterpreter.initialized, put the following two lines.

    global turtle
    turtle = turtleTk3D.Turtle3D(dx, dy)
    						

    The first line tells the function to use the turtle variable in the global symbol table, and the second line creates a new turtleTk3D object. By putting the 3D turtle object in a variable called turtle, expressions like turtle.left(angle) or turtle.forward(distance) still work as expected.

  3. Update TurtleInterpreter to Handle 3D Rotations

    Make the following additional changes to turtle_interpreter.py.

    1. Edit the hold method so that it calls only turtle.mainloop(). Delete everything else.
    2. If you have not already done so, make the '[' and ']' cases in drawString to store and restore the turtle width in addition to the heading and position.
    3. Edit the orient method so it takes two additional optional arguments: roll and pitch. The definition should look like:

      def orient(self, angle, roll=0, pitch=0)

      The function should first call the turtle method setheading with an argument of 0, then roll by the roll argument (e.g. turtle.roll(roll)), pitch by the pitch argument, and yaw by the angle argument. Executing a yaw rotation is the same as a left rotation in the planar turtle.

    4. If you have a goto method, add an optional parameter zpos with a default value of 0. Then change the turtle.goto call to include zpos as the third argument.
    5. Do the same with the place method. It should have two required arguments (xpos and ypos) and four optional arguments (angle, roll, pitch, and zpos).

      The other change to the place method is in the case where angle is not None, call the turtle interpreter's orient function (self.orient) with angle, roll, and pitch as the arguments.

      Design Note: if you set up the arguments as place(xpos, ypos, zpos=0, angle=None, pitch=0, roll=0) it will break your prior code that assumes angle is the third argument. It's probably better to make zpos the last argument, even if it seems out of place. Give the new arguments (pitch, roll, and zpos) default values of 0. Backward compatibility is often a requirement when making changes to an existing system, even if it leads to some odd decisions.

    6. Create additional turtle interpreter methods called roll, pitch, and yaw, that call the 3D turtle functions roll, pitch and yaw. These functions will look like your existing width or color functions.
    7. The turtle.position() method now returns the tuple (x, y, z) instead of just (x, y). There are likely some places in the TurtleInterpreter's forward method that need to be updated to handle the z coordinate. In the jitter and jitter3 cases, you probably want to add a random offset in z, in addition to x and y, and make all of your goto calls include the z coordinate.
    8. Add cases in in drawString method for pitch ( & and ^ ) and roll (\ and /). The & symbol means to execute the pitch method with a positive angle (down), and ^ should execute the pitch method with a negative angle (up). The backslash '\' should execute the roll with a positive angle (right), and the forward slash '/' should execute roll with a negative angle (left).

      Yaw is still + and -.

      Syntax Note: 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). Two backslashes in a row tells Python to insert a single backslash in the string.

    9. The 3D turtle color function works slightly differently from the regular 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'll need to edit your angle bracket cases '<' and '>' to take into account that there is no pencolor/fillcolor separation.
    10. You may need to update your code in the turtle interpreter to use functions provided by the 3D turtle but not the planar turtle, but most of the functions and their variations are supported. Change these as they crop up.
    11. When you are done with these changes, try out first test program. (Note that is assumes your TurtleInterpreter has a method named width. If you have a method named setWidth, then update the test code to use the method you have. You don't want to just change the name of your method to width because you can't have a method and a data field with the same name.)
  4. Update the Shape Class for 3D

    Make the following changes to shapes.py.

    1. Add three optional parameters to the Shape class draw function. The arguments should be called roll, pitch, and zpos and be added to the parameter list in that order. Given them all default values of 0. The orientation parameter already holds the yaw information.
    2. Add pitch, roll, and zpos to the place call before drawString.

    Run test function two. It is a simple example of how to begin building a 3D scene. It should look like a poorly designed shelving unit.

  5. Update the Tree Class to Use 3D

    Update your tree.py file. Add roll, pitch, and zpos to the parameter list of the Tree class draw method, giving them all default values of 0. Then pass the three parameters on to the parent draw function in the proper order.

    Run the test function 3 using one of the L-systems below. SystemZ works well with 3 or 4 iterations. SystemZ2 and systemZ3 work well with 7-9 iterations.


When you are done with the lab exercises, you may begin the project.


Appendix: Turtle3D Documentation

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

The default turtle window supports three user operations.

  1. Once drawing is complete, the user can rotate the image using the first mouse button and clicking and dragging.
  2. The user can close the window and quit the program by typing Command-q or by typing just q.
  3. The user can reset the view to the default orientation by typing Command-r or by typing just r.

The Turtle3D class includes the following methods for public use.