(Turtle Graphics) The Logo language, which is popular among elementary school children, made the concept of turtle graphics famous. Imagine a mechanical turtle that walks around the room under the control of a C++ program. The turtle holds a pen in one of two positions, up or down. While the pen is down, the turtle traces out shapes as it moves; while the pen is up, the turtle moves about freely without writing anything. In this problem, you’ll simulate the operation of the turtle and create a computerized sketchpad as well. Use a 20-by-20 array floor that is initialized to false. Read commands from an array that contains them. Keep track of the current position of the turtle at all times and whether the pen is currently up or down. Assume that the turtle always starts at position (0, 0) o
Suppose that the turtle is somewhere near the center of the floor. The following “program” would draw and print a 12-by-12 square and end with the pen in the up position:
2
5,12
3
5,12
3
5,12
3
5,12
1
6
9
As the turtle moves with the pen down, set the appropriate elements of array floor to true. When the 6 command (print) is given, wherever there is a true in the array, display an asterisk or some other character you choose. Wherever there is a zero, display a blank. Write a program to implement the turtle graphics capabilities discussed here. Write several turtle graphics programs to draw interesting shapes. Add other commands to increase the power of your turtle graphics language.
```
#include
using namespace std;
// constant global variables
const int MAXCOMMANDS = 100;
const int SIZE = 20;
int turnRight( int ); // function prototype
int turnLeft( int ); // function prototype
void getCommands( int [][ 2 ] ); // function prototype
void movePen( int, int [][ SIZE ], int, int ); // function prototype
void printArray( const int [][ SIZE ] ); // function prototype
int main()
{
int floor[ SIZE ][ SIZE ] = {};
int command;
int direction = 0;
int commandArray[ MAXCOMMANDS ][ 2 ] = {};
int distance;
int count = 0;
bool penDown = false;
getCommands( commandArray );
command = commandArray[ count ][ 0 ];
// continue receiving input until 9 is entered
while ( command != 9 )
{
// determine what command was entered and perform desired action
switch ( command )
{
case 1:
penDown = false;
break;
case 2:
penDown = true;
break;
case 3:
direction = turnRight( direction );
break;
case 4:
direction = turnLeft( direction );
break;
case 5:
distance = commandArray[ count ][ 1 ];
movePen( penDown, floor, direction, distance );
break;
case 6:
cout << "\nThe drawing is:\n\n";
printArray( floor );
break;
} // end switch
command = commandArray[ ++count ][ 0 ];
} // end while
} // end main
// function that prompts user for and keeps track of commands
void getCommands( int commands[][ 2 ] )
{
int tempCommand, i;
cout << "Enter command (9 to end input): ";
cin >> tempCommand;
// receive commands until 9 or 100 commands are entered
for ( i = 0; tempCommand != 9 && i < MAXCOMMANDS; i++ )
{
commands[ i ][ 0 ] = tempCommand;
// ignore comma after 5 is entered
if ( tempCommand == 5 )
{
cin.ignore(); // skip comma
cin >> commands[ i ][ 1 ];
} // end if
cout << "Enter command (9 to end input): ";
cin >> tempCommand;
} // end for
commands[ i ][ 0 ] = 9; // last command
} // end function getCommands
// function to turn turtle to the right
int turnRight( int d )
{
return ++d > 3 ? 0 : d;
} // end function turnRight
// function to turn turtle to the left
int turnLeft( int d )
{
return --d < 0 ? 3 : d;
} // end function turnLeft
// function to move the pen
void movePen( int down, int a[][ SIZE ], int dir, int dist )
{
static int xPos = 0;
static int yPos = 0;
int j; // looping variable
// determine which way to move pen
switch ( dir )
{
case 0: // move to the right
for ( j = 0; j < dist && yPos + j < SIZE; j++ )
{
if ( down )
a[ xPos ][ yPos + j ] = 1;
} // end for
yPos += j - 1;
break;
case 1: // move down
for ( j = 0; j < dist && xPos + j < SIZE; j++ )
{
if ( down )
a[ xPos + j ][ yPos ] = 1;
} // end for
xPos += j - 1;
break;
case 2: // move to the left
for ( j = 0; j < dist && yPos - j >= 0; j++ )
{
if ( down )
a[ xPos ][ yPos - j ] = 1;
} // end for
yPos -= j - 1;
break;
case 3: // move up
for ( j = 0; j < dist && xPos - j >= 0; j++ )
{
if ( down )
a[ xPos - j ][ yPos ] = 1;
} // end for
xPos -= j - 1;
break;
} // end switch
} // end function movePen
// function to print array drawing
void printArray( const int a[][ SIZE ] )
{
// display array
for ( int i = 0; i < SIZE; i++ )
{
for ( int j = 0; j < SIZE; j++ )
cout << ( a[ i ][ j ] ? '*' : ' ' );
cout << endl;
} // end outer for
} // end function printArray
```
You might also like to view...
You are approximating a definite integral of a function that does not have an antiderivative among the simple functions. You should use
a. regression analysis b. central difference formula c. Simpson's or trapezoidal rule d. Newton's or bisection method
Consider a class that uses the following variables to implement an array-based stack:
``` String [] s = new String[100]; int top = 0; ``` a method that implements the String peek() operation can be written as A) return s[top]; B) if (top == 0) throw new RuntimeException("Underflow"); else return s[top]; C) if (top == 0) throw new RuntimeException("Underflow"); else return s[top-1]; D) return s[top-1];