/*--------------------------------------------------------------------
   Elizabeth Sklar
   2 Oct 2001

   CS1007 Homework #2 Solution

   file name: hw2eis2003.java

   This program reads two pairs of coordinates as input, from the 
   command line, representing the locations of a robot and a soccer
   ball within a rectangular field. The program calculates the 
   distance between the robot and the soccer ball. The program also 
   determines the angle of rotation required for the robot to turn
   from facing due east (0 degrees) in the direction of the ball,
   rotating counterclockwise.
--------------------------------------------------------------------*/

import java.lang.*;

public class hw2eis2003 {
    
    public static void main ( String[] args ) {

	// declare variables
	Integer tmp;
	int     x_robot = 0, y_robot = 0, x_ball = 0, y_ball = 0;
	int     dx, dy;
	double  distance;
	double  tmp2;
	double  angle, angle_deg;
	
	// ---- STEP 1 -----
	// check if enough arguments were entered
	if ( args.length < 4 ) {
	    System.out.println( 
	     "usage: java hw2 x_robot y_robot x_ball y_ball" );
	}
	else {
	    // ---- STEP 2 -----
	    // convert the input args of type String into int;
	    // you have to go String->Integer->int to get there.
	    // there are many ways to do this. here's a few...
	    
	    // (1) what we did in class; implicitly converting to 
	    //     Integer:
	    tmp = new Integer( args[0] );
	    x_robot = tmp.intValue();
	    
	    // (2) this works too; implicitly converting to Integer:
	    tmp = Integer.valueOf ( args[1] );
	    y_robot = tmp.intValue();
	    
	    // (3) this is more concise, but it is good to trim() the
	    //     args String, which makes it appear more complicated
	    x_ball = Integer.parseInt( args[2].trim() );
	    y_ball = Integer.parseInt( args[3].trim() );
	    
	    // ---- STEP 3 -----
	    // echo input
	    System.out.println( "robot is at position ("+x_robot+","+
				y_robot+")" );
	    System.out.println( "ball is at position ("+x_ball+","+
				y_ball+")" );
	    
	    // ---- STEP 4 -----
	    // determine the compass relationship between the robot 
	    // and the ball. i use nested if's below. you can also do
	    // this  with compound && statements like this:
	    //  if (( x_ball > x_robot ) && ( y_ball > y_robot )) etc.
	    // but we hadn't gone over this in class before the 
	    // assignment was due.
	    System.out.print( "the ball is " );
	    if ( x_ball > x_robot ) {
		if ( y_ball > y_robot ) {
		    System.out.print( "northeast" );
		}
		else if ( y_ball < y_robot ) {
		    System.out.print( "southeast" );
		}
		else {
		    System.out.print( "east" );
		}
	    }
	    else if ( x_ball < x_robot ) {
		if ( y_ball > y_robot ) {
		    System.out.print( "northwest" );
		}
		else if ( y_ball < y_robot ) {
		    System.out.print( "southwest" );
		}
		else {
		    System.out.print( "west" );
		}
	    }
	    else {
		if ( y_ball > y_robot ) {
		    System.out.print( "north" );
		}
		else if ( y_ball < y_robot ) {
		    System.out.print( "south" );
		}
		else {
                    System.out.print( "on top" );
		}
	    }
	    System.out.println( " of the robot." );

	    // ---- STEP 5 -----
	    // calculate the distance from the ball to the robot
	    dx = Math.abs( x_ball - x_robot );
	    dy = Math.abs( y_ball - y_robot );
	    distance = Math.sqrt( dx*dx + dy*dy );

	    // ---- STEP 6 -----
	    // calculate the angle between the ball and the robot;
	    // assume the robot is looking at 0 degrees, in the 
            // direction of the positive x axis (angle is in radians).
	    if ( distance != 0 ) {
		angle = Math.asin( dy/distance );
		// now adjust angle, since we did calculation (above) 
                // using absolute value, which forces the ball to be
		// effectively northeast of the robot for the 
		// calculation; but we need to adjust the rotation 
		// angle for the robot so it will turn from facing 
		// east to facing the ball.
		if ( x_ball > x_robot ) {
		    if ( y_ball >= y_robot ) {
			// no adjustment is needed, since the ball 
			// really is northeast of the robot
		    }
		    else {
			// the ball is southeast of the robot, so
			// adjust by 270 degrees
			angle = angle + (3*Math.PI/2); 
		    }
		}
		else if ( x_ball < x_robot ) {
		    if ( y_ball > y_robot ) {
			// the ball is northwest of the robot, so
			// adjust by 90 degrees
			angle = angle + (Math.PI/2);

		    }
		    else {
			// the ball is southwest of the robot, so
			// adjust by 180 degrees
			angle = angle + Math.PI; 
		    }
		}
		else { // ( x_ball == x_robot )
		    if ( y_ball <= y_robot ) {
			// the ball is south of the robot, so 
			// adjust by 180 degrees
			angle = angle + Math.PI;
		    }
		}
	    }
	    else {
		// the ball is on top of the robot, so avoid divide
		// by zero errors
		angle = 0;
	    }
	
	    // ---- STEP 7 -----
	    // convert angle to degrees
	    angle_deg = angle*180/Math.PI;

	    // ---- STEP 8 -----
	    // print out results
	    System.out.println( "distance="+distance );
	    System.out.println( "angle="+angle+" radians" );
	    System.out.println( "     ="+angle_deg+" degrees" );
	    
	} // end else
       
	// done!
	System.exit( 0 );

    } // end main

} // end class hw2eis2003
