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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
|
#include <iostream>
using namespace std;
#include <math.h>
void DisplayMenu(void);
void getXgetY(double &x,double &y);
void pointsDistance(void);
double calculateDistance(double x1,double y1,double x2,double y2);
void horizontalAngle(void);
double calculateAngle(double x1,double y1,double x2,double y2);
void horizontalDistance(void);
double calculateHorizontalDistance(double angle, double velocity);
void destinationPoint(void);
void calculateAngleDistance(double x1, double y1, double distance,
double degree,double&x2,double&y2);
int main()
{
int option;
do
{
DisplayMenu();
cout<<"Enter option(1-5): ";
cin>>option;
if(option == 1)
pointsDistance();
else if(option == 2)
horizontalAngle();
else if(option == 3)
horizontalDistance();
else if(option == 4)
destinationPoint();
else if(option != 5)
cout<<endl<<"Please enter a number from 1 to 5"
<<endl<<endl;
}
while (option != 5);
cout<<"Goodbye!"<<endl;
return 0;
}
void DisplayMenu(void)
{
cout<<"Menu: "<<endl;
cout<<"1: Distance between two points"<<endl;
cout<<"2: Find horizontal angle between two points"<<endl;
cout<<"3: Horizontal distance traveled"<<endl;
cout<<"4: Option 4"<<endl;
cout<<"5: Exit program"<<endl;
}
void getXgetY(double&x,double&y)
{
cout<<"x-coordinate?"<<endl;
cin>>x;
cout<<endl;
cout<<"y-coordinate?"<<endl;
cin>>y;
}
void pointsDistance(void)
{
double x1,y1,x2,y2;
double distance;
cout<<endl<<"Point 1: "<<endl;
getXgetY(x1,y1);
cout<<"Point 2: "<<endl;
getXgetY(x2,y2);
distance=calculateDistance(x1,y1,x2,y2);
cout<<"Distance: "<<distance<<endl<<endl;
}
double calculateDistance(double x1,double y1,double x2,double y2)
{
double distance;
distance=sqrt(pow(x2-x1,2)+pow(y2-y1,2));
return distance;
}
void horizontalAngle(void)
{
double x1,y1,x2,y2,horAngle;
cout<<endl<<"Point 1: "<<endl;
getXgetY(x1,y1);
cout<<"Point 2: "<<endl;
getXgetY(x2,y2);
horAngle=calculateAngle(x1,y1,x2,y2);
cout<<"Horizontal angle is: "<<horAngle<<endl;
}
double calculateAngle(double x1,double y1,double x2,double y2)
{
const double PI=3.1415926;
double horAngleRad,horAngleDegree,dx,dy;
dx=x2-x1;
dy=y2-y1;
if (dx>0)
horAngleRad=atan(dy/dx);
else if(dx<0)
horAngleRad=atan(dy/dx) + PI;
else if(dx=0 && dy>=0)
horAngleRad=PI/2.0;
else
horAngleRad=-PI/2.0;
horAngleDegree=horAngleRad*180/PI;
return horAngleDegree;
}
void horizontalDistance(void)
{
double distance,elevationAngle,velocity;
cout<<"Elevation angle (in degrees): "<<endl;
cin>>elevationAngle;
cout<<endl<<"Velocity (in miles per hour): "<<endl;
cin>>velocity;
distance=calculateHorizontalDistance(elevationAngle,velocity);
cout<<"Horizontal distance traveled: "<<distance<<endl;
}
double calculateHorizontalDistance(double angle, double velocity)
{
const double PI=3.1415926;
const double gravity=32.172;
const double feet_per_mile=5280;
const double seconds_per_hour=3600;
double elevationAngleRad,horDistance;
elevationAngleRad=angle*PI/180.0;
velocity=velocity*feet_per_mile/seconds_per_hour;
horDistance=pow(velocity,2)*sin(2*elevationAngleRad)/gravity;
return horDistance;
}
void destinationPoint(void)
{
double x1,y1,x2,y2;
double travelDistance,horAngleDeg;
cout<<"Starting point: "<<endl;
getXgetY(x1,y1);
cout<<"Distance to travel (in feet)?"<<endl;
cin>>travelDistance;
cout<<"Horizontal angle (in degrees)?"<<endl;
cin>>horAngleDeg;
calculateAngleDistance(x1,y1,travelDistance,horAngleDeg);
cout<<"X-coordinate: "<<x2<<endl;
cout<<"Y-coordinate: "<<y2<<endl;
}
void calculateAngleDistance(double x1, double y1, double distance,
double degree,double&x2,double&y2)
{
double degreeRad,dx,dy;
const double PI=3.1415926;
degreeRad=degree*PI/180.0;
dx=distance*cos(degreeRad);
dy=distance*sin(degreeRad);
x2=x1+dx;
y2=y1+dy;
}
|