function [ imin, imax, jmin, jmax ] = boundary_range ( w, p )

%*****************************************************************************80
%
%% boundary_range() determines the range of a polyiamond from its boundary word.
%
%  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 polyiamond is drawn by starting at position (0,0) and then following
%    the instructions of the boundary word.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    25 November 2021
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    string W: the boundary word.
%
%    integer P(1,2): the boundary word starting point.
%
%  Output:
%
%    integer IMIN, IMAX, JMIN, JMAX: the I and J ranges of the polyiamond.
%
  imin = p(1,1);
  imax = p(1,1);
  jmin = p(1,2);
  jmax = p(1,2);

  i = p(1,1);
  j = p(1,2);
  
  wn = length ( w );

  for k = 1 : wn

        if ( w(k) == '0' )
      j = j + 1;
    elseif ( w(k) == '1' )
      i = i + 1;
    elseif ( w(k) == '2' )
      i = i + 1;
      j = j - 1;
    elseif ( w(k) == '3' )
      j = j - 1;
    elseif ( w(k) == '4' )
      i = i - 1;
    elseif ( w(k) == '5' )
      i = i - 1;
      j = j + 1;
    end
 
    imin = min ( imin, i );
    imax = max ( imax, i );
    jmin = min ( jmin, j );
    jmax = max ( jmax, j );
    
  end

  return
end

