#!/usr/bin/python

#
# Finds the correct angle and velocity to fire
# a projectile so that its trajectory goes through
# the two given points and initial height. This was written specifically
# for the online app at
#    http://www.mathplayground.com/ProjectTRIG/ProjectTRIGPreloader.html
#
# Step 1.
#   Solve for b and d for the equation
#      y = -4.5*(x/d)^2 + bx/d + H
#
# Step 2.
#   Use b and d to compute the initial velocity and angle
#
# Author: Qiyam Tung
#

import sys
import os
import math 

def print_usage():
  print "./quadratic_solver.py x1 y1 x2 y2 H"

def main():
  if len(sys.argv) <> 6:
    print_usage()
    sys.exit(1)
  
  x1 = float(sys.argv[1])  
  y1 = float(sys.argv[2])  
  x2 = float(sys.argv[3])  
  y2 = float(sys.argv[4])  
  H  = float(sys.argv[5])  

  # Solving for the parameters of the quadratic equation
  A = H-y2 - (H-y1)*x2/x1
  B = 0
  C = 4.9*x1*x2 - 4.9*(x2**2)
  
  d1 = -B+math.sqrt(B**2 - 4*A*C)/(2*A)
  b1 = 4.9*x1/d1 - d1*(H-y1)/x1

  d2 = -B-math.sqrt(B**2 - 4*A*C)/(2*A)
  b2 = 4.9*x1/d2 - d2*(H-y1)/x1
 
  # Determine which solution makes sense.
  if d1 > 0 and b1 > 0:
    d = d1
    b = b1
  elif d2 > 0 and b2 > 0:
   d = d2
   b = b2
  else :
    print 'no solution'
    sys.exit(1)

  # Find the angle and velocity
  vx = d
  vy = b
  velocity = math.sqrt(vx*vx + vy*vy)
  angle = math.atan(vy/vx) * 180/math.pi
  print 'Angle:    ' + str(angle)
  print 'Velocity: ' + str(velocity)


if __name__ == "__main__":
 main()
