function boundary_to_poly ( n, b, filename )

%*****************************************************************************80
%
%% boundary_to_poly() writes boundary information to a Triangle POLY file.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    01 August 2015
%
%  Author:
%
%    John Burkardt
%
%  Reference:
%
%    Alessandra Agostini, Chiara Piazzola,
%    Mesh Generation with FreeFem++ and Triangle,
%    Master's degree presentation,
%    University of Verona, 2014.
%
%  Input:
%
%    integer N, the number of boundary points.
%
%    integer B(*,2), a list of pixels that form the first black and
%    white boundary detected in the image.
%
%    string FILENAME, the name of the POLY file to be created.
%

%
%  Open the file.
%
  output = fopen ( filename, 'wt' );
%
%  Header.
%
  fprintf ( output, '# %s\n', filename );
  fprintf ( output, '# poly file suitable for input to triangle.\n' );
  fprintf ( output, '# Pixels on a closed boundary.\n' );
%
%  Points.
%
  fprintf ( output, '#\n' );
  fprintf ( output, '# Points:\n' );
  fprintf ( output, '#\n' );
  fprintf ( output, '%i  2  0  1\n', n );
  for i = 1 : n
    fprintf ( output, '%i  %i  %i  2\n', i, b(i,1), b(i,2) );
  end
%
%  Edges are pairs of consecutive points.
%
  fprintf ( output, '#\n' );
  fprintf ( output, '# Edges:\n' );
  fprintf ( output, '#\n' );
  fprintf ( output, '%i  1\n', n );
  for i = 1 : n
    ii = mod ( i, n ) + 1;
    fprintf ( output, '  %i  %i  %i\n', i, i, ii );
  end
%
%  Holes.
%
  fprintf ( output, '#\n' );
  fprintf ( output, '# Holes:\n' );
  fprintf ( output, '#\n' );
  fprintf ( output, '0\n' );
%
%  Close the file.
%
  fclose ( output );

  fprintf ( 1, '\n' );
  fprintf ( 1, '  Graphics data written to "%s"\n', filename );

  return
end 
