/* @JUDGE_ID: 8962YR 203 C++ "navigate system" */ /* simple running light calculator masthead light 0 - 360 stern light 110 - 250 red running light 245 - 2.5 green running light 357.5 - 115 >= 10nm not visible Formula: distance = sqrt((y2-y1)^2 + (x2-x1)^2) rearing(angle) = 360 - (course - atan(abs(y2-y1)/(x2-x1)) + 90 ) after get angle then choose which light in */ #include #include #include #include #define PI 3.141592653589793238 using namespace std; class Ship{ private: string id; double x; double y; double course; double speed; double distance; double bearing; string lights; public: Ship(){} Ship(string i, double x1, double y1, double c, double s){ id = i; x = x1; y = y1; course = c; speed = s; } void initialize(string i, double x1, double y1, double c, double s){ id = i; x = x1; y = y1; course = c; speed = s; } // s1 = owner double getDistance(Ship& s1, Ship& s2){ distance = sqrt( pow((s2.y - s1.y),2) + pow( (s2.x - s1.x),2) ); return distance; } // s1 = owner double getBearing(Ship& s1, Ship& s2){ //angle between two vector // and double dot = (s1.y-s2.y)*cos(s2.course*2*PI/360.0) + (s1.x-s2.x)*sin(s2.course*2*PI/360.0); double len = sqrt(pow((s1.y-s2.y),2)+pow((s1.x-s2.x),2)); double ang = 360/(2*PI) * acos( dot/len ); // now determine whether we shall 360 - ang // no need substract at range from a to (a+180) % 360 //a = s2.course double a; double m = (s2.y-s1.y)/(s2.x-s1.x) ; if(m<0) a = 360 - 360/(2*PI)*abs((int)atan(m)); else a = 360/(2*PI)*abs((int)atan(m)); double b = ((int)(a+180))%360; // so the range will from a to (a + 180)%360 // if s2.course fall in between good else ang= 360-ang // consider the direction of s2 is above s1 in y direction if(s2.y >s1.y){ if(a>b) { if(s2.course >=b && s2.course<=a) ang = 360-ang; } else { // if a pass through 0 line if(s2.course>=b && s2.course<=360 || s2.course>=0 && s2.course<=a) ang = 360 -ang; } } // s2 is below s1 in y direction else{ if(a=a && s2.course<=b) ang = 360-ang; } else { // if a pass through 0 line if(s2.course>=a && s2.course<=360 || s2.course>=0 && s2.course<=b) ang = 360 -ang; } } bearing = ang; return bearing; } /* masthead light 0 - 360 stern light 110 - 250 red running light 245 - 2.5 green running light 357.5 - 115 */ void getLights(){ if(distance>= 10) lights = "light not visible"; else{ lights = "masthead"; if(bearing >=110 && bearing <=250) lights += " stern"; if(bearing>=245 && bearing<=360|| bearing>=0 && bearing<=2.5) lights += " red"; if(bearing>=357.5 && bearing<=360 || bearing>=0&& bearing<=115) lights +=" green"; } } void passTime(int min){ x += cos(course*2.0*PI/360)*3.0/60; y += sin(course*2*PI/360)*3.0/60; if(x<0.00001&&x>=-0.00001) x = 0; if(y<0.00001&&y>=-0.00001) y = 0; //cout.precision(5); //cout<<"X "<>n; string name; double x,y,b,s; // read own ship fin>>name; fin>>x; fin>>y; fin >>b; fin>>s; Ship own(name,x,y,b,s); // now dynamic allocate n ship for it Ship *ship = new Ship[n]; int i; for( i=0; i>name; fin>>x; fin>>y; fin >>b; fin>>s; ship[i].initialize(name,x,y,b,s); ship[i].eval(own,ship[i]); ship[i].display(fout); } for(int j=0;j<10; j++){ // after 3minutes fout<