/* Draw a circle in the image */
void drawColorCircle( colorImage img, int row, int col,
int radius, pixel pix )
{
int r, c;
int boxLeft, boxRight, boxTop, boxBottom;
/* find the bounding box for the circle */
boxLeft = col-radius;
if ( boxLeft < 0 ) boxLeft = 0;
boxTop = row-radius;
if ( boxTop < 0 ) boxTop = 0;
boxRight = col+radius;
if ( boxRight >= img.ncols ) boxRight = img.ncols-1 ;
boxBottom = row+radius;
if ( boxBottom >= img.nrows ) boxBottom = img.nrows-1 ;
/* scan thru the pixels in the bounding box, setting */
/* those inside the circle to the forground color */
for ( r = boxTop; r<boxBottom; r++ )
for ( c = boxLeft; c<boxRight; c++ )
if ( (r-row)*(r-row)+(c-col)*(c-col) <= radius*radius )
setColorPixel( img, r, c, pix );
}
Comments: This function is an easy variation of puzzle I23. The "trick" is to use a bounding box.