// File: projectile.cxx // Written by: Michael Main (main@colorado.edu) on Sep 27, 2011 // The purpose of this program is to show // how to draw the path of a free-falling projectile. //------------------------------------------------------------- // Directives: #include #include #include #include #include using namespace std; //------------------------------------------------------------- //------------------------------------------------------------- const int S = 500; // Screen size in pixels const double W = 200.0; // World coordinates go from -W to +W //------------------------------------------------------------- //------------------------------------------------------------- // Function Prototypes. // pixel(v, v0, v1, p0, p1) converts v from an interval that // ranges from v0 to v1 into an integer interval that ranges // from p0 to p1. int pixel(double v, double v0, double v1, int p0, int p1); // Draws the path of a projectile in a square graphics window // of size pmax. The world coordinate system goes from // -wmax to +wmax, with the origin in the center of the screen. // The projectile is launched from the origin at an angle of // theta radians and with a speed of v meters per second. The // strength of gravity is 9.8 m/sec^2. void projectile(int pmax, double wmax, double theta, double v); //------------------------------------------------------------- //------------------------------------------------------------- int main() { double speed; // Initial speed of the projectile // Open the window and draw the cliff initwindow(S, S, "Projectile"); setfillstyle(SOLID_FILL, GREEN); bar (0, S/2, S/2, S); // Try different initial speeds, but all at the same angle // of 45 degrees. for (speed = 5; speed < 50; speed += 5) { projectile(S, W, M_PI/4.0, speed); } // Pause and exit delay(20000); return EXIT_SUCCESS; } //------------------------------------------------------------- //------------------------------------------------------------- int pixel(double v, double v0, double v1, int p0, int p1) { return int(p0 + (v - v0)/(v1 - v0) * (p1 - p0)); } //------------------------------------------------------------- //------------------------------------------------------------- void projectile(int pmax, double wmax, double theta, double v) { const double G = -9.8; // Strength of gravity in m/sec^2 double t; // Time in seconds double wx, wy; // Position of the projectile in meters int px, py; // Pixel coordinates of the projectile double vx = v * cos(theta); // Initial x velocity in m/sec double vy = v * sin(theta); // Initial y velocity in m/sec // Move the drawing pen to the center of the screen: moveto(pmax/2, pmax/2); // For t values starting at t=0, draw a line to the point // of the curve that is determined by t. The curve is drawn // until the y coordinate falls off the bottom. t = 0; do { wx = vx*t; wy = vy*t + 0.5*G*t*t; px = pixel(wx, -wmax, +wmax, 0, pmax); py = pixel(wy, +wmax, -wmax, 0, pmax); lineto(px, py); t += 0.01; } while (wy > -wmax); } //-------------------------------------------------------------