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 GNU LGPL license.
#
#  Modified:
#
#    08 February 2023
#
#  Author:
#
#    Original MATLAB version by Paul Nahin.
#    This version by John Burkardt.
#
#  Reference:
#
#    Paul Nahin,
#    Dueling Idiots and Other Probability Puzzlers,
#    Princeton, 2012,
#    ISBN: 978-0691155005.
#
  print ( '' )
  print ( 'needle():' )
  print ( '  A circular table top has radius r.' )
  print ( '  A needle of length 2*a <= 2*r is dropped onto the table.' )
  print ( '  * how many times does one end stick out over the edge?' )
  print ( '  * how manu times do both ends stick out?' )
  print ( '  * how many times does neither end stick out?' )
  print ( '' )
  print ( '  Report the results graphically.' )

  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 ( 'on' )
  title ( 'Probability Neither End of Needle Sticks-Out Over Edge' )
  filename = 'needle1.png'
  plt.savefig ( filename )
  print ( '  Graphics saved as "' + filename + '"' )
  plt.close ( )

  figure(2)
  plot ( ratio, prob2 )
  xlabel('a/r')
  ylabel('probability')
  grid ( 'on' )
  title('Probability One End of Needle Sticks-Out Over Edge')
  filename = 'needle2.png'
  plt.savefig ( filename )
  print ( '  Graphics saved as "' + filename + '"' )
  plt.close ( )

  figure(3)
  plot ( ratio, prob3 )
  xlabel('a/r')
  ylabel('probability')
  grid ( 'on' )
  title('Probability Both Ends of Needle Stick-Out Over Edge')
  filename = 'needle3.png'
  plt.savefig ( filename )
  print ( '  Graphics saved as "' + filename + '"' )
  plt.close ( )

  return
end

