function [ xc, yc ] = boundary_word_centroid ( s )

%*****************************************************************************80
%
%% boundary_word_centroid(): centroid 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:
%
%    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 )
    xc = NaN;
    yc = NaN;
    return
  end

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

  x = 0;
  y = 0;
  xc = 0.0;
  yc = 0.0;
  for i = 1 : s_len

    if ( s(i) == 'u' )
      y = y + 1;
      xc = xc + x^2 / 2.0;
    elseif ( s(i) == 'd' )
      y = y - 1;
      xc = xc - x^2 / 2.0;
    elseif ( s(i) == 'l' )
      x = x - 1;
      yc = yc + y^2 / 2.0;
    elseif ( s(i) == 'r' )
      x = x + 1;
      yc = yc - y^2 / 2.0;
    end

  end

  area = boundary_word_area ( s );

  xc = xc / area;
  yc = yc / area;

  return
end
