CSCI 1300
Introduction to Computer Science
Homework 5: Acceleration Field

Due Date

This work must be submitted by 10pm on Friday, October 14. Late work will be accepted until 10am on the following Monday with a 10 point late penalty.


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. M\

Programming Style

As always, all your work must follow these ten essential style items from www.cs.colorado.edu/~main/style.html.

The Assignment

You are the junior astrogation programmer on the USS Field of Dreams, a 2-dimensional starship that is approaching a multi-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 functions that will compute the amount of acceleration that a star (at location (sx,sy)) will generate on a ship (at location (x,y)):

//-----------------------------------------------------------------------------
// The parameters x and y give the location of a spaceship in a
// two-dimensional star field. The parameters sx and sy give the
// location of a star of mass m in this field. The return value
// is the acceleration along the x-axis that the ship feels from
// the star. All distances are in meters and the mass
// is in kilograms. The answer is in m/sec^2.
double accx(double x, double y, double sx, double sy, double m)
{
    double dx = (sx - x);
    double dy = (sy - y);
    double denominator = pow(dx*dx + dy*dy, 1.5);

    return G*m*dx/denominator;
}
//----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// The parameters x and y give the location of a spaceship in a
// two-dimensional star field. The parameters sx and sy give the
// location of a star of mass m in this field. The return value
// is the acceleration along the y-axis that the ship feels from
// the star. All distances are in meters and the mass
// is in kilograms. The answer is in m/sec^2.
double accy(double x, double y, double sx, double sy, double m)
{
    double dx = (sx - x);
    double dy = (sy - y);
    double denominator = pow(dx*dx + dy*dy, 1.5);
    
    return G*m*dy/denominator;
}
//----------------------------------------------------------------------------
Notice that the chief took CSCI 1300 at CU, and she still follows the CSCI 1300 style guide!

Both functions have the same parameters: The x and y coordinates are the location of a ship in the system measured in meters from the point that the astrogator calls the "center.". The coordinates (sx,sy) are the location of a star with mass m (measured in kilograms). The return value of the first function is the acceleration (in Newtons) that the ship feels along the x-axis; and similarly for accy and the y-axis.

"What's G?" you ask.

"You're a silly junior astrogator!" replies the chief. "G is a global named constant for the universal constant of gravity. You'll have to look that up if you don't know its value by heart." (It is 6.673e-11 in the units that we are using!)

The chief 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 10:00pm on Friday 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 -2.5e11 meters (on the left and bottom) to +2.5e11 meters (on the right and top). This number, 2.5e11, is declared as a global constant named WMAX in your program. (The e11 means that the 2.5 is multiplied by 10 to the 11th power.)
  2. The three dots are the stars (filled circles with radii of 5 pixel for YELLOW, 7 pixels for BLUE and, 9 pixels for RED). Their coordinates are (3.0e10, -7.5e10) for YELLOW, (-4.5e10, +3.0e10) for BLUE and (+4.5e10, +9.0e10) for RED. The masses of the stars are 2.5e30 kg for YELLOW, 9.5e30 kg for BLUE, and 1.5e31 for RED.
  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. Note that the circle end of the line is not at the grid point; it is at the other end of the line! That's because it represents a tiny arrowhead of a vector.
  5. The length of the line in the x-direction is determined by adding together the x-accelerations that a ship would feel at that point from all three stars. Multiply the sum of these three accelerations times 5.0e10 to get the length of the line in the x-direction (measured in meters).
  6. Do a similar thing to get the length of the line in the y-direction.
  7. Note: Any line with a total length that is longer than WMAX/7.07107 is not drawn (because those lines are so long that it messes up the pretty picture).
After the program finished drawing the chart, it should stay open for 60 additional seconds and then close with EXIT_SUCCESS.

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. I even have a function that draws a colored blob on the screen with a location that is specified in world coordinates.
  6. Most numbers that are 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.
  7. Exception: The coordinates of the three stars must be stored in six variables in the main program (not constants) because we plan to change these coordinates in the future.