I18 Answer


int main ( int argc, char* argv[] )
{
  image img ;
  int r, c;
  int nrows, ncols, back, fore, radius ;
  int rc, cc; /* coordinates of center */

  if ( argc != 7)
  {
    printf("solidCircle fileName nrows ncols radius back fore\n");
    return EXIT_SUCCESS;
  }

  nrows = atoi( argv[2] );
  ncols = atoi( argv[3] );
  radius= atoi( argv[4] );
  back  = atoi( argv[5] );
  fore  = atoi( argv[6] );

  if ( newImage( &img, nrows, ncols ) == NULL )
  {
    printf(">>error<< can't allocate memory\n");
    system( "pause" );
    return;
  }

  rc = nrows/2;
  cc = ncols/2;

  for ( r = 0; r<img.nrows; r++ )
    for ( c = 0; c<img.ncols; c++ )
      if ( (r-rc)*(r-rc) + (c-cc)*(c-cc) < radius*radius )
        setPixel( img, r, c, fore );
      else
        setPixel( img, r, c, back );

  writePGMimage( img, argv[1]);
}

Comments: The circle is in the center of the image at row = nrows/2 and column = ncols/2. A pixel at row r and column c will be inside the circle (and should be set to the foreground color) if the distance from it to the center is less than the length of the radius. The square of this distance is:

(r-rc)*(r-rc) + (c-cc)*(c-cc)

This square should be less than the radius squared.


back