function [ w, p ] = vertex_to_boundary ( v )

%*****************************************************************************80
%
%% vertex_to_boundary() converts a polyiamond description from vertex to boundary.
%
%  Discussion:
%
%    The vertex list of a polyiamond gives the IJ coordinates of the
%    polygonal vertices, in counterclockwise order.
%
%    The boundary word describes how to trace the boundary by starting
%    at a base point (i,j), and taking a sequence of steps of unit length 
%    in one of 6 directions, as suggested by this diagram:
%
%          5-----0-----*
%      ^  / \   / \   /
%     /  /   \ /   \ /
%    J  4-----P-----1
%      / \   / \   /
%     /   \ /   \ /
%    *-----3-----2
%
%          I-->
%
%    If the current position P has parallelogram coordinates (I,J), then
%    the step of given index moves to
%
%      0  (I,  J+1)
%      1  (I+1,J  )
%      2  (I+1,J-1)
%      3  (I,  J-1)
%      4  (I-1,J  )
%      5  (I-1,J+1)
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    21 October 2020
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer V(VN,2), the IJ parallelogram point coordinates of the 
%    vertices bounding the polyiamond. 
%
%  Output:
%
%    string W: the boundary word.
%
%    integer P(2): the boundary word starting point.
%
  w = '';
  vn = size ( v, 1 );

  i = v(1,1);
  j = v(1,2);

  for k = 1 : vn

    kold = k;
    iold = i;
    jold = j;

    k = 1 + mod ( k, vn );
    i = v(k,1);
    j = v(k,2);

    while ( i == iold && jold < j )
      w = [ w, '0' ];
      jold = jold + 1;
    end

    while ( iold < i && jold == j )
      w = [ w, '1' ];
      iold = iold + 1;
    end

    while ( iold < i && jold > j )
      w = [ w, '2' ];
      iold = iold + 1;
      jold = jold - 1;
    end

    while ( iold == i && jold > j )
      w = [ w, '3' ];
      jold = jold - 1;
    end

    while ( iold > i && jold == j )
      w = [ w, '4' ];
      iold = iold - 1;
    end

    while ( iold > i && jold < j )
      w = [ w, '5' ];
      iold = iold - 1;
      jold = jold + 1;
    end

    if ( iold ~= i || jold ~= j )
      fprintf ( 1, '\n' );
      fprintf ( 1, 'vertex_to_boundary - Fatal error!\n' );
      fprintf ( 1, '  Illegal vertex sequence!\n' );
      fprintf ( 1, '  V(%d,1:2) = (%d,%d)\n', kold, v(kold,1:2) );
      fprintf ( 1, '  V(%d,1:2) = (%d,%d)\n', k,   v(k,1:2) );
      error ( 'vertex_to_boundary - Fatal error!' ); 
    end

  end

  p(1) = v(1,1);
  p(2) = v(1,2);

  return
end

