function e = boundary_to_edge ( w, p )

%*****************************************************************************80
%
%% boundary_to_edge() converts a polyiamond description from boundary to edge.
%
%  Discussion:
%
%    The boundary word describes how to trace the boundary by starting
%    at a base point P with coordinates (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 edge list contains the sequence of parallelogram IJ coordinates
%    of the nodes that lie on the boundary of the polyiamond, listed
%    in counterclockwise order.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    24 October 2020
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    string W: the boundary word.
%
%    integer P(1,2): the starting point.
%
%  Output:
%
%    integer E(EN,2): the IJ parallelogram point coordinates of the 
%    sequence of nodes that bound the shape.  
%
  wn = length ( w );
  e = zeros ( wn + 1, 2 );

  i = p(1);
  j = p(2);
  e(1,1:2) = [ i, j ];
  c = w(1);

  for wi = 1 : wn

    c_old = c;
    c = w(wi);

    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_edge - Fatal error!\n' );
      fprintf ( 1, '  Illegal boundary word character w(%d)=%s\n', w_i, c );
      error ( 'boundary_to_edge - Fatal error!' ); 
    end

    e(wi+1,1:2) = [ i, j];

  end  

  return
end

