/* Draw a circle in the image */ void drawCircle( image img, int row, int col, int radius, unsigned char value ) { 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 ) setPixel( img, r, c, value ); }
Comments: The idea of a bounding box might not have occured to you. It is a common way to speed up such drawing operations.