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

%*****************************************************************************80
%
%% boundary_rotate() rotates by k*60 degrees a polyiamond using its 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)
%
%  Example:
%
%    w  = '66543445432121';
%    r  = 2
%    w2 = '22165661654343';
%
%  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 boundary word starting point.
%
%    integer R: the number of 60 degree turns clockwise (R positive) or
%    counterclockwise (R negative)
%
%  Output:
%
%    string W2: the boundary word for the reflected shape.  
%
%    integer P2(2): the starting point for the reflected shape.
%
  c = [ '1', '2', '3', '4', '5', '6' ];

  w2 = w;
  for i = 1 : 6
    j = find ( w == c(i) );
    v(j) = mod ( i - 1 + r, 6 ) + 1;
    w2(j) = c ( v(j) );
  end
%
%  Rotate the base point.
%
  p2 = [ p(1); ...
         p(2) ];
%
%  Clockwise
%
  if ( 0 < r )

    A = [ 1, 1; ...
         -1, 0 ];

    for k = 1 : r
      p2 = A * p2;
    end
%
%  Counterclockwise
%
  elseif ( r < 0 )

    A = [ 0, -1; ...
          1,  1 ];

    for k = 1 : - r
      p2 = A * p2;
    end

  end

  p2 = p2';

  return
end

