% cannon_until.m
%  A cannonball is fired at an angle ALPHA, with initial velocity V0.
%  Determine the time at which the cannonball hits the ground.
%
degrees = input ( 'Enter the angle, in degrees: ' );
angle = degrees * pi / 180.0;
x0 = input ( 'Enter x position of cannon: ' );
y0 = input ( 'Enter y position of cannon (Must be nonnegative!): ' );
if ( y0 < 0.0 )
  error ( 'I told you the y position must be nonnegative!' );
end
v0 = input ( 'Enter initial velocity, in feet/second: ' );

t = 0;

while ( true )

  x = x0 + v0 * cos ( angle ) * t; 
  y = y0 + v0 * sin ( angle ) * t - 16.0 * t^2;

  if ( y < 0.0 )
    break;
  end

  t = t + 1;

end

fprintf ( '%d seconds,  (%g, %g) feet\n', t, x, y );

