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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
|
// Gravity = G = 9.81 m/s^2
// PI = 3.1415927
#include <iostream>
#include <iomanip> //This is a library that will set the decimal precision
#include <cmath> //This is used so pow function can be called
#include "graphics.h" //This is so the graphics library can be used
using namespace std;
const double G = 9.81;
const double PI = 3.1415927;
double CONVERT (double degrees); //This is a function to convert from degrees
//to radians (value returning)
double TOTAL_DISTANCE (double speed, double angle); //This is a function to calculate
//total distance traveled by the projectile using the speed, angle,
//and gravity constant (value returning)
double HEIGHT (double angle, double speed, double TOTAL_DISTANCE);
//This is function to calculate the
//maximum height reached by the projectile (value returning)
void TRAJECTORY_PATH (double speed, double angle, double G); //This is a function
//that will graph the trajectory path using the BGI graphics library
int SCALE_Y (int height, int y); //This is a function to scale the y values
//so they will fit into the graphics console window
int SCALE_X (int distance, int x); //This is a function to scale the x
//values so they will fit into the graphics console window
int main()
{
/* request autodetection */
int gdriver = DETECT, gmode, errorcode;
/* initialize graphics mode */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
cout << "Graphics error: " << grapherrormsg(errorcode) << endl;
return 1;
}
cout << fixed << showpoint << setprecision(2); //So the decimal places throught
//the program will show two places behind the decimal
double user_speed, user_angle; //These are user entered values
double angle_radians; //This is the user_angle converted to radians
double cal_distance; //This is the distance the projectile travels
double cal_height; //This is the maximum height reached by the projectile
cout << "Trajectory Plotter!!" << endl; //Name of program
cout << "Enter a speed of launch (mps):"; //Prompts user to enter a launch speed
cin >> user_speed; //Reads in the user launch speed
cout << "Enter the angle of launch (in degees):"; //Prompts user to enter a launch
//angle
cin >> user_angle; //Reads in the user launch angle
cout << endl;
angle_radians = CONVERT (user_angle); //Will return the value for the angle that
//was given in degrees, in radians
cout << "Speed = " << user_speed << " mps, " << "Angle = " << user_angle;
cout << " degrees = " << angle_radians << " radians " << endl;
//This is going to output the users input values back to them, and also
//display the value for the angle in radians
cal_distance = TOTAL_DISTANCE (user_speed, angle_radians);
//This is going to give the value of the total distance traveled by the
//projectile
cal_height = HEIGHT (angle_radians, user_speed, cal_distance/2.0);
//This is going to give the value of the maximum height that was reached by the
//projectile
cout << "Distance = " << cal_distance << " m, height " << cal_height << " m";
//Outputs to the user the values that were calculated for distance and height
cout <<endl;
TRAJECTORY_PATH(user_speed, angle_radians, G);
return 0;
}
/*Returns the value of the angle in radians*/
double CONVERT (double degrees)
{
return degrees * (PI / 180);
}
/*Returns the value of the total distance traveled by the projectile*/
double TOTAL_DISTANCE (double speed, double angle)
{
return (pow (speed, 2) * sin ((2 * angle)))/G;
}
/*Returns the value of the maximum height reached by the projectile*/
double HEIGHT (double angle, double speed, double distance)
{
return distance * tan (angle) - (G * pow (distance, 2))/
(2 * pow ((speed * cos (angle)), 2));
}
/*Displays the projectile path on a set of axies*/
void TRAJECTORY_PATH (double speed, double angle, double G)
{
/*Declaring the variables that will be points on graph*/
int pt_x1, pt_x2, pt_x3, pt_x4, pt_x5;
int pt_y1, pt_y2, pt_y3, pt_y4, pt_y5;
/*Declaring the variables that will allow to put into graph SCALE_Y equation*/
double y2_at_x2, y3_at_x3, y4_at_x4;
/*The y and x axies of the graph*/
line (10, 0, 10, (getmaxy() - 10));
line (0, (getmaxy()- 10), (getmaxx() - 10), (getmaxy() - 10));
/*The following are the x points on the parabola*/
pt_x1= SCALE_X(TOTAL_DISTANCE, 0);
pt_x2 = SCALE_X(TOTAL_DISTANCE, ((.25) * TOTAL_DISTANCE));
pt_x3 = SCALE_X(TOTAL_DISTANCE, ((.50) * TOTAL_DISTANCE));
pt_x4 = SCALE_X(TOTAL_DISTANCE, ((.75) * TOTAL_DISTANCE));
pt_x5 = SCALE_X(TOTAL_DISTANCE, TOTAL_DISTANCE);
/*Calculating the y at x along the graph so it can be put into the above equation*/
y2_at_x2 = HEIGHT(angle_radians, user_speed, (.25 * TOTAL_DISTANCE));
y3_at_x3 = HEIGHT(angle_radians, user_speed, (.50 * TOTAL_DISTANCE));
y4_at_x4 = HEIGHT(angle_radians, user_speed, (.50 * TOTAL_DISTANCE));
/*The following are the y points on the parabola*/
pt_y1 = SCALE_Y(HEIGHT, 0);
pt_y2 = SCALE_Y(HEIGHT, y2_at_x2);
pt_y3 = SCALE_Y(HEIGHT, y3_at_x3);
pt_y4 = SCALE_Y(HEIGHT, y4_at_x4);
pt_y5 = SCALE_Y(HEIGHT, 0);
/*Points on the graph*/
circle (pt_x1, pt_y1, 5);
circle (pt_x2, pt_y2, 5);
circle (pt_x3, pt_y3, 5);
circle (pt_x4, pt_y4, 5);
circle (pt_x5, pt_y5, 5);
}
/*To scale the y values so they will fit better in the graphics console window*/
int SCALE_Y (double height, double y)
{
return (getmaxy() - 10)-(y * (getmaxy() - 20)) / height;
}
/*To scale the x values so they will fit better in the graphics console window*/
int SCALE_X (double distance, double x)
{
return 10 + ((getmaxx() - 20) * x) / distance;
}
| |