#include #include #include #include const double DELTA_T = .05; // increment of time in seconds const double GX = 0; // x component of gravitational acceleration const double GY = -32.2; // y component of gravitational acceleration const int XDIM = 500; const int YDIM = 500; void display_state(int t, double px, double py, double vx, double vy); void run_simulation(double px, double py, double vx, double vy); void main() { double cannon_angle, vel; double px, py, vx, vy; // initialize simulation cout << "Enter cannon angle: "; cin >> cannon_angle; cout << "Enter initial velocity of cannonball: "; cin >> vel; initwindow(XDIM, YDIM); px = 0; py = 0; vx = vel * cos(cannon_angle * 3.14159 / 180); vy = vel * sin(cannon_angle * 3.14159 / 180); run_simulation(px, py, vx, vy); while (!kbhit()) ; } ////////////////////////// run_simulation ////////////////////////////////////// void run_simulation(double px, double py, double vx, double vy) { int t, crash; for (crash=t=0;!crash;++t) { display_state(t, px, py, vx, vy); if (py < 0 || px >= XDIM) crash = 1; vx += DELTA_T * GX; vy += DELTA_T * GY; px += DELTA_T * vx; py += DELTA_T * vy; } } ////////////////////////// display_state ////////////////////////////////////// void display_state(int t, double px, double py, double vx, double vy) { static double prevx = 0, prevy = 0; //cout << "t = " << t*DELTA_T //cout << " pos = " << px << " " << py; //cout << " vel = " << vx << " " << vy; //cout << endl; circle(int(px),YDIM-int(py),10); line(int(px),YDIM-int(py),int(prevx),YDIM-int(prevy)); prevx = px; prevy = py; }