urgent

hi, please, I need someone to help me code with this. in a field there are four animals - a dog, a mongoose, a snake and a mouse. dogs kill mongoose, mongoose kill snakes and snakes kill mice. the speeds of the mouse, snake mongoose and dogs are, 8,12,18,30km/hour respectively. simulate the chase with different starting position to see which also
animal gets killed first. Thanks!
you need to put this in the jobs section along with your offered pay amount. You accidentally put this in the 'I tried to do it and need a little help' area.
¿how much risk is there for the mongoose when facing a cobra?
Assuming all animals :
- run in straight lines;
- have sufficient trigonometrical ability to intercept their prey in minimum time;
- don't know what's behind them;

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";
}


Initial positions (km):
Dog:                   0             0
Mongoose:            0.1             0
Snake:               0.1           0.1
Mouse:                 0           0.1

Points of potential demise (km):
Mongoose: 	   0.0690983     0.0345492	 at time 9.27051 s
Snake:    	   0.0105573           0.1	 at time 26.8328 s
Mouse:    	        -0.2           0.1	 at time 90 s

Dead mongoose
Last edited on
Topic archived. No new replies allowed.