/* Listing 2 */

void filled_circle( int x_pos, int y_pos,
                    int radius, int fillcolor);
int colormax;


/************************************************/

void generate_circles()

/* this function draws a series of circles at random */
/* positions, each one after the first touching the  */
/* nearest circle that has been previously drawn     */

{
   int xmax, ymax, x_pos, y_pos, radius;
   randomize();   /* initialize the random numbers */
   xmax = getmaxx(); /* maximum x and y positions  */
   ymax = getmaxy(); /* that can be displayed      */

   rmax = max( xmax, ymax)/2;
          /* divisor of 2 can be changed to allow */
          /* different maximum radii              */

   colormax = getmaxcolor();

   /* select random position and draw first circle */

   x_pos = rand() % xmax;
   y_pos = rand() % ymax;
   radius = rand() % rmax;
   c[0].x = x_pos;
   c[0].y = y_pos;
   c[0].r = radius;
   filled_circle( x_pos, y_pos, radius,
                      rand() % colormax);

   /* select and draw remaining randomly placed circles */
   /* each tangent to the nearest previously drawn      */
   /* circle                                            */

   for ( N = 1; N <= MAX_CIRCLES; N++)
       {
       do
           {
           exit_if_kbhit();
           x_pos = rand() % xmax;
           y_pos = rand() % ymax;
           radius = new_radius( x_pos, y_pos);
           } while (radius <= 0);
       filled_circle( x_pos, y_pos, radius,
                      rand() % colormax + 1);
       c[N].x = x_pos;
       c[N].y = y_pos;
       c[N].r = radius;
       }
}

/************************************************/

void filled_circle( int x_pos, int y_pos,
                    int radius, int fillcolor)

/* draws a circle filled with the specified color */

{
   circle( x_pos, y_pos, radius);
   setfillstyle( SOLID_FILL, fillcolor);
   floodfill( x_pos, y_pos, getcolor());
}
