function v = word_to_vertex ( w, p )

%*****************************************************************************80
%
%% word_to_vertex() computes a vertex list from a 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 
%    in one of 8 directions, as suggested by this diagram:
%
%      D--C--B
%   ^  |\ | /|
%   |  | \|/ |
%   J  E--P--A
%      | /|\ |
%      |/ | \|
%      F--G--H
%
%         I-->
%
%    If the current position P has coordinates (I,J), then
%    the step of given index moves to
%
%      A  (I+1,J  )
%      B  (I+1,J+1)
%      C  (I,  J+1)
%      D  (I-1,J+1)
%      E  (I-1,J  )
%      F  (I-1,J-1)
%      G  (I,  J-1)
%      H  (I+1,J-1)
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    08 December 2021
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    string W: the boundary word.
%
%    integer P(1,2): the boundary word starting point.
%
%  Output:
%
%    integer V(VN,2): the IJ coordinates of the vertices.  
%
  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 == 'A' )
      i = i + 1;
    elseif ( c == 'B' )
      i = i + 1;
      j = j + 1;
    elseif ( c == 'C' )
      j = j + 1;
    elseif ( c == 'D' )
      i = i - 1;
      j = j + 1;
    elseif ( c == 'E' )
      i = i - 1;
    elseif ( c == 'F' )
      i = i - 1;
      j = j - 1;
    elseif ( c == 'G' )
      j = j - 1;
    elseif ( c == 'H' )
      i = i + 1;
      j = j - 1;
    else
      fprintf ( 1, '\n' );
      fprintf ( 1, 'word_to_vertex - Fatal error!\n' );
      fprintf ( 1, '  Illegal boundary word character w(%d)=%s\n', w_i, c );
      error ( 'word_to_vertex - Fatal error!' ); 
    end

  end

  return
end

