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
|
#include <iostream>
#include <iomanip>
#include <cmath>
#include <algorithm>
using namespace std;
struct Pt { double x, y; };
Pt operator + ( Pt p, Pt q ){ return { p.x + q.x, p.y + q.y }; }
Pt operator - ( Pt p, Pt q ){ return { p.x - q.x, p.y - q.y }; }
Pt operator * ( double d, Pt p ){ return { d * p.x, d * p.y }; }
Pt operator / ( Pt p, double d ){ return { p.x / d, p.y / d }; }
ostream & operator << ( ostream & out, const Pt & p ){ return out << setw( 12 ) << p.x << " " << setw( 12 ) << p.y; }
double dot( Pt p, Pt q ){ return p.x * q.x + p.y * q.y; }
double mag( Pt p ){ return sqrt( dot( p, p ) ); }
//======================================================================
void intercept( Pt hunter, Pt prey, Pt DIRprey, double Vhunter, double Vprey, double &T, Pt &DEAD, Pt &DIRhunter )
{
double d = mag( prey - hunter );
double cosAngle = dot( DIRprey, hunter - prey ) / d;
double a = Vhunter * Vhunter - Vprey * Vprey;
double b = 2 * d * Vprey * cosAngle;
double c = -d * d;
T = ( -b + sqrt( b * b - 4 * a * c ) ) / ( 2.0 * a ); // Time to intercept
DEAD = prey + T * Vprey * DIRprey; // Position of interception
DIRhunter = ( DEAD - hunter ) / mag( DEAD - hunter ); // Direction taken by hunter
}
//======================================================================
int main()
{
const int SecondsPerHour = 3600;
Pt dog{ 0, 0 }, mongoose{ 0.1, 0 }, snake{ 0.1, 0.1 }, mouse{ 0, 0.1 }; // Initial coordinates (in km)
double Vdog = 30, Vmongoose = 18, Vsnake = 12, Vmouse =8; // Speeds (in km/h)
double Tmouse, Tsnake, Tmongoose; // Time until demise (hr)
Pt DEADmouse, DEADsnake, DEADmongoose; // Position of demise (km)
Pt DIRmouse , DIRsnake , DIRmongoose , DIRdog; // Unit direction vectors
DIRsnake = DIRmouse = ( mouse - snake ) / mag( mouse - snake ); // Snake heads for the mouse, who runs away
intercept( snake , mouse , DIRmouse , Vsnake , Vmouse , Tmouse , DEADmouse , DIRsnake );
intercept( mongoose, snake , DIRsnake , Vmongoose, Vsnake , Tsnake , DEADsnake , DIRmongoose );
intercept( dog , mongoose, DIRmongoose, Vdog , Vmongoose, Tmongoose, DEADmongoose, DIRdog );
cout << "Initial positions (km):\n"
<< "Dog: " << " " << dog << '\n'
<< "Mongoose: " << " " << mongoose << '\n'
<< "Snake: " << " " << snake << '\n'
<< "Mouse: " << " " << mouse << "\n\n";
cout << "Points of potential demise (km):\n"
<< "Mongoose: " << '\t' << DEADmongoose << '\t' << " at time " << Tmongoose * SecondsPerHour << " s" << '\n'
<< "Snake: " << '\t' << DEADsnake << '\t' << " at time " << Tsnake * SecondsPerHour << " s" << '\n'
<< "Mouse: " << '\t' << DEADmouse << '\t' << " at time " << Tmouse * SecondsPerHour << " s" << "\n\n";
double T = min( { Tmouse, Tmongoose, Tsnake } );
if ( Tmouse == T ) cout << "Dead mouse\n";
if ( Tsnake == T ) cout << "Dead snake\n";
if ( Tmongoose == T ) cout << "Dead mongoose\n";
}
| |