Comp 360 Lab1 : Turtle Graphics
Due Monday, 14 September 2009, 11:59 PM
Overview
In this project you will implement turtle graphics as discussed in class. First you will create a virtual turtle that can carry out the basic turtle commands. Next, you will use your turtle to draw simple shapes. You will then implement operators, such as shift, spin, and scale, which you will apply to simple shapes to generate more complicated geometry. Finally, you will create recursive turtle functions that generate fractals.
This project is due at 11:59 PM on Monday, September 14th. It is worth 100 points.
You should work with a partner on this lab. As soon as possible, send an email with your partner's name and username to powei.feng@rice.edu. If you have trouble finding a partner, let us know and we will try to pair you up.
Specification
Within the code framework we have provided, the user may enter a text command by pressing the tilde (~) key. Doing so brings up a command line. When the user presses enter on this command line, any text she enters is passed as a string argument to the parseInput() function in Callbacks.cpp. In this lab, most user interaction will be in the form of text commands entered in this way.
When your program starts, the screen should initially be blank. Your turtle should be located at the center of the window, facing to the right. The turtle will respond to text commands entered in the program's command line. Those commands are given below.
Basic Turtle Commands (17 points)
- Forward [steps]
This command should move the turtle forward by [steps] steps, leaving a trail on the screen in the process. By default one step is equivalent to one pixel on the screen.
- Move [steps]
This command should move the turtle forward as in the Forward command, but the turtle should not leave a trail.
- Turn [angle]
This command should rotate the turtle by [angle] degrees. The turtle's convention is that positive angles produce counterclockwise turns.
- Resize [scale]
This command should scale the turtle's step size by a factor of [scale].
- Color [red] [green] [blue]
This command should change the color of the turtle trail left by future Forward commands. Each of the arguments [red], [green], and [blue] should be a floating point value between 0 and 1. (See the hints section for a discussion on setting colors in OpenGL.)
- Linesize [size]
This command should change the width of the trail left by the turtle to [size]. The value of [size] should be consistent with the pixel width of the lines drawn on the screen. (See the hints section below for the necessary OpenGL commands.)
- Reset
This command clears the screen and restores the turtle's original position, orientation, color, step size, and trail width. Remember that the turtle's initial state should be in the center of the window, facing right.
Once you have implemented and tested these commands, you are ready to start writing turtle programs.
Simple Turtle Programs (17 points)
- Polygon [length] [sides]
Draws a polygon with [sides] sides, with each side of length [length].
- Star [length] [vertices]
Draws a star with [vertices] vertices, where each edge is of length [length].
- Circle [radius]
Draws a circle with radius [radius].
- InscribedStar [radius] [vertices]
Draws a circle of radius [radius] inscribed with a star with [vertices] vertices. The points of the star should lie along the circle.
- Wheel [length] [vertices]
Draws a wheel with [vertices] vertices, where each outer edge is of length [length]. Remember that a wheel is a polygon with every vertex connected to the center.
- Rosette [length] [vertices]
Draws a rosette with [vertices] vertices, where each outer edge is of length [length]. Remember that a rosette is a polygon with every vertex connected by a straight line to every other vertex.

A 20-sided rosette.
- Spiral [length] [angle] [scale]
Draw a spiral whose initial arm is of length [length], whose arms meet at [angle] degrees, and whose arms decrease in length by a factor of [scale].
Turtle Program Operators (17 points)
- Shift [distance] [repeats] ( [procedure] )
This operator should draw the geometry described by [procedure], then shift the turtle (using the Move command) over [distance] steps, repeating the sequence [repeats] times. The [procedure] can be either a turtle command, a program/shape/fractal, or another operator. Operators may be nested. The parentheses allow you to parse the statement without ambiguity.
- Spin [angle] [repeats] ( [procedure] )
Like Shift, this operator should draw the [procedure], then rotate the turtle by [angle] degrees, repeating this process [repeats] times.
- Scale [scale] [repeats] ( [procedure] )
This operator should draw the [procedure], then scale the turtle's step size by a factor of [scale], repeating this process [repeats] times.
Your program should also respond to the following fractal commands.
Turtle Fractals (17 points)
- KochSnowflakeSide [level] [length]
Draw one side of a Koch snowflake (that is, a Koch curve). The parameter [level] determines how many recursive calls to make for each side of the curve. The overall structure should start, at level 1, as a line of length [length]. At level 2 the program should draw a triangular bump in the middle of the line, and so on.

