function ng = annulus_grid_count ( n, r1, r2, c )

%*****************************************************************************80
%
%% annulus_grid_count() counts the grid points inside an annulus.
%
%  Discussion:
%
%    The grid is defined by specifying the center of the annulus,
%    the inner and outer radius, and the number of subintervals N 
%    into which the horizontal radius should be divided.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    10 November 2022
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    integer N, the number of subintervals.
%
%    real R1, R2, the inner and outer radius of the annulus.
%
%    real C(2), the coordinates of the center of the annulus.
%
%  Output:
%
%    integer NG, the number of grid points inside the annulus.
%
  ng = 0;

  for j = 0 : n

    i = 0;
    x = c(1);
    y = c(2) + r2 * 2 * j / ( 2 * n + 1 );

    if ( r1 * r1 <= ( x - c(1) ).^2 + ( y - c(2) ).^2 )
      if ( r1 == 0.0 && j == 0 )
        ng = ng + 1;
      else
        ng = ng + 2;
      end
    end

    while ( true )

      i = i + 1;
      x = c(1) + r2 * 2 * i / ( 2 * n + 1 );
      if ( r1 * r1 <= ( x - c(1) ).^2 + ( y - c(2) ).^2 )
        if ( r2 * r2 < ( x - c(1) ).^2 + ( y - c(2) ).^2 )
          break
        end
        ng = ng + 1;
        ng = ng + 1;
        if ( 0 < j )
          ng = ng + 1;
          ng = ng + 1;
        end
      end
    end

  end

  return
end
