function e = hand_area ( s_num )

%*****************************************************************************80
%
%% hand_area() estimates the area of the hand in the hand data file.
%
%  Discussion:
%
%    This program assumes that the file 'HAND_NODES.TXT' is available.
%
%    It uses sampling to estimate the area contained within the hand outline.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    29 September 2015
%
%  Author:
%
%    John Burkardt
%
%  Reference:
%
%    Cleve Moler,
%    Numerical Computing with MATLAB,
%    SIAM, 2004,
%    ISBN13: 978-0-898716-60-3,
%    LC: QA297.M625. 
%
%  Input:
%
%    integer S_NUM, the number of sample points.
%
%  Output:
%
%    real E, the error in the area estimate.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'hand_area():\n' );
  fprintf ( 1, '  Estimate the area inside a curve that approximates the\n' );
  fprintf ( 1, '  shape of a hand, using sampling.\n' );
%
%  1: DATA
%

%
%  Read the hand data.
%  It comes in as an Nx2 array.
%
  xy = load ( 'hand_nodes.txt' );
%
%  Append a copy of the first row to the end of the array.
%  This will make the PLOT program happy when it draws the outline.
%
  xy = [ xy; [ xy(1,:) ] ];
%
%  We will want to know how many columns there are.  Use the SIZE command.
%
  xy_num = size ( xy, 1 );
%
%  Determine the range of the data.
%
  x_min = min ( xy(:,1) );
  x_max = max ( xy(:,1) );
  y_min = min ( xy(:,2) );
  y_max = max ( xy(:,2) );
%
%  Compute the area of the rectangle that contains the data.
%
  area_total = ( x_max - x_min ) * ( y_max - y_min );
%
%  2.  SAMPLING
%

%
%  Get an array of S_NUMx2 random values, each in [0,1].
%
  s = rand ( s_num, 2 );
%
%  Shift and stretch the points to fit in the rectangle.
%
  s(:,1) = x_min + s(:,1) * ( x_max - x_min );
  s(:,2) = y_min + s(:,2) * ( y_max - y_min );
%
%  Set up a vector Z to record if each sample point is inside (1)
%  or outside (0) the hand curve.
%
  z = zeros ( s_num, 1 ); 
  for j = 1 : s_num
    p = [ s(j,1); s(j,2) ];
    z(j) = polygon_contains_point_2d ( xy_num, xy, p );
  end
%
%  Use FIND to make a list of the points inside and outside the curve.
%
  j = find ( z == 1 );
  j_num = length ( j );

  k = find ( z == 0 );
  k_num = length ( k );
%
%  Estimate the hand area using the proportion of sample points that landed
%  inside the hand.
%
  area_hand = area_total * j_num / s_num;

  fprintf ( 1, '\n' );
  fprintf ( 1, '  Number of sample points = %d\n', s_num );
  fprintf ( 1, '  Total area = %g\n', area_total );
  fprintf ( 1, '  Estimated hand area = %g\n', area_hand );
%
%  Since the hand is a polygon, we can actually compute the exact area.
%
  area_hand2 = polygon_area_2d ( xy_num, xy );
  fprintf ( 1, '  Exact hand area = %g\n', area_hand2 );
  e = abs ( area_hand - area_hand2 );
  fprintf ( 1, '  Error = %g\n', e );
%
%  3: GRAPHICS
%

%
%  Clear any old graphics information from the screen.
%
  clf ( );
%
%  Prepare for multiple plot commands on one image.
%
  hold ( 'on' );
%
%  Plot:
%    the hand curve (cyan)
%    the hand data points (blue)
%    the sample points inside the hand (red)
%    the sample points outside the hand (green)
%
  plot ( xy(:,1), xy(:,2), 'c-', 'LineWidth', 4 );
  plot ( xy(:,1), xy(:,2), 'b.', 'MarkerSize', 15 );
  plot ( s(j,1), s(j,2), 'r.', 'Markersize', 15 );
  plot ( s(k,1), s(k,2), 'g.', 'Markersize', 15 );
%
%  Use the same scale for X and Y.
%  Draw grid lines.
%  Print a title.
%
  axis ( 'equal' );
  grid ( 'on' );
  title ( 'Samples inside (red) and outside (green) the hand data', 'Fontsize', 16 );
%
%  The plot is done.
%
  hold ( 'off' );
%
%  Save a copy to a file.
%
  filename = 'hand_area.png';
  print ( '-dpng', filename );
  fprintf ( 1, '  Graphics saved as "%s"\n', filename );

  return
end