KochSnowflakeSide at recursion level 2.

KochSnowflakeSide at recursion level 3.
- SnowflakeSide [level] [sides] [length]
Draw a bump fractal containing a polygon with [sides] sides. This is the generalization of the Koch curve to arbitrary polygons. (A call of SnowflakeSide N 3 L would be equivalent to KochSnowflakeSide N L.)
- Snowflake [level] [sides] [length]
This function should generate a polygon of [sides] sides, where each side is generated using the SnowflakeSide program with the same arguments. Thus, if [sides] = 3, this command should produce a Koch snowflake as shown below- a triangle where each side is the fractal defined by SnowflakeSide [level] 3 [length].

The Koch Snowflake, created by the Snowflake program with [sides] = 3.
- PolyGasket [level] [sides] [length] [scale ]
This program should produce a gasket from a polygon with [sides] sides, each of length [length]. Recursive copies of the gasket should be scaled by the factor [scale]. For example, if [sides] is 3 and [scale] is 0.5, the result should be the Sierpinski gasket:

The Sierpinski Gasket, a PolyGasket with 3 sides and a scale factor of 0.5.
Additional Fractals (16 points)
For the remaining fractals, you should create your own command syntax. Be sure to clearly document how to call these turtle programs in your README file.
- Implement turtle programs to generate the five fractals shown below.

The Square Gasket and the Fractal Staircase.

The Hangman Fractal, a Fractal Bush, and a Fractal Tree.
Flag of Freedonia (16 points)
Freedonia has just emerged from several centuries of a brutal absolute monarchy.
Rufus T. Firefly, the newly elected democratic prime minister of Freedonia, has declared a
contest among his citizens to create an appropriate flag for the new Republic of Freedonia
to replace the old flag of the monarchy, which displayed a lion disemboweling a lamb on
a bloody red background. You are going to enter this contest, using turtle graphics to
generate a new flag for the fledgling republic. The best flag wins the grand prize,
sharing a bowl of duck soup with the prime minister. Your grade will reflect the
artistic quality of your flag.
Once again, you should document in your README the commands necessary to produce your flag.
Notes
- You may build a command using other commands. You may not, however, draw using anything other than basic turtle commands. In other words, there should be no OpenGL calls inside any commands other than turtle commands.
- The turtle does not have to return to its orginal position or scale after commands "shift", "spin", and "scale".
- The number of times to repeat the lines (spiral arms) in the "spiral" should be sufficiently large that it generates a reasonable spiral. I trust your artistic judgement.
- For the inscribed star of even number of vertices. You have the freedom to decide what to draw. Please describe your decision in the README.
- Colors are generally implemented as a set of three numbers, representing the amount of red, green, and blue light to project on the screen. Often we treat these numbers as floating point values normalized to the range [0.0, 1.0]. Thus, for the Color command, you are given three floating point numbers; the triple (1.0, 1.0, 1.0) represents pure white, and (0.0, 0.0, 0.0) represents pure black. The OpenGL function glColor3f() can be used to set the drawing color with normalized floating point values.
- By default, OpenGL draws points that are single pixels and lines that are 1 pixel wide. You can change these values by calling the GL function glPointSize(float size) and glLineWidth(float width). Note that these functions may not be validly called between calls to glBegin() and glEnd().
- Parsing is a complex discipline, and we don't want you to get bogged down with writing a robust text parser. For this project, you may assume that the commands given by the user are well-formed. (However, it will be appreciated if your program does not crash if the user makes a typo.)
- As you write this program, you might have to type and re-type several commands as you go through the code/test/debug cycle, it might be a good idea to write some code to read the commands from a file and feed them to the InputBox.
What To Turn In
Create a "lab1" directory. This directory should contain your source code and all files needed to build your program in Visual C++ or with CMake. Also include a README file containing:
- The names of you and your partner.
- A brief overview of how your code is organized.
- Instructions for running your additional fractals and extra credit programs.
- Any other implementation details you think your grader should know.
Only one partner per group should turn in code.
If your project will be completed late, email your labby and let him know.
Please follow the instruction on the home page of the class to submit your lab.
File translated from
TEX
by
TTH,
version 3.85.
On 27 Aug 2009, 14:11.