CSCI 1300
Introduction to Computer Science
Homework 4: Orbit Simulation


This is not part of the assignment, but it is a cool image from Zina Deretsky of the National Science Foundation. It compares the orbits of the known planets around Gliese 581 (20 light years away) to our own solar system:


Due Date

The due date is 10pm on Friday, September 30.

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

You programming style must follow the ten essential style items listed at www.portmain.com/intro/style.html.

The Assignment

Write a program that uses the orbit_angle, orbit_dist and pixel functions from the previous homework to draw a graphical simulation of at least TWO planets in orbit around a star. The program must:

  1. Not have any input (no cin statements). When the TA starts your program, it should immediately open a graphics window with the moving planets!

  2. The size of the window must be S (a named constant), with double buffering turned on.

  3. The program runs an animation loop. The loop is controlled by a time variable, t, that starts at zero days. Each iteration of the loop does these things:

    • Draw the next frame, consisting of the sun in the center of the screen and the planets in their location that is determined by t. The sun should be a filled in yellow circle (use the fillellipse function with the two radii equal to each other). The planet(s) should be smaller than the sun. Each planet must be a different color.

    • Swap buffers and delay for some an amount of time that will give exactly 30 frames per second.

    • Add some number of days to t. (A bigger increment to t will cause the simulation to run faster.)

The difficult step is drawing the planet at the right location. This must be done by a void function that you write:

draw_planet(a, ecc, period, t, and maybe more parameters);

The parameters are:

You will have to look up these pieces of information if you want to simulate real planets. The extra parameters could tell the planet's size or color or starting angle (at t==0) or anything else that your creative program needs.

The function uses Kepler's method to compute the polar coordinates (r, theta) of the planet in astronomical units at time t. It then converts those values to Cartesian coordinates (x = r*cos(theta) and y = r*sin(theta)). Finally, it converts to pixel coordinates before drawing. For this last conversion, you'll have to figure out what you want for your maximum and minimum x and y coordinates in terms of the solar system. What units will be convenient for you to use? Where will the sun be in this coordinate system?

The animation loop must be infinite (while (true)...), so that the only way to end the program is to click the close X or press ctrl-C.

Grading Criteria

  1. Your program must be called orbit.cxx. You must submit this file and no other.
  2. You must meet all the style requirements.
  3. Your code compiles correctly with no warnings.
  4. The behavior of your program must be correct.
  5. Additional requirement #1: You must use double buffering. Each iteration of the animation loop takes 1/30th of a second and simulates some number of days of the actual orbit.
  6. Additional requirement #2: You must have a function to draw the planet with the exact prototype show above for the first four parameters. You must also make use of the functions from the previous assignment (but not the sr function).