This assignment, which will be presented in stages, will play a bit with various implementations and applications of a turtle graphics system.
(x, y, θ)
:
Implement the following set of functions in OpenGL that provides turtle graphics (I've also coded it as the source file Turtle.h):
init(x, y, theta)
initializes the turtle's position and orientation
forward(distance)
- moves the turtle forward the specified distance (leving a path
if the pen is lowered
left(angle)
- turn the turtle to the left the specified degrees (turning to the left increases the angle)
right(angle)
- turn the turtle to the right the specified degrees (turning to the right decreases the angle)
penUp()
- raises the pen -- turtle movemements leave no mark on the surface
penDown()
- lowers the pen -- turtle movemements leave a mark on the surface
display
function of an OpenGL program. I've included
an example below.
forward
function.
#define GLUT_DISABLE_ATEXIT_HACK #includeand here is the output:#include #include #include "Turtle.h" using namespace std; void init() { glClearColor(1, 1, 1, 1); gluOrtho2D(-100, 100, -100, 100); } static void display(void) { glClear (GL_COLOR_BUFFER_BIT); glColor3f (1.0, 0.0, 0.0); init(0, 0, 0); penUp(); forward(25); penDown(); left(90); forward(25); left(90); forward(50); left(90); forward(50); left(90); forward(50); left(90); forward(25); } int main(int argc, char *argv[]) { glutInit(&argc, argv); glutInitWindowPosition (0, 0); glutInitWindowSize (200, 200); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutCreateWindow ("Turtle"); glutDisplayFunc(display); init(); glutMainLoop(); return 0; }
Some More Implementation Details
Turtle.cpp
file, have (file scope, i.e., global) variablkes corresponding to the
init
function accepts initial settings for the position and angle (you might also choose to
allow the init function to accept an initial up/down position for the pen).
left
/right
functions increase/decrease the value of the current angle by the parameter (you might consider using the %
operator to keep the angle value within the 360 degree range).
glVertex
calls within a glBegin
/glEnd
block with a GL_LINES
value).
theta
, and a distance
(the parameter to forward
)
distance * cos(theta)
distance * sin(theta)
cos
and sin
functions accept radians as their
arguments. To convert your angle values to radians, multiply the angle value by π/180.0
(there are π radians in 180 degrees; thus one degree is equal to π/180.0
radians).
Write a turtle app that reproduces the above image (it doesn't have to be exact). (Click the picture to see the Wiki picture page -- it also contains pseudocode for the spiral-- you can either try to decipher it, or alternatively, examine the image and figure out how to reproduce it.)