Some introductory graphics programs

The goals of this lab are threefold: to give you some experience using the graphics system developed by Horstmann, to introduce you to the Unix make (actually gmake) utility, and to give you some more practice writing C++ programs.

We are providing the following auxiliary document which you should peruse before doing the corresponding part of the lab:

Drawing triangles (1 point)

The goal of this part of the lab is to write the program described in programming exercise P3.2 on page 120 of the text. However, we are going to specify more explicitly how you do this so that you have the opportunity of learning how to use gmake.
  1. Go to your mc38 directory, create a new subdirectory called graphics-fun, and then copy the following file into the graphics-fun directory:
         cp ~mc38/labs/graphics-fun/triangle.cc graphics-fun
    
    The program triangle.cc is the shell of the program you are going to expand into a solution to exercise P3.2. We are providing it since it includes the correct files and provides a starting point for you to write the Makefile you need in order to use gmake.

    Now go to the graphics-fun directory and open up triangle.cc in emacs.

  2. First compile the program triangle.cc from emacs using the following command, which I will have explained in class:
         g++ -I/usr/local/lib/cccfiles -L/usr/X11R6/lib -g -Wall -o triangle triangle.cc -lX11
    

  3. We now ask you to write a Makefile that will cause the exact same compilation to occur when you execute the following command:
         make -k
    
    Note that this is the default command when you do M-x compile in emacs. This implies that you will no longer have to remember what compile/link command to issue from one emacs session to the next; the Makefile remembers it for you.

    In order to correctly write Makefile, you will need to read the online notes on Using gmake, at least through (and including) the section Basic Operation and Syntax.

  4. Now add the necessary code to triangle.cc so that it solves programming exercise P3.2.

  5. Check-off: Show us your completed code and demonstrate that it works as stipulated. Also, show us your Makefile and demonstrate that it causes the correct compilation to occur.

Drawing a clock (1 point)

The goal of this part of the lab is to write the program described in programming exercise P3.16 on page 123 of the text. However, you are going to have to modify Makefile so that it will simultaneously maintain both this program and the one you just wrote.
  1. Create a shell of a program called, say, clock.cc in the same directory as triangle.cc and Makefile. Note that you will have to include ccc_time.cpp in addition to ccc_win.cpp. You also need to link in the math library by tacking -lm onto the end of your compile/link command.

  2. Now modify Makefile so that it simultaneously maintains the two programs. The easiest way for you do this is to create a new .PHONY dependency called (for example) all that depends on the two executables you are maintaining. Then make sure that you have separate dependencies for each of them. (You don't need an action for this .PHONY dependency.)

  3. Now add the necessary code to clock.cc so that it solves programming exercise P3.16. Be sure that you don't use magic numbers in your code; use constants instead. Note that it's easiest to measure all angles clockwise from midnight.

  4. Check-off: Show us your completed code and demonstrate that it works as stipulated. Also, show us your Makefile and demonstrate that it causes the correct compilations to occur.

Drawing a better clock (1 point)

The goal of this part of the lab is to write the program described in programming exercise P3.16 on page 123 of the text. However, you are going to have to modify Makefile so that it will simultaneously maintain both this program and the one you just wrote.
  1. Write another program called, say, better-clock.cc the add twelve ``tick-marks'' around the clock you drew in the previous part corresponding to the twelve hours. Be sure to update Makefile so that it maintains all three programs.

    You are free to use a for loop even though you haven't read about it in the book yet. The following piece of C++ code outputs the numbers from 1 to 12:

       for (int i = 1; i <= 12; i++) {
          cout << i << endl;
       }
    

  2. (optional improvement) Add text to the clock giving the twelve hours. Be sure the text is placed in a visually appealing manner. As usual, update Makefile.

  3. Check-off: Show us your completed code and demonstrate that it works as stipulated. Also, show us your Makefile and demonstrate that it causes the correct compilations to occur.

Adding variables to your Makefile (1 point)

  1. Read the Variables section in the online notes on Using gmake, and modify your Makefile so that it defines and uses the following variables. (These variables names, though seemingly obscure, are standard.)

    Once you've defined these variables, a typical rule will be:

      triangle: triangle.cc
         $(CXX)  ...  triangle.cc -o triangle ...
    
    where you'll need to fill in the ... with appropriate variables. You won't need to use the $(CC) variable.

  2. Check-off: Show us your Makefile and demonstrate that it causes the correct compilations to occur.