function needle ( )

%*****************************************************************************80
%
%% NEEDLE studies a needle dropped onto a table.
%
%  Discussion:
%
%    A circular table top has radius r.
%    A needle of length 2*a <= 2*r is dropped onto the table.
%    * how many times does one end stick out over the edge?
%    * how manu times do both ends stick out?
%    * how many times does neither end stick out?
%
%  Modified:
%
%    29 January 2018
%
%  Author:
%
%    Paul Nahin
%
%  Reference:
%
%    Paul Nahin,
%    Dueling Idiots and Other Probability Puzzlers,
%    Princeton, 2012,
%    ISBN: 978-0691155005.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'NEEDLE:\n' );
  fprintf ( 1, '  A circular table top has radius r.\n' );
  fprintf ( 1, '  A needle of length 2*a <= 2*r is dropped onto the table.\n' );
  fprintf ( 1, '  * how many times does one end stick out over the edge?\n' );
  fprintf ( 1, '  * how manu times do both ends stick out?\n' );
  fprintf ( 1, '  * how many times does neither end stick out?\n' );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Report the results graphically.\n' );

  ratio = ( 0 : .01 : 1);
  factor1 = asin ( ratio );
  factor2 = sqrt ( 1 - ratio .^ 2 );
  prob1 = 1 - ( 2 / pi ) * ( ratio .* factor2 + factor1 );
  factor3 = sqrt ( 1 - .25 * ratio .^ 2 );
  factor4 = asin ( .5 * ratio );
  prob3 = ( 2 / pi ) * ( ratio .* factor3 + 2 * factor4 - ratio .* factor2 - factor1 );
  prob2 = 1 - ( prob1 + prob3 );
%
%  Graphics.
%
  clf

  figure(1)
  plot ( ratio, prob1 )
  xlabel('a/r')
  ylabel('probability')
  grid
  title('Fig.11.3-Probability Neither End of Needle Sticks-Out Over Edge')
  print ( '-dpng', 'needle1.png' );

  figure(2)
  plot ( ratio, prob2 )
  xlabel('a/r')
  ylabel('probability')
  grid
  title('Fig.11.4-Probability One End of Needle Sticks-Out Over Edge')
  print ( '-dpng', 'needle2.png' );

  figure(3)
  plot ( ratio, prob3 )
  xlabel('a/r')
  ylabel('probability')
  grid
  title('Fig.11.5-Probability Both Ends of Needle Stick-Out Over Edge')
  print ( '-dpng', 'needle3.png' );

  return
end

