CSCI 1300
Introduction to Computer Science
Homework 1: Triangles


Due Date

All work must be submitted by 10pm on Friday, Sep 2. Late work will be accepted until 10am on the following Monday with a 10 point late penalty. After that, your only way to submit is by using one of your two very late submissions for the semester (which will be due by 11:59pm on the following Wednesday). Submit the programming work online to http://culearn.colorado.edu, following the instructions at www.portmain.com/intro/submit.html.

Working Alone

You may talk with other students and instructors about the assignments, but you may not look at or copy code written by others. The penalty for violating this code is an F for the entire semester.

The Assignment

Write a program that opens a graphics window and draws five equilateral triangles. The triangles must be fairly small and at the positions of the five brightest stars in the constellation Canis Major (the "Dog"):

Each triangle must be drawn with three distinct lines; it must be an equilateral triangle in this orientation:

Of course, in winbgim, your triangles will be white lines on a black background. The sizes of the triangles should decrease in this order: Sirius (the biggest), Adhara, Beta (β), Delta(δ), and Eta(η). No triangle should be bigger than about 28 pixels from top to bottom.

After drawing the triangles, the program must pause for two seconds and then draw the same triangles in the same spots on the screen, but this time they will be flipped upside down (with the peak pointing down). Then the program pauses for 20 seconds and ends.

Program Requirements

Don't leave it until the last moment in case you have trouble figuring out how to submit to CULearn.

  1. The program file must be called canis.cxx. You must submit this file and no other.

  2. The program must correctly draw the triangles as specified, and then redraw the triangles upside-down. All 10 triangles will be on the screen at that point.

  3. The program must begin with a comment that includes:
    1. The file name and date it was written.
    2. Your name and e-mail address.
    3. A short description of the purpose of the program.

  4. The program must use one global constant for the window size (which is both the height and width). Call this int constant S.

  5. The positions of the triangles must be based on the window size S. You could do this with 10 more int constants for the triangles' (x,y) positions. These constants must be computed with formulas that involve S. For example, if you want the x position of triangle3 to be 5% of the way across the window, you could write:
    const int X3 = int(0.05*S);
    
    Or you could use the expression int(0.05*S) directly in your main program. The decision is up to you!

  6. The program must have a function that you write to draw a single equilateral triangle, and this function is called 10 times! A prototype and comment for this function must appear at the top of your code. The parameters for the function must be exactly these items:
    • the x and y coordinates of the center of the triangle;
    • a single number to indicate the distance from the center to a vertex;
    • a single number to indicate the angle of one vertex. This angle must be specified in radians, so if the vertex is straight up (above the center), then that parameter would have a value of M_PI/2. (The name M_PI is a predefined constant for pi from #include <cmath>.) Since the triange is equilateral, the other two vertexes will be at angles that are 2*M_PI/3 after the first vertex and 2*M_PI/3 before the first vertex.

    The implementation of the funtion will call the WinBGIm line function three times. You'll need to use some trigonometry to figure out the (x,y) coordinates of the endpoints of the three lines that it draws. For example, the x-coordinate of the peak point is given by this formula:

    center_x + radius*cos(peak_angle)
    
    where peak_angle is the name of your parameter that indicates the angle of the peak. The formula gives a double number, so you'll get a warning from the compiler (because the line function expects an int). Don't worry about that for now. (Later, we'll see how to avoid such warnings!)

  7. After the first group of triangles, the program must pause for 2 seconds before drawing the second group of triangles. At the end, the program must pause for 20 seconds before closing.

  8. The main program returns EXIT_SUCCESS.

  9. Each function definition (main and the triangle drawing function) must be preceded by a blank line and a line of dashes and followed by a line of dashes and a blank line.

  10. No line of code may exceed 80 characters.