


disp(' ')
disp('Press space bar, when prompted, for a "no go"')
disp('Press a mouse button, when prompted, for a "go"')
rate_bogey = input('Please enter a speed between 0 and 1: ');
height     = input('Please enter a height: ');


% Distance = rate * time, so
% total time to get to (1,1)
% is Distance/rate. We want 
% .10 second intervals, so
% we want 10/rate_bogey + 1 (for the 
% 'fencepost') intervals

n = ceil(10/rate_bogey + 1);
time_bogey = linspace(0,1/rate_bogey,n)';

x_1 = (rate_bogey * time_bogey);
y_1 = ones(n,1);
z_1 = height * ones(n,1);

for m=1:n

   user_input = waitforbuttonpress;

   % The user wants to wait for launch:
   if user_input == 1
      hold on
      axis([-.1 1.1 -.1 1.1 -.1 height+.1])
      plot3(x_1(m),y_1(m),z_1(m),'k>');
      hold off
      pause(.1)

   % Time to launch our projectile:
   else
 
      t_0 = (m-1)/10;

      if ((height/sqrt(1+height*height)) + sqrt(1+height*height)/8 > 1 | (height/sqrt(1+height*height)) + sqrt(1+height*height)/8 < -1)
         disp('Impossible... the bogey is too high!')
         break
      end

      [thi,theta,collision_time] = newt(t_0,rate_bogey,height);

      if (theta < atan(2) | theta > pi - atan(2))
 
	 disp('Sorry, Charlie, you are too late!')
	 break;
      end
      
      intervals = round(10*(collision_time-t_0));
      launch_time = (linspace(0,collision_time-t_0,intervals+1))';
      x_2 = 1/2 + 2*(launch_time)*cos(theta)*cos(thi);
      y_2 = 2*(launch_time)*sin(theta)*cos(thi);
      z_2 = 2*(launch_time)*sin(thi) - (1/2)*(launch_time).*(launch_time);


      for final=1:intervals
  	hold on
        axis([-.1 1.1 -.1 1.1 -.1 height+.1])
        plot3(x_1(m+final-1),y_1(m+final-1),z_1(m+final-1),'k>',x_2(final),y_2(final),z_2(final),'b^')
        pause(.1)
      end
	plot3(x_2(final+1),y_2(final+1),z_2(final+1),'r*')
 	hold off

      break;
   end

end
