function [ w, p ] = edge_to_boundary ( e )

%*****************************************************************************80
%
%% edge_to_boundary() converts a polyiamond description from edge_to_boundary.
%
%  Discussion:
%
%    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.
%
%    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:
%
%    27 October 2020
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer E(EN,2), the IJ parallelogram coordinates of the nodes that
%    lie on the boundary of the polyiamond.
%
%  Output:
%
%    string W: the boundary word.
%
%    integer P(2): the boundary word starting point.
%
  en = size ( e, 1 );
%
%  From the edge list, form the word.
%
  w = '';

  for k = 1 : en

    if ( k < en )
      kp1 = k + 1;
    else
      kp1 = 1;
    end 

    i = e(k,1);
    j = e(k,2);

    ip1 = e(kp1,1);
    jp1 = e(kp1,2);

        if ( ip1 == i     && jp1 == j + 1 ) 
      w = [ w, '0' ];
    elseif ( ip1 == i + 1 && jp1 == j )
      w = [ w, '1' ];
    elseif ( ip1 == i + 1 && jp1 == j - 1 )
      w = [ w, '2' ];
    elseif ( ip1 == i     && jp1 == j - 1 )
      w = [ w, '3' ];
    elseif ( ip1 == i - 1 && jp1 == j )
      w = [ w, '4' ];
    elseif ( ip1 == i - 1 && jp1 == j + 1 )
      w = [ w, '5' ];
    else
      fprintf ( 1, '\n' );
      fprintf ( 1, 'edge_to_boundary - Fatal error!\n' );
      fprintf ( 1, '  Illegal edge sequence (%d,%d) -> (%d,%d)\n', i, j, ip1, jp1 );
      error ('edge_to_boundary - Fatal error!' );
    end

  end

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

  return
end

