import java.util.Scanner;
/*******************************************************************************/
/**
 * What follows is a lengthy explanation of the physics, which is necessary to
 * understand what the computation is doing.
 * 
 * One of the standard exercises in a freshman calculus class is that a
 * projectile fired into the air travels the greatest horizontal distance when
 * the angle at which it is fired is 45 degrees to the horizontal.
 *
 * This can be seen computationally with this program.
 *
 * The physics of the problem is this. If a projectile is fired at an angle of
 * 'alpha' up from the horizontal with a velocity of 'vel' meters per second,
 * then the initial (and continual) horizontal velocity is
 *    velX = vel * cos(alpha)
 * Since there are no forces acting horizontally, this is the constant horizontal
 * velocity for the duration of the flight of the projectile.
 *
 * The initial vertical velocity is
 *    initVelY = vel * sin(alpha)
 * upward, but that velocity is acted upon by gravity at 9.8 meters per second
 * per second * downward. The vertical acceleration is thus -9.8 and the
 * vertical velocity at any time 't' is
 *    velY =  -9.8*t + initVelY.
 *
 * Given an initial velocity 'vel' at an angle 'alpha', we can solve for the
 * time 't' at which the vertical velocity becomes zero. This will be the point
 * at which the projectile stops rising and begins falling. Due to the symmetry
 * of the trajectory, this time 't' is exactly halfway into the total flight,
 * and the projectile will have travelled exactly halfway along its horizontal
 * path at this point, since the horizontal velocity is a constant.
 *
 * In a computer, of course, we need to have time steps of some fixed size,
 * so that will be an input to the program as well as the angle 'alpha'.
 * And we will accept the fact that we won't know that the vertical velocity
 * has become zero until (probably) it goes negative.
 *
 * So it goes with numerical programming; answers just don't come out exact.
 *
 * @author Duncan Buell
 * 5 February 2008
**/

public class TrajectoryMain
{
  public static void main (String[] args)
  {
    double alphaDegrees,alphaRadians,halfwayTime,initVel,initVelY,t,timeStep,velX,velY;
    Scanner console = new Scanner(System.in);

    System.out.println("Enter the angle alpha (in degrees) at which the projectile is fired: ");
    alphaDegrees = console.nextDouble();
    alphaRadians = alphaDegrees * Math.PI / 360.0;
    if((alphaDegrees <= 0) || (alphaDegrees > 90))
    {
      System.out.println("ERROR:  Input value " + alphaDegrees + " for 'alpha' is not between 0 and 90 degrees");
      System.exit(0);
    }

    System.out.println("Enter the time step for the simulation: ");
    timeStep = console.nextDouble();
    if(timeStep <= 0)
    {
      System.out.println("ERROR:  Input value " + timeStep + " for 'timeStep' is negative");
      System.exit(0);
    }

// this is a somewhat generic program, so we assume an initial velocity of 1000 meters per second
    initVel = 1000.0;
    velX = initVel * Math.cos(alphaRadians);
    initVelY = initVel * Math.sin(alphaRadians);

    System.out.println("Fire the projectile at " + alphaDegrees + " which is " + alphaRadians + " radians");
    System.out.println("Run the simulation in time increments of " + timeStep + " seconds");
    System.out.println("Initial (and final) horizontal velocity is " + velX + " meters per second");
    System.out.println("Initial vertical velocity is " + initVelY + " meters per second");

// and we're going to do all the work in the main method, just to keep things simple
    t = 0.0;
    halfwayTime = 0.0; // Eclipse complains if we don't initialize variables outside of loops
    velY = initVelY;
    while(velY > 0.0)
    {
      t = t + timeStep;
      velY =  -9.8*t + initVelY;
      System.out.println("At time " + t + " the vert vel is " + velY + " and horiz dist is " + t*velX);
    } // while(velY > 0.0)
    halfwayTime = t;

    System.out.println("Halfway point of the flight is approximately " + halfwayTime + " seconds");
    System.out.println("Horizontal distance travelled in full flight is approximately " + 2.0*velX*halfwayTime);

  }
} // public class TrajectoryMain

