CMSC 10600 Fundamentals of Computer Programming 2
Winter 2002 --- N. Russo and S. Salveter

Homework 4 -- 1000 Words

Due Friday 08 February 2002

This is your first assignment using classes. We will provide the class structure, and specific goals. You'll need to fill in the details of the class definition and implementation. You are also responsible for making a main function which exercises the various capabilities of your classes.

The next assignment will be a follow-up assignment, in which we'll introduce some new concepts and start to manage larger programs.

Turn in your program in the usual way, as described in Instructions for Handing in Assignments.

Introduction

Everytime you send a file to a printer, whether it's an MS Word document, a webpage, an image of your nephew, or a midterm, the file is first converted into PostScript, a language which printers are programmed to interpret. We're going to learn a few PostScript commands, and then we'll use them to print visual representations of our class objects.

Our goal is to output a valid PostScript file, so first we need to understand the format of the file.

It should begin with this on the first line: %!PS-Adobe-2.0. This identifies the file as a PostScript Level 2 file. Next we'll put 10 setlinewidth on the second line to set the default line width for our drawings. At the end of the file, we should put the word showpage on a line by itself. In between the preamble and the postscript, we can put commands indicating where to draw lines, or write text.

To draw a line, we might do something like this:

30 200 moveto 600 200 lineto stroke
A triangle:
30 30 moveto 600 30 lineto 300 900 lineto closepath stroke
A square:
40 40 moveto 440 40 lineto 440 440 lineto 40 440 lineto closepath stroke

So the whole file might look like this:

%!PS-Adobe-2.0
10 setlinewidth
30 200 moveto 600 200 lineto stroke
30 30 moveto 600 30 lineto 300 900 lineto closepath stroke
40 40 moveto 440 40 lineto 440 440 lineto 40 440 lineto closepath stroke
showpage

Assignment

By now we know enough to draw all sorts of simple pictures, but it would be very tedious to write out all the lines of PostScript. Instead, we'll use classes to represent points, and shapes. We can then combine shapes until we have a whole picture, and we can ask the shapes to draw themselves on the page.

  1. Write a point class which holds doubles for the x and y coordinates of a point in a 2-dimensional plane.
  2. Write classes for at least two different shapes which have points as member variables. Each shape should have a draw member function which takes no arguments and has no return value.
  3. All your classes should keep variables private, and provide useful constructors.
  4. Write functions for printing out the PostScript header and footer.
  5. Provide a main function which exercises all the functionality of your classes. It might start out looking like this, but should be at least twice as long:
    int main() {
    	// Triangle vertices:
    	Point tv1(30,200);
    	Point tv2(600,200);
    	Point tv3(300,900);
    	Triangle T(tv1,tv2,tv3);
    	printHeader();
    	T.draw();
    	printFooter();
    	return 0;
    }
  6. Generate a few nice looking pictures.
  7. Turn in one file containing all your class definitions, member function definitions, auxiliary functions and main function. Turn in the .ps file so I can see what your PostScript code looks like. When turning in the paper copy, print out both the Postscript code and the interpretted version. Only turn in two files electronically, shapes.cpp and output.ps.

Notes

PostScript files can be sent directly to a printer, but bad PostScript files have the ability to cause lots of junk to spew out, so always check out .ps file in a PostScript viewer (like Ghostscript on a Mac or gv on linux). Also, don't print out stacks of triangles and squares. Don't print anything until you have the assignment finished. We all know how expensive trees are, and we don't want to waste them.

Extra Credit

  1. Add translate, rotate member functions which modify the object.
  2. Add translate and rotate friend functions which return a translated version of the object.
  3. I can add in some text like this:

    gsave
     0.8 setgray
     /Helvetica-Bold findfont 64 scalefont setfont
     60 400 translate 3.2 rotate 0 0 moveto
     (Nick Russo) show
    grestore
    Add another shape called Text which contains a Point as a starting location, and a c-string or c++ string to print out when it's draw function is called.

This assignment was inspired by a similar one given by Todd Dupont in 1998 for CS116.