function [ w2, p2 ] = boundary_reflect ( w, p, r )

%*****************************************************************************80
%
%% boundary_reflect() reflects a polyhex described by a boundary word.
%
%  Discussion:
%
%    The boundary word describes how to trace the boundary by starting
%    at a base point P=(i,j), and taking a sequence of steps of unit length 
%    in one of 6 directions, as suggested by these diagrams:
%
%          3     2-----*             3     2     *  
%      ^        /                ^    \         /   
%     /        /                /      \       /     
%    J  4-----P     1          J  4     P-----1      
%      /       \                       /             
%     /         \                     /              
%    *     5     6             *-----5     6         
%
%          I-->                      I-->           
%
%    If the current position P has parallelogram coordinates (I,J), then
%    the step of given index moves to
%
%      1  (I+1,J)
%      2  (I,  J+1)
%      3  (I-1,J+1)
%      4  (I-1,J)
%      5  (I,  J-1)
%      6  (I+1,J-1)
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    30 June 2021
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    string W: the boundary word.  
%
%    integer P(2): the starting point.
%
%    integer R: the reflection option.
%    0: no reflection;
%    1: reflect about the 0 degree axis;
%    2: reflect about the 60 degree axis;
%    3: reflect about the 120 degree axis.
%
%  Output:
%
%    string W2: the boundary word for the reflected shape.  
%
%    integer P2(2): the starting point for the reflected shape.
%
  c1 = [ '1', '2', '3', '4', '5', '6' ];

  if ( r == 1 )
    c2 = [ '1', '6', '5', '4', '3', '2' ];
  elseif ( r == 2 )
    c2 = [ '3', '2', '1', '6', '5', '4' ];
  elseif ( r == 3 )
    c2 = [ '5', '4', '3', '2', '1', '6' ];
  else
    w2 = w;
    p2 = p;
    return
  end
%
%  Reflect the boundary word.
%
  w2 = w;
  for i = 1 : 6
    j = find ( w == c1(i) );
    w2(j) = c2(i);
  end
%
%  Reflect the base point.
%
  p2 = p;

  if ( r == 1 )
    p2(1) = - p2(1) - p2(2);
  elseif ( r == 2 )
    p2(1) =   p2(1) + p2(2);
    p2(2) = - p2(2);
  elseif ( r == 3 )
    p2(1) = - p2(1);
    p2(2) = - p2(2);
  end

  return
end

