function v = boundary_to_vertex ( w, p )

%*****************************************************************************80
%
%% boundary_to_vertex() converts a polyiamond description from boundary to vertex.
%
%  Discussion:
%
%    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)
%
%    The vertex list of a polyiamond gives the IJ coordinates of the
%    polygonal vertices, in counterclockwise order.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    05 November 2020
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    string W: the boundary word.
%
%    integer P(2): the boundary word starting point.
%
%  Output:
%
%    integer V(VN,2): the IJ parallelogram point coordinates of the 
%    vertices bounding the polyiamond.  
%
  w_n = length ( w );

  w_i = 0;
  v = [];
  i = p(1);
  j = p(2);
  v = [ v; i, j ];
  c = w(1);

  while ( w_i < w_n );

    w_i = w_i + 1;
    c_old = c;
    c = w(w_i);

    if ( c ~= c_old || w_i == w_n )
      v = [ v; i, j ];
    end

    if ( c == '0' )
      j = j + 1;
    elseif ( c == '1' )
      i = i + 1;
    elseif ( c == '2' )
      i = i + 1;
      j = j - 1;
    elseif ( c == '3' )
      j = j - 1;
    elseif ( c == '4' )
      i = i - 1;
    elseif ( c == '5' )
      i = i - 1;
      j = j + 1;
    else
      fprintf ( 1, '\n' );
      fprintf ( 1, 'boundary_to_vertex - Fatal error!\n' );
      fprintf ( 1, '  Illegal boundary word character w(%d)=%s\n', w_i, c );
      error ( 'boundary_to_vertex - Fatal error!' ); 
    end

  end
%
%  Shift so min I and min J are 0.
%
% imin = min ( v(:,1) );
% v(:,1) = v(:,1) - imin;
% jmin = min ( v(:,2) );
% v(:,2) = v(:,2) - jmin; 

  return
end

