CSCI 1300
Introduction to Computer Science
Homework 5: Acceleration Field

Due Date

All work must be submitted by 7:50am on Tuesday, Feb 23. Submit the programming work online to http://culearn.colorado.edu. No late work is accepted, so submit what you have before the due date!

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.

Programming Style

As always, all your work must follow these ten essential style items. See (snowmen.cxx for examples of this style. Also see www.cs.colorado.edu/~main/style.html.

The Assignment

You are the junior astrogation programmer on the USS Field of Dreams, a starship that is approaching a triple star system. The captain needs some accurate information about the acceleration due to gravity at various points in the system. "No problem," says the chief astrogation programmer, and she writes these two functions that will compute the acceleration at any point in the x-y plane that contains the three stars:

//----------------------------------------------------------------------------
double system_ax(double x, double y)
{
    double answer = 0;
    answer += (+2 - x)/((x - 2)*(x - 2) + (y + 5)*(y + 5));
    answer += (-3 - x)/((x + 3)*(x + 3) + (y - 2)*(y - 2));
    answer += (+3 - x)/((x - 3)*(x - 3) + (y - 6)*(y - 6));
    return answer;
}
//----------------------------------------------------------------------------

//----------------------------------------------------------------------------
double system_ay(double x, double y)
{
    double answer = 0;
    answer += (-5 - y)/((x - 2)*(x - 2) + (y + 5)*(y + 5));
    answer += (+2 - y)/((x + 3)*(x + 3) + (y - 2)*(y - 2));
    answer += (+6 - y)/((x - 3)*(x - 3) + (y - 6)*(y - 6));
    return answer;
}
//----------------------------------------------------------------------------
Notice that the chief took CSCI 1300 from Michael and Dmitry, and he still follows the CSCI 1300 style guide!

Both functions have the same parameters: The x and y coordinates of a location in the system measured in astronomical units. The output of system_ax is the acceleration that any object will feel if it is at that location in astronomical units per second squared (these are massive stars to generate that kind of force--maybe they are actually black holes!).

She takes her functions to the captain, but he bellows, "This does me no good! I have to see a chart!"

"I don't have time to make charts!" replies the chief.

But you do. So, you grab the chief's code for the two functions and say, "I'll be back by 7:50am on Tuesday with your charts!"

Your Program's Output

Your program will produce a chart that looks like this:

Here is how to interpret things:

  1. The chart's coordinate system runs from -10 A.U. (on the left and bottom) to +10 A.U. (on the right and top). This number, 10, is declared as a global constant named WMAX in your program.
  2. The three yellow dots are the stars (filled circles with a radius of 5 pixels). Their coordinates are (2, -5), (-3, 2) and (3, 6).
  3. The lines begin at regular grid points. The x and y values of this grid go from -WMAX to +WMAX in increments of WMAX/6.
  4. At the other end of the line, is a small, filled, white circle with radius of 2 pixels.
  5. The coordinates of the other end of the line are determined by adding system_ax and system_ay to the line's starting point.
  6. Any line with a total length that is longer than WMAX/7.07107 is not drawn.
After the program finished drawing the chart, it should stay open for 60 additional seconds and then close with EXIT_SUCCESS.

Next week, we will simulate your ship flying through this star field.

Grading Criteria

  1. Your program must be called field.cxx. You must submit this file and no other. The title of the graphics window is "Field of Dreams."
  2. You must meet all the style requirements.
  3. Your code compiles correctly with no warnings.
  4. You must draw the correct chart of the acceleration field.
  5. Additional requirement #1: You must demonstrate a good use of functions in your design rather than dumping all the work into the main program. For example, my solution has a function to draw the entire field, another function to draw a single vector in the field, and I also made use of the pixel function from Homework 4.
  6. Any number that is used multiple times in the program is declared as a global constant (such as WMAX in the above description). If the TA changes any of your global constants and recompiles, then the program continues to work flawlessly.