Due: Friday, May 5, 2017, 11:59 pm
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.
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.
At the top of turtle_interpreter.py, import turtleTk3D instead of turtle, and create a global variable called turtle 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.
def orient(self, angle, roll=0, pitch=0)
Then have the function call the turtle method setheading with an argument of 0, then roll with the roll argument, pitch with the pitch argument, and yaw with the angle argument.
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. Give the new arguments (zpos, pitch, and roll) default values of 0.
The other change to the place method is in the case where angle is not None, call your turtle interpreter's orient function (self.orient) with angle, roll, and pitch as the arguments.
turtle.position()method now returns the tuple (x, y, z) instead of just (x, y). There are likely some places in your 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.
Yaw is still + and -.
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) .
When you are done with the lab exercises, you may begin Project 11.
The Turtle3D class implements a 3D turtle abstraction using the Tkinter package.
The default turtle window supports three user operations.
The Turtle3D class includes the following methods for public use.
__init__: the constructor function has six optional arguments.
reset(): deletes all drawing and resets the turtle to its initial position.
forward(distance): goes forward in the current turtle direction. If the pen is down, it creates a line.
goto(xnew, ynew, znew): The function can take one, two or three parameters. If the pen is down, it creates a line.
left(angle): turn left (yaw) as defined by the current heading and up direction.
right(angle): turn right (yaw) as defined by the current heading and up direction.
yaw(angle): go left (positive) or right (negative) relative to the current turtle orientation.
pitch(angle): go down (positive) or up (negative) relative to the current turtle orientation.
roll(angle): rotate right (positive) or left (negative) about the current turtle heading.
width(w): set the width of the pen to w
color(r, g, b): sets the pen color. Arguments can be the standard X11 color strings defined in rgb.txt or r, g, b values. The first argument can be a tuple (r, g, b) or the color values can be given individually. Color values should be in [0, 1].
up(): pick up the pen
down(): put down the pen
tracer(blah): does nothing (no visible turtle)
circle(radius, theta): draws a circle of the given radius. The theta argument is optional and permits drawing only the angular fraction of the circle specified.
position(): returns a tuple with (x, y, z)
heading(): returns a tuple of two tuples with the current heading and up vectors. ( (hx, hy, hz), (ux, uy, uz) )
setheading(heading): ideally heading should be two vectors representing the turtle's forward and up directions. If a single scalar is provided, the turtle is set so the turtle is in the drawing plane (up of (0, 0, 1)) and is rotated according to the argument. This gives it the same functionality as the 2D turtle.
fill(q): if q is True, the turtle will begin to store points until the fill function is called with False as the argument. Then it fills in the area defined by the points. The turtle must visit at least three points after fill(True) is called and before fill(False) is called in order to generate a polygon.
nudge(n): allows the user to adjust the turtle's coordinate system by nudging the forward vector in the specified direction. The argument n should be a 3-element sequence (list or tuple). A value like (0.0, -0.1, 0.0) nudges the turtle's orientation down (i.e. like gravity).
cube(distance): takes one optional parameter (size) and draws a cube.
setRightMouseCallback(func): takes one argument, which should be a function with an argument (other than self for a class method) called event. The mouse click location will be in event.x and event.y. Note that the click location will be in window coordinates, not turtle coordinates.
window2turtle(x, y): takes in window coordinates (like those in event.x and event.y above) and returns a tuple (x', y', 0) with the corresponding turtle coordinates in the default view.
hold(): goes into a main loop waiting for user input.
wait(): goes into a main loop waiting for user input.
updateCanvas(): updates the Canvas to draw any new shapes.
© 2017 Ying Li. Page last modified: .