How to draw a filled circle?

I am creating bitmap / bmp files according to the specifications with my C code, and I would like to draw simple primitives on my bitmap. The following code shows how I draw a rectangle in my bitmap:

if(curline->type == 1) // draw a rectangle { int xstart = curline->x; int ystart = curline->y; int width = curline->width + xstart; int height = curline->height + ystart; int x = 0; int y = 0; for(y = ystart; y < height; y++) { for(x = xstart; x < width; x++) { arr[x][y].blue = curline->blue; arr[x][y].green = curline->green; arr[x][y].red = curline->red; } } printf("rect drawn.\n"); } ... save_bitmap(); 

Output Example: enter image description here

So basically I set the red, green, and blue values ​​for all the pixels in the given x and y.

Now I would like to fill the circle, knowing its middle and radius. But how do you know which pixels are inside this circle and which are not? Any help would be appreciated, thanks for reading.

+4
source share
2 answers

A point lies within the circle if the distance from the point to the center of the circle is less than the radius of the circle.

Consider the point (x1, y1) in comparison with a circle with center (x2, y2) and radius r:

 int dx = x2 - x1; // horizontal offset int dy = y2 - y1; // vertical offset if ( (dx*dx + dy*dy) <= (r*r) ) { // set pixel color } 
+9
source

You can also try the midpoint algorithm, here on Wikipedia.

+3
source

All Articles