function human_data ( )

%*****************************************************************************80
%
%% human_data() prompts the user to outline a human figure being displayed.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    20 May 2021
%
%  Author:
%
%    John Burkardt
%
%  Reference:
%
%    Cleve Moler,
%    Numerical Computing with MATLAB,
%    SIAM, 2004,
%    ISBN13: 978-0-898716-60-3,
%    LC: QA297.M625. 
%
%  Local:
%
%    real X(*), Y(*): the coordinates of the selected boundary points.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'human_data():\n' );
  fprintf ( 1, '  This function will help you record the outline of a\n' );
  fprintf ( 1, '  human body as a pair of X, Y data vectors.\n' );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  In a moment, the human body image will appear.\n' );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Your job is to select a sequence of points along this outline,\n' );
  fprintf ( 1, '  so that the straight line segments connecting these points will\n' );
  fprintf ( 1, '  approximate the shape smoothly.\n' );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  You can start at the top of the head, moving counterclockwise,\n' );
  fprintf ( 1, '  clicking on points you select, until you get to the crotch.\n' );
  fprintf ( 1, '  The program will copy your left hand data to complete the image.\n' );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Press RETURN to begin recording the boundary!\n' );
  pause ( )

  fprintf ( 1, '\n' );
  fprintf ( 1, '  Start at the top of the head.\n' );
  fprintf ( 1, '  Click on points, moving counterclockwise.\n' );
  fprintf ( 1, '  Stop (halfway) at the crotch.\n' );
  fprintf ( 1, '  Press RETURN to signal you are done.\n' );

  I = imread ( 'human_outline.jpg' );
  imshow ( I );
  [ x, y ] = ginput ( );
  clf ( );
%
%  Need to make reflected copy of x.
%
  n = length ( x );
  xmax = max ( x );
  x2 = 2.0 * xmax - x(n-1:-1:2);
%
%  Need to invert y because the coordinate system is counterintuitive.
%
  y_max = max ( y );
  y = y_max - y;
  y2 = y(n-1:-1:2);
%
%  Now append the reflected data for right side of body.
%
  x = [ x; x2 ];
  y = [ y; y2 ];

  fprintf ( 1, '  Left hand data reflected to complete the image.\n' );
%
%  Rescale the data:
%
  xmax = max ( x );
  xmin = min ( x );
  xmean = mean ( x );
  ymax = max ( y );
  ymin = min ( y );

  x = 1.5 * ( x - xmean ) / ( xmax - xmean );
  y = 6.0 * ( y - ymin ) / ( ymax - ymin );

  fprintf ( 1, '  Data rescaled to 0 <= x <= 3, 0 <= y <= 6.\n' );
%
%  Save data.
%
  filename = 'human_points.txt';
  v = [ x, y ];
  save ( filename, 'v', '-ASCII' );

% n = length ( x );
% file_unit = fopen ( filename, 'wt' );
% for i = 1 : n
%   fprintf ( file_unit, '%6.2f  %6.2f\n', x(i), y(i) );
% end
% fclose ( file_unit );

  fprintf ( 1, '\n' );
  fprintf ( 1, '  (x,y) boundary data saved as "%s"\n', filename );
%
%  Display points only.
%
  clf ( );
  plot ( x, y, 'r.', 'Markersize', 10 );
  axis ( 'equal' );
  grid ( 'on' );

  fprintf ( 1, '\n' );
  fprintf ( 1, '  You selected these %d points.\n', n );
  fprintf ( 1, '  Press RETURN to join the points with straight lines.\n' );
  pause ( );
  filename = 'human_points.png';
  print ( '-dpng', filename );
  fprintf ( 1, '  Graphics saved as "%s"\n', filename );
%
%  Display points and lines.
%
  clf ( );
  hold ( 'on' );
    plot ( [x;x(1)], [y;y(1)], 'b-', 'linewidth', 2 );
    plot ( x, y, 'r.', 'Markersize', 20 );
  hold ( 'off' );
  axis ( 'equal' );
  grid ( 'on' );
  fprintf ( 1, '  Press RETURN to finish.\n' );
  pause ( );
  filename = 'human_lines.png';
  print ( '-dpng', filename );
  fprintf ( 1, '  Graphics saved as "%s"\n', filename );
%
%  Close the figure.
%
  close ( );

  fprintf ( 1, '\n' );
  fprintf ( 1, '  You can retrieve the (x,y) data by the command\n' );
  fprintf ( 1, '    [ x, y ] = load ( ''human_points.txt'' );\n' );
%
%  Terminate.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'human_data():\n' );
  fprintf ( 1, '  Normal end of execution.\n' );

  return
end

