CPS 6: Assignment 5

Summer 1999

Drawing Pictures

Due: Monday, June 14, 11:59pm

15 points

Change into your cps6 directory and create a directory called "assign5". Change into the "assign5" directory. If you did this correctly, when you type "pwd" you should see a long path name that ends with "/cps6/assign5"

In order to do this assignment, you need to copy files using the following "cp" command (don't forget the trailing period, or dot). Type:

 cp  ~dr/cps6/assign5/*   .

If you type "ls" you should see "Makefile", "pict1", "pict2", "pict3", "pict4", "pict5", "pict6", "gwindow.cc", "gwindow.h" and "pictsamp.cc".

For the programming problem that follows, you should use the style rules discussed in class, which includes meaningful variable names, indentation, and comments at the top of the file and for each function.

A Sample Run: Getting Started

In this assignment, you'll use the Window class to draw a colored picture of rectangles, circles, etc. The Window class uses the Samba animator program. The information describing the picture you draw will be stored in a file, as it would be too much information for you to type each time.

The program pictsamp.cc is a sample program you can use as a starting place. Since we've divided the Window class into files, gwindow.h and gwindow.cc, you need a special Makefile to compile everything. You should have already copied this Makefile at the beginning of this lab. Compile the program pictsamp.cc by typing: make pictsamp

Notice that make executes the g++ command three times. It compiles gwindow.cc first creating an object file gwindow.o, then it compiles pictsamp.cc, creating an object file pictsamp.o, then in the third compile it links pictsamp.o, gwindow.o together with the code for other files stored in a library into one executable file called pictsamp.

We'll be using the Samba animator to display the picture. The animator will only work under X windows; any of the Sparc 5's should work fine.

To use the animator, you should have first compiled pictsamp.cc above. The Window class outputs animator commands that are understood by the Samba animator. To run your program, all output will be piped into the animator by typing:

          pictsamp | samba

Go ahead and run the program now. A Samba window and a Polka Control Window should appear. You might need to move the Samba window so you can see the windows at the same time. To start the animation, click on the button start. You will need to type in the name of a file in the xterm window, type: pict1 . To quit at any time, click on the quit button.

The animator is set to run on high speed. The Polka window has a speed control bar which is currently set on the highest speed. You might want to slow the animation down until your program is correct, so you can see how the picture is drawn. Move the rectangle in this column to midway to slow the animation down before clicking the run animation button. Click here to see what the picture should look like.

IMPORTANT NOTE: All output is being sent to the animator. If you want to print anything to the screen (for debugging purposes or otherwise) you'll need to print the word comment at the begining of each output line and put an endl at the end of each line output. That is, each cout statement in your program should start and end with:

    cout << "comment " << .... << endl;

Requirement: One meaningful function

Your program should include at least one meaningful function besides the main function. If you want to use the Window class in the function, you will have to pass it with an "&". If you want to read from the file in the function, you will have to pass the input filestream with an "&". We will cover this in section 6.4.

For example if you have a void function called DoSomething that uses a variable of type Window and reads from the input stream "input", which has already been defined, it's function prototype would look like:

  void DoSomething(Window & w, istream & input)

The call to DoSomething might look like:

  Window graphics;
  string filename;
  ifstream input;

  cout << "comment Enter filename: " << endl;
  cin >> filename;
  input.open(filename);

  DoSomething(graphics, input);

Part 1: Reading rectangles, circles, etc. from a file.

Make a copy of pictsamp.cc to picture.cc by typing: cp pictsamp.cc picture.cc

The program pictsamp.cc creates a graphics window called w, and can only draw rectangles. This program reads in four integers at a time that represent a rectangle (these 4 integers are the same as the integers for the Rectangle function specified in the Window class), and draws the rectangle. The file pict1 has data on rectangles in this format. Note that you can only create one graphics window, and it has already been created for you. You can change the name from w to something else if you want.

For this part of the project, modify picture.cc to draw rectangles, circles, triangles, and lines, reading the information representing these objects from a file. In the file, the type of object will be specified first, followed by information about the object.

Shown below is a sample datafile. The format is the name of an object, followed by integers that describe the particular object. Rectangles are followed by 4 integers, triangles by 6 integers, lines by 4 integers and circles by 3 integers. See the gwindow.h file for a description of what the integers mean. The integers below correspond to the same integers (and in the same order) needed to call the corresponding member function in the Window class. For example, the Triangle function needs 6 integers (representing the 3 endpoints) to describe a triangle.

rectangle 30 5 30 40
triangle 30 45 45 60 60 45
rectangle       
12 5 18 20
line 34 11 4 0
circle 76 14 6

Be sure to use the Circle, Rectangle, Triangle, and Line member functions in the Window class (see gwindow.h).

After making these changes, you can test your program with the file pict2, which has the format described above. To compile this program, type: make picture

To run this program with the animator, type:

 picture | samba

and then type the filename: pict2
Click here to see what the picture should look like.


Part 2: Adding Initial Colors

We'll allow each of the objects from part 1 to optionally be specified with a color. If the color is specified, it will appear immediately after the name of the object. If the color is not specified, then the object will just default to the color it did in part 1. If a color is given, you'll have to first create the object, and then call the member function Color to change the color of the object. Note that each of the object member functions Rectangle, Circle, Line, and Triangle return a unique number for the object when it is created. You'll need this number to change the color of the object.

Below is a sample from a data file for this part. The first object specified is a rectangle, and it should be drawn in red. The triangle and the next rectangle do not have a color, so they will default to the color the Window class uses. The next object, the line, is to be drawn in blue. The first circle uses the default color and the second circle is to be drawn in green.

rectangle red 30 5 30 40
triangle 30 45 45 60 60 45
rectangle       
12 5 18 20
line blue 34 11 4 0
circle 76 14 6
circle green 46 14 6

After reading the name of an object, the second item read will either be a color or an integer. You'll have to read this in as a string. You can then call the Window member function IsValidColor to determine if what you read in is a color or an integer. If it is an integer, you can use the Window member function StringToNumber to convert the "string" to a number. For example, StringToNumber("87") returns the integer 87. If you call StringToNumber on a string that is not a number, for example StringToNumber("red"), your program will halt and print out an error message.

After making these changes, you can test your program with the file pict3, it has the format described above. To compile this program, type: make picture

To run this program with the animator, type:

 picture | samba

and then type the filename: pict3
Click here to see what the picture should look like.


Part 3: More Features

In this part, in addition to drawing the outlines of objects, these objects can be filled in or shaded and change their color (see the gwindow.h description for valid colors). The following commands should be processed:

Following is a sample data file. The first line indicates a circle specified by three integers. The outline of a circle is drawn. The next line is two commands, first a color command, followed by the color blue, then a fill command. The current object is the circle, the most recent object read from the file. Note that the Circle member function returns a number associated with the circle drawn. You can use this number to color the circle or fill it in. This circle is colored blue, and then the fill command fills it in so it is solid blue. The next four lines are 4 color commands that are applied to the circle (still the current object). It changes color to green, then blue, then green, then blue again (at slow speed you should see this). The next line has 6 commands, changing the shading of the color on the circle (still the current object) several times between halffilled and filled. The next line draws the green outline of a rectangle (now the current object). Note that color, fill or halffill commands may not always follow an object, as in this case. The next four lines draw the outline of a blue rectangle (now this rectangle is the current object), then changes its color to black, and then shades it in with the color black.

circle 85 85 8
color blue fill
color green
color blue
color green
color blue
halffill fill halffill fill halffill fill
rectangle green 60 72 8 10
rectangle blue
82 72 6 4
color black
halffill

See the gwindow.h file for an example using these commands.

Modify picture.cc to accept data in the above format. You can test your program with the file pict4, it has the format described above. To compile and run this program, see the instructions in the previous part. This time when it runs, type the filename: pict4
Click here to see what the picture should look like.


Part 4: Text and Comments

Modify picture.cc in the following manner. You might want to test these individually.

  1. Accept the text command, a new object, which displays a phrase in the picture at a specified location. This command is followed by an integer indicating the number of words in the phrase, two integers representing the (x,y) position of the center of the text when displayed, and then followed by the phrase. Text can be followed by color commands and can also optionally be given an intial color. Text commands will not be followed by fill and halffilled commands.

  2. Accept comments between commands. A comment starts with /* and ends with */ . A comment can appear between any two commands (commands are listed above in Part 2, plus the text command above), and can extend over several lines. A comment cannot appear between a command and its arguments. There is white space on either side of the /* and the */.

    Below is a sample datafile. There is a comment between the line and rectangle commands, between the rectangle and color commands, and between the color and fill command. There CANNOT be a comment between the command "line" and its 4 arguments. Comments can only appear between commands. Also below is an example of the text command. This phrase has 4 words "My cat is hungry." and will be centered at position (30,90).

    line 52 11 4 0
    /* shutters */
    rectangle
    32 8 2 6   /* comment is
    ok here also because this is between commands */
    color black /* let's fill it in */ fill
    text 4 30 90 
    My cat is hungry.
    

    Compile and run your program with the data file pict5 which has text commands and comments. Click here to see what the picture should look like.


    Extra Credit (1 pt): Picture

    Create your own picture in a file called mypict. This picture must use at least 30 commands.


    Extra Credit (3 pts): Polygon

    Make a copy of the program picture.cc called pictureX.cc using the "cp" command by typing:

     cp picture.cc pictureX.cc

    Modify pictureX.cc in the following manner. All these changes should occur in pictureX.cc. Do not modify the Window class.

    Accept the polygon command. The polygon command is optionally followed by a color, then followed by the number of points in the polygon, followed by the points in order, each point represented by an x coordinate and a y coordinate. For example, consider the following polygon command.

      polygon black 5 32 30 37 25 27 20 17 25 22 30
    

    The polygon is shown below with its coordinates (you would not display the coordinates, they are there just for the explanation). The polygon is drawn in black and contains 5 points. The top right point (32,30) is listed as the first point in the command above. The remaining points must be listed in order around the polygon (either clockwise or counterclockwise). In the example polygon command above, the remaining points are listed clockwise (37,25), (27,20), (17,25), and (22,30).

    To draw this polygon, you would draw the five lines forming the polygon, coloring each line the initial color if a color is specified. Otherwise, you would default to the default color for a line.

    Consider the following polygon commands shown below. The first two would produce the same polygon as above. Note the points are listed in a different order, but points sharing a line are still adjacent. The third polygon also produces the same polygon as above, but it defaults to the default color for drawing lines. The fourth polygon uses the same points as the polygon above, but connects the points in a different way, so this will not produce the same polygon as above.

      polygon black 5 37 25 27 20 17 25 22 30 32 30 
      polygon black 5 17 25 27 20 37 25 32 30 22 30
      polygon 5 32 30 37 25 27 20 17 25 22 30
      polygon black 5 32 30 37 25 22 30 27 20 17 25
    

    The polygon cannot be filled in or recolored. Thus, it will not be followed by a color, fill or halffill command.

    Compile and run your program with the data file pict6 which has polygon commands Click here to see what the picture should look like.


    Submitting Programs

    When all your programs compile and produce the correct output, create a "README" file (please use all capital letters). Include your name, section number, the date, and an estimate of how long you worked on the assignment in the "README" file. You must also include a list of names of all those people with whom you consulted on the assignment. See the CPS 6 syllabus for the rules on consulting.

    To submit your programs electronically type (leave off mypict and pictureX.cc if you did not do the extra credit)).

        submit6 assign5 README picture.cc mypict pictureX.cc
    

    You should receive a message telling you that the programs were submitted correctly.