function [ c_try, c_park, density_obs ] = circle_circles_packing ( C, R, r )

%*****************************************************************************80
%
%% circle_circles_packing() randomly fills a circle with smaller circles.
%
%  Licensing:
%
%    This code is distributed under the MIT license.
%
%  Modified:
%
%    11 August 2024
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    real C, R: the center and radius of the enclosing circle.
%
%    real r: the radius of the inserted circles.
%
%  Output:
%
%    integer c_try: the number of times a random circle was generated.
%
%    integer c_park: the number of random circles successfully placed.
%
%    real density_obs: the final density, between 0 and 1.
%
  x_list = [];
  y_list = [];
  c_park = 0;
  c_try = 0;
%
%  If we don't park a circle within this many tries, give up.
%
  guard = 5000;

  while ( 0 < guard )

    c_try = c_try + 1;
%
%  Pick a random possible circle within the big circle.
%  Against my better judgment, it seems we will circles even if some of
%  their area extends outside the big circle.  We only insist that
%  the circle center is inside the big circle!
%
    tr = 2.0 * pi * rand ( );
    cr = R * sqrt ( rand ( ) );
    cx = cr * cos ( tr );
    cy = cr * sin ( tr );
%
%  Compute the minimum distance to the circles.
%
    if ( isempty ( x_list ) )
      c_min = 2.0 * r;
    else
      c_min = min ( sqrt ( ( x_list - cx ).^2 + ( y_list - cy ).^2 ) );
    end
%
%  If the chosen spot is large enough, park!
%
    if ( 2.0 * r <= c_min )
      guard = 5000;
      c_park = c_park + 1;
      x_list = [ x_list, cx ];
      y_list = [ y_list, cy ];
    else
      guard = guard - 1;
    end

  end
%
%  Compute maximum and observed parked-car densities.
%
  density_obs = ( c_park * pi * r * r ) / ( pi * R * R );

  return
end
