function polygon_sample_test ( )

%*****************************************************************************80
%
%% polygon_sample_test() tests polygon_sample().
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    15 January 2025
%
%  Author:
%
%    John Burkardt
%
  addpath ( '../polygon_sample' );

  timestamp ( );

% rng ( 'shuffle' );

  fprintf ( 1, '\n' );
  fprintf ( 1, 'polygon_sample_test():\n' );
  fprintf ( 1, '  MATLAB/Octave version %s\n', version ( ) );
  fprintf ( 1, '  Test polygon_sample(), which uniformly samples points\n' );
  fprintf ( 1, '  in a polygon.\n' );

  n = 1000;

  v = [ ...
    0,   0; ...
   10,   7; ...
   12,   3; ...
   20,   8; ...
   13,  17; ...
   10,  12; ...
   12,  14; ...
   14,   9; ...
    8,  10; ...
    6,  14; ...
   10,  15; ...
    7,  18; ...
    0,  16; ...
    1,  13; ...
    3,  15; ...
    5,   8; ...
   -2,   9; ...
    5,   5 ];

  nv = size ( v, 1 );

  fprintf ( 1, '\n' );
  fprintf ( 1, '  Number of points N =    %12d\n', n );
  fprintf ( 1, '  Number of vertices NV = %12d\n', nv );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Polygonal vertices:\n' );
  disp ( v )

  p = polygon_sample ( nv, v, n );
  triangles = polygon_triangulate ( nv, v(1:nv,1), v(1:nv,2) );

  for picture = 1 : 4

    clf ( );
    hold ( 'on' );
%
%  Plot sample points.
%
    if ( 4 <= picture )
      plot ( p(:,1), p(:,2), 'bo', 'markersize', 2 );
    end
%
%  Plot triangulation lines.
%
    if ( 3 <= picture )
      for i = 1 : nv - 3
        i1 = triangles(i,1);
        i2 = triangles(i,2);
        line ( [ v(i1,1), v(i2,1) ], ...
               [ v(i1,2), v(i2,2) ], ...
               'LineWidth', 3, 'Color', 'm' );
      end
    end
%
%  Plot polygon edges.
%
    if ( 2 <= picture )
      im1 = nv;
      for i = 1 : nv
        plot ( [ v(im1,1), v(i,1) ], ...
               [ v(im1,2), v(i,2) ], 'r-', 'linewidth', 3 );
        im1 = i;
      end
    end
%
%  Plot vertices.
%
    plot ( v(:,1), v(:,2), 'g.', 'markersize', 30 );

    hold ( 'off' );
    xlabel ( '<--- X --->' );
    ylabel ( '<--- Y --->' );
    grid ( 'on' );
    axis ( 'equal' );
    if ( picture == 1 )
      title ( 'polygon vertices' );
      filename = 'polygon_vertices.png';
    elseif ( picture == 2 )
      title ( 'polygon edges' );
      filename = 'polygon_edges.png';
    elseif ( picture == 3 )
      title ( 'polygon triangulation' );
      filename = 'polygon_triangulation.png';
    else
      title ( 'polygon sample points' );
      filename = 'polygon_sample.png';
    end
    print ( '-dpng', filename );
    fprintf ( 1, '  Graphics saved as "%s"\n', filename );

  end
%
%  Terminate.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'polygon_sample_test():\n' );
  fprintf ( 1, '  Normal end of execution.\n' );

  timestamp ( );

  rmpath ( '../polygon_sample' );

  return
end
function timestamp ( )

%*****************************************************************************80
%
%% timestamp() prints the YMDHMS date as a timestamp.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    14 November 2022
%
%  Author:
%
%    John Burkardt
%
  t = now ( );
  c = datevec ( t );
  s = datestr ( c, 0 );
  fprintf ( 1, '%s\n', s );

  return
end


