COS 126

Turtle Graphics
Programming Assignment

Due: Wednesday

Write a C program that reads in a set of turtle graphics commands and creates a PostScript program that depicts the drawing described by the commands. With turtle graphics, you can make simple 2-dimensional drawings such as those below:

Dragon Curve

Flower

The turtle graphics language. Imagine that you are directing the actions of a turtle. The turtle walks or flies around on a 2D plane (the floor) while carrying a magic pen. The magic pen draws in over 16 million different colors and never needs refilling. While walking on the ground, the turtle always holds her magic pen down, writing as she walks. She can only walk forward, in the direction that she is currently facing, but she can rotate to change her direction. The turtle can also fly to a specified point in the plane, but no writing can occur during flight. While hovering over the specified point, the turtle can use her magic pen to leak a spot of ink on the point beneath it.

The turtle has a location in the x-y plane and a direction that is an angle measurement with respect to the positive x-axis. You provide commands to choreograph the actions of the turtle. Each command is encoded with a single uppercase letter (F, C, S, T, or D) followed by some number of real-valued parameters.

Below is a sample program that instructs the turtle to draw three colored squares and two parallel lines.


C 255 0 0
F 200.0 100.0
S 20.0

C 0 255 0
F 200.0 200.0
S 40.0

C 0 0 255
F 200.0 300.0
S 40.0

F 30 20.0
D 360.0

F 30 410.0 
D 360.0

Sample PostScript program


To practice using the turtle graphics language, experiment with this Java applet. It uses a language very similar to the one described in this assignment.

Part 1. As a first step, write a C program that reads in the turtle graphics commands and prints them out. Note that each command begins with a single uppercase letter, but the number of parameters varies depending on the command. Use scanf() to read in the commands; store each command in a variable of type char and the parameter(s) in one or more variables of type int or double.

A PostScript primer. You will write a C program turtle.c that reads a sequence of turtle graphics commands (like the one above) from standard input and prints out a corresponding PostScript program to standard output. It is unlikely that you have seen raw PostScript commands before, but learning the basics is not difficult. It is best learned by example.





%!PS-Adobe-3.0 EPSF-3.0
%%BoundingBox: 0 0 512 512
1.0 0.0 0.0 setrgbcolor
190 90 20 20 rectfill 
0.0 1.0 0.0 setrgbcolor
180 180 40 40 rectfill 
0.0 0.0 1.0 setrgbcolor
160 260 80 80 rectfill
1.0 0.0 1.0 setrgbcolor
30.0 20.0 moveto 390.0 20.0 lineto stroke  
30.0 410.0 moveto 390.0 410.0 lineto stroke
showpage

Sample PostScript program


The first line indicates that the text file contains PostScript data. The BoundingBox line indicates the boundaries of the canvas on which all drawing will take place. The next 6 lines draw three squares of different sizes and colors. The setrgbcolor routine sets the pen color in RGB units: the first square is red, the second is green, and the third is blue. Note that all three parameters are specified before calling the setrgbcolor routine. The rectfill routine draws filled rectangles. It takes four arguments: the lower left x and y coordinates and the width and the height of the rectangle. After drawing the three squares, the program moves the imaginary pen (without drawing anything) to the point (30, 20) using the routine moveto. Then, the program draws a line to the point (390, 20). Finally, the program draws a second line from (30, 410) to (390, 410). The showpage line indicates that the drawing on the page is complete.

Your C program will output text almost exactly like the text above using printf(), except that it will call the PostScript routines with different values and it will interleave them in a different order. It is very important that you get the syntax exactly right; otherwise you will not be able to view anything.

To understand how it works, put this PostScript program into a text file called test.ps and view it using a PostScript viewer such as Ghostview.

Part 2. Implement the C, F, and S commands in PostScript. Write a separate function for each command, perhaps named C(), F(), and S(). To get you started, the complete code for the color-setting function is

void C(double r, double g, double b) {
    printf("%f %f %f setrgbcolor\n");
}
The functions F() and S() require a bit more care since you need to access the current x and y coordinates of the turtle. Consider using global variables to store these quantities.

Part 3. Now, write C code to keep track of the D and T commands. Unlike in Part 2, these commands do not move to absolute coordinates, but instead move relative to the current reference frame. The case for an R command is not much code: use a variable to record the turtle's current direction in degrees, and add the input number to the turtle's current direction. For now, we will treat M and D commands as the same and not worry about whether the pencil is up or down. For both M and D commands, we need to update the position of the turtle. If we know the direction the turtle is facing and we also know her current position, we can use sine and cosine functions to determine the new position. If the current direction is a (with respect to the positive x-axis), the current position is (current_x, current_y), and we want to move the turtle forward by some distance d, then the new x and y coordinates are given as follows:

new_x = current_x + d*cos(a)
new_y = current_y + d*sin(a)
These equations are correct for any value of a.

Below are two geometry examples that show the current and new positions of the turtle based on d and a:

Geometry I

Geometry II

The sine and cosine functions are library functions that are defined in the math.h library. To find the cosine or sine of an angle a (specified in radians), call cos(a) or sin(a).

Compilation and execution. Depending on your system, you may need to explicitly link in the math library with the -lm flag when compiling, e.g,

gcc turtle.c -lm

When you run your C program, read the input from a data file and redirect its output to a file with the command

a.out < turtleinput.txt > turtleoutput.ps         (Unix)
turtle < turtleinput.txt > turtleoutput.ps        (DOS)
The file turtleoutput.ps now contains a PostScript program! To debug, first make certain that your C program produces a valid PostScript program like the sample program, except with different calls to rectfill, setrgbcolor, moveto, and lineto.

Submission. Submit the following files: turtle.c, readme.txt.

Extra credit. There are many opportunities to challenge yourself and earn extra credit. Try writing new programs in the turtle language to create interesting drawings. You can also try to add some new commands to the turtle language. For example: create a command that changes the color of the lines, a command that fills an area with color, or a command for making loops. You can look on the web for information on extra PostScript routines (such as color commands or fill area commands) that will help you if you decide to try expanding the turtle language.

This assignment was created by Rachel Fithian and Kevin Wayne.

Copyright © 2002 Robert Sedgewick