1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
|
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
const double pi = 3.14159265;
const double grav = 9.8; // Earth gravity (m/s^2)
// Given time, angle, velocity, and gravity
// Update x and y values
void Trajectory(double t, double a, double v,
double& x, double& y) {
x = v * t * cos(a);
y = v * t * sin(a) - 0.5 * grav * t * t;
return;
}
// convert degree value to radians
double DegToRad(double deg) {
return ((deg * pi) / 180.0);
}
// print time, x, and y values
void PrintUpdate(double t, double x, double y) {
cout << "Time " << fixed << setprecision(0)
<< setw(3) << t << " x = " << setw(3)
<< x << " y = " << setw(3) << y << endl;
return;
}
void PrintIntro(){
cout << "Welcome to Upset Fowl!\n";
cout << "The objective is to hit the mean Swine by launching an Upset Fowl.\n";
}
void GetUsrInput(double swineX, double fowlAngle, double fowlVel){
cout << "\nThe Mean Swine is " << swineX << " meters away.\n";
cout << "Enter fowl launch angle (deg): ";
cin >> fowlAngle;
fowlAngle = DegToRad(fowlAngle); // convert to radians
cout << "Enter fowl launch velocity (m/s): ";
cin >> fowlVel;
}
double LaunchFowl(double fowlAngle, double fowlVel){
double t = 1.0; // time (s)
double fowlX = 0.0; // object's horiz. dist. from start (m)
double fowlY = 0.0; // object's height above ground (m)
double fowlLandingX = 0.0;
do {
PrintUpdate(t, fowlX, fowlY);
Trajectory(t, fowlAngle, fowlVel, fowlX, fowlY);
t=t+1.0;
} while ( fowlY > 0.0 ); // while above ground
PrintUpdate(t, fowlX, fowlY);
fowlLandingX = fowlX;
return fowlLandingX;
}
bool DtrmIfHit(double fowlLandingX, double swineX){
bool didHitSwine = false;
double beforeSwineX = 0.0;
beforeSwineX = swineX - 30;
if ((fowlLandingX <= swineX) && (fowlLandingX >= beforeSwineX)) {
cout << "Hit'em!!!" << endl;
didHitSwine = true;
} else {
cout << "Missed'em..." << endl;
didHitSwine = false;
}
return didHitSwine;
}
int main() {
double fowlAngle = 0.0; // angle of launch of fowl (rad)
double fowlVel = 0.0; // velocity of fowl (m/s)
double fowlLandingX = 0.0;
double swineX = 0.0; // distance to swine (m)
bool didHitSwine = false; // did hit the swine?
srand(time(0));
swineX = (rand() % 201) + 50;
PrintIntro();
GetUsrInput(swineX, fowlAngle, fowlVel);
fowlLandingX = LaunchFowl(fowlAngle, fowlVel);
didHitSwine = DtrmIfHit(fowlLandingX, swineX);
do {
LaunchFowl(fowlAngle, fowlVel);
DtrmIfHit(fowlLandingX, swineX);
GetUsrInput(swineX, fowlAngle, fowlVel);
}
while (didHitSwine == false);
return 0;
}
| |