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 (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)
%
%    Rotation replaces each character K in the boundary word by K+R.
%
%  Example:
%
%    w  = '0053422134444';
%    k  = 2
%    w2 = '2215044350000';
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    07 January 2022
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    string W: the boundary word.  
%
%    integer P(1,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(1,2): the starting point for the reflected shape.
%
  c = [ '0', '1', '2', '3', '4', '5' ];

  w2 = w;
  for i = 1 : 6
    j = find ( w == c(i) );
    v(j) = mod ( i - 1 + r, 6 );
    w2(j) = c ( v(j) + 1 );
  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

