# Filling up a circle

This is a discussion on Filling up a circle within the C Programming forums, part of the General Programming Boards category; Hi, I am working on a program, for which I wrote a function to draw a circle based on a ...

1. ## Filling up a circle

Hi, I am working on a program, for which I wrote a function to draw a circle based on a given color, x and y coordinate and a radius, this function however doesn't have the possibility to be filled with this given color, no matter what I come up with, nothing really seems to do the trick, here is what I have so far:

Code:
```void PlotCircle(Color color, int xcenter,int ycenter,int radius)
{
int x=0;
int p=3 - 2 * radius;

while(x <= y)
{
putPixelScreen(color,xcenter+x,ycenter+y);
putPixelScreen(color,xcenter-x,ycenter+y);
putPixelScreen(color,xcenter+x,ycenter-y);
putPixelScreen(color,xcenter-x,ycenter-y);
putPixelScreen(color,xcenter+y,ycenter+x);
putPixelScreen(color,xcenter-y,ycenter+x);
putPixelScreen(color,xcenter+y,ycenter-x);
putPixelScreen(color,xcenter-y,ycenter-x);

if(p < 0)
p += 4 * x++ + 6;
else
p += 4 * (x++ - y--) + 10;
}
}```
the putPixelScreen function draws one pixel from a given color and x/y coordinates to the screen

any ideas on how I can fill up the circle?

2. The scanline algorithm is popular
http://www.cse.ohio-state.edu/~gurar...93s04su77.html

The flood fill is easy, but very aggressive on stack usage if you're not very careful.
http://www.student.kuleuven.ac.be/~m...floodfill.html

3. Instead of drawing 8 pixels, draw 4 lines.

4. Instead of drawing 8 pixels, draw 4 lines.
That wouldn't work, would it? Which pixel operations would you combine?
Code:
```        putPixelScreen(color,xcenter+x,ycenter+y);
putPixelScreen(color,xcenter-x,ycenter+y);
putPixelScreen(color,xcenter+x,ycenter-y);
putPixelScreen(color,xcenter-x,ycenter-y);
putPixelScreen(color,xcenter+y,ycenter+x);
putPixelScreen(color,xcenter-y,ycenter+x);
putPixelScreen(color,xcenter+y,ycenter-x);
putPixelScreen(color,xcenter-y,ycenter-x);```
They look pretty different to me.

And besides, plotting lines is slower than pixels, and the line function would probably call the pixel function anyway. I think it would be best to use 8 pixels.

Look at the line-plotting code here: http://www.brackeen.com/home/vga/sou.../circle.c.html
[/edit]

5. Originally Posted by dwks
That wouldn't work, would it?
It would.

Originally Posted by ibid.
Which pixel operations would you combine?
Either the ones with identical effective ordinates, or the ones with identical effective abscissas. Except that horizontal lines are more optimizable than vertical ones. Lines restricted to one dimension are optimizable. Especially horizontal ones which just degenerate into memcpy()'s, and can be further optimized to use SIMD or block-move instructions.

Originally Posted by ibid.
And besides, plotting lines is slower than pixels, and the line function would probably call the pixel function anyway.
Still faster then either of Salem's solutions.

Originally Posted by ibid.
I think it would be best to use 8 pixels.
Except that the OP wants to fill the circle.