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?
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  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.0 : 0.01 : 1.0 );
  factor1 = asin ( ratio );
  factor2 = sqrt ( 1.0 - ratio .^ 2 );
  prob1 = 1.0 - ( 2.0 / pi ) * ( ratio .* factor2 + factor1 );
  factor3 = sqrt ( 1 - 0.25 * ratio .^ 2 );
  factor4 = asin ( 0.5 * ratio );
  prob3 = ( 2 / pi ) * ( ratio .* factor3 + 2 * factor4 - ratio .* factor2 - factor1 );
  prob2 = 1 - ( prob1 + prob3 );
%
%  Graphics.
%
  clf ( );
  plot ( ratio, prob1, 'linewidth', 3 );
  xlabel ( 'a/r' );
  ylabel ( 'probability' );
  grid ( 'on' );
  title ( 'Probability Neither End of Needle Sticks-Out Over Edge' );
  filename = 'needle1.png';
  print ( '-dpng', filename );
  fprintf ( '  Graphics saved as "%s"\n', filename );

  clf ( );
  plot ( ratio, prob2, 'linewidth', 3 );
  xlabel ( 'a/r' );
  ylabel ( 'probability' );
  grid ( 'on' );
  title ( 'Probability One End of Needle Sticks-Out Over Edge' );
  filename = 'needle2.png';
  print ( '-dpng', filename );
  fprintf ( '  Graphics saved as "%s"\n', filename );

  clf ( );
  plot ( ratio, prob3, 'linewidth', 3 );
  xlabel ( 'a/r' );
  ylabel ( 'probability' );
  grid ( 'on' );
  title ( 'Probability Both Ends of Needle Stick-Out Over Edge' );
  filename = 'needle3.png';
  print ( '-dpng', filename );
  fprintf ( '  Graphics saved as "%s"\n', filename );

  return
end

