import java.io.*; import java.util.*; import java.awt.geom.*; /** * Solution to Collision Detection * * @author vanb */ public class collisiondetection2 { public Scanner sc; public PrintStream ps; public static final double EPSILON = 0.000001; public static final double TOLERANCE = 18.0*18.0; /** * All of the movement parameters of a car * * @author vanb */ public class Car { // Input parameters: Time, velocity, and (x,y) position for two readings public double v1, v2, t1, t2, x1, x2, y1, y2; // Computed things: // a = acceleration // cos, sin = cosine and sine of angle of movement // vf = velocity at time of freeze (when the car hits either 0 or 80) // tf = time of freeze // (xf,yf) = position of freeze public double a, cos, sin, vf, tf, xf, yf; public Car( double t1, double x1, double y1, double v1, double t2, double x2, double y2, double v2) { this.t1 = t1; this.x1 = x1; this.y1 = y1; this.v1 = v1; this.t2 = t2; this.x2 = x2; this.y2 = y2; this.v2 = v2; // Acceleration a = (v2-v1)/(t2-t1); // Angle double theta = Math.atan2( y2-y1, x2-x1 ); cos = Math.cos( theta ); sin = Math.sin( theta ); // Terminal velocity vf = Math.abs(a)0.0 ? 80.0 : 0.0; // Time of Freeze tf = Math.abs(a)tmax ) tmax = t2; Car car2 = new Car( t1, x1, y1, v1, t2, x2, y2, v2 ); // Find a time when the cars are within the tolerance of each other double lo = tmax; while( d2(car1, car2, lo) > TOLERANCE && lo<=tmax+30.0 ) lo += 0.00001; ps.println( lo>tmax+30.0 ? "Safe" : "Dangerous"); } } /** * @param args */ public static void main( String[] args ) throws Exception { new collisiondetection2().doit(args[0]); } }