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.
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 strokeA triangle:
30 30 moveto 600 30 lineto 300 900 lineto closepath strokeA 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
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.
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; }
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.
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 grestoreAdd 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.