function moment = boundary_word_moment ( s )

%*****************************************************************************80
%
%% boundary_word_moment(): moment of inertia of a polyomino defined by a boundary word.
%
%  Discussion:
%
%    The description of the polyomino is assumed to begin at (0,0).
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    07 May 2020
%
%  Author:
%
%    John Burkardt
%
%  Reference:
%
%    George Bell,
%    The dynamics of spinning polyominoes,
%    Gathering for Gardner, G4G13.
%
%    Srecko Briek, Gilbert Labelle, Ariane Lacasse,
%    Algorithms for polyominoes based on the discrete Green theorem,
%    Discrete Applied Mathematics,
%    Volume 147, pages 187-205, 2005.
%
%  Input:
%
%    char s(*): a string of the characters 'u', 'd', 'l' and 'r' that should
%    represent a path around the boundary of a polyomino.
%
%  Output:
%
%    real xc, yc: the coordinates of the centroid.
%
  check = boundary_word_check ( s );

  if ( ~ check )
    moment = NaN;
    return
  end

  s_len = length ( s );
  s = lower ( s );

  x = 0;
  y = 0;
  moment = 0.0;
  for i = 1 : s_len

    if ( s(i) == 'u' )
      moment = moment + x * y + x / 3.0 + x^3 / 3.0 + x * y^2;
      y = y + 1;
    elseif ( s(i) == 'd' )
      moment = moment + x * y - x / 3.0 - x^3 / 3.0 - x * y^2;
      y = y - 1;
    elseif ( s(i) == 'l' )
      x = x - 1;
    elseif ( s(i) == 'r' )
      x = x + 1;
    end

  end

  area = boundary_word_area ( s );
  [ xc, yc ] = boundary_word_centroid ( s );

  moment = moment - area * ( xc^2 + yc^2 );

  return
end
