CS 2005 Techniques of Programming WPI, B Term 1996
Craig E. Wills Lab 6
Assigned: Wednesday, December 11, 1996

# Turtle Graphics and Recursion

Turtle graphics is a popular way to write graphics programs. You may have seen turtle graphics before if you used LOGO. The idea is that there is a ``turtle'' (it really just looks like a triangle), that you can move around the screen. As the turtle moves it draws a track, so you can make just about any line drawing by giving turtle commands. Even though there are just a few turtle graphics commands, they are perfect for playing around with recursion, which is when a function in a program calls itself. Here are the commands in our version of turtle graphics:

```right( int angle )       - turn (in one place) "angle" degrees clockwise
left( int angle )        - turn (in one place) "angle" degrees counter-clockwise
forward( int steps )     - move "steps" distance in the direction you're heading
back( int steps )        - move "step" opposite the direction you're heading
hide()                   - make turtle invisible (this makes drawing faster)
show()                   - make turtle visible (this makes drawing slower)
clear()                  - clear window
repeat( int num )        - this is like a 'C' for loop
init_turtle()            - set up turtle graphics
wait_button()            - wait for user to click right mouse button```

Here's an example of a simple turtle graphics function to draw a triangle:

```tri( int length, int level )
{
if( level == 0 ) return;

repeat( 3 )
{
forward( length );
right( 120 );
}
}```

# Exercises

1. First to set stuff up, make a `lab6` directory somewhere in your home directory and copy files to it, as follows.
```> mkdir lab6
> cd lab6
> cp /cs/cs2005/pub/labs/lab6/* .```

2. Edit the file squares.c. Note that in this assignment the source files are written in C rather than C++, hence the lower case ``c'' file extension. Write the code for the function `square()` in the section where it says ` /* ADD COMMANDS HERE */ `, and make it draw a square. Use the triangle function as a guide. To compile and run your program type:

```> make squares
> squares```

3. Add the following line as the last line in your `square()` function, and say what gets drawn when you run square? Explain why.
`	square( length/2, level-1 );`

4. Move the line you just added, so that it is right after the ``forward'' command in the `square()` function. What gets drawn when you run squares now? Explain why.

5. What does the level parameter control; in other words, what happens when you change the line in main to:
`        square( 100, 6 );`

6. Edit the file tree.c The function `vee()` draws a 'V', like a branch of a tree. Try running tree to see what it does. To compile and run the tree program type:

```> make tree
> tree```

7. Change the `vee()` function (you only have to add 2 lines!), so that a smaller 'v' is drawn at the end of every branch.

8. Turn in squares.c, tree.c and lab6.questions for today's lab, using the following command line

`   /cs/cs2005/bin/turnin lab6 squares.c tree.c lab6.questions`

9. If you would like you can experiment with other recursive functions to draw other fun designs. As an example there is also a `snow.c` file in your directory that you can compile and run.