Z0q Z0q - 1 month ago 19
C Question

How to fill an ellipse shape?

I have a function in C++ which draws an ellipse. Now I want to add a fill option to that ellipse. How do I do that? I tried using the flood fill algorithm, but it does not work correctly when the surface already contains paintings.

Here is my current algorithm:

const double TwoPI = ( PI * 2 );
const double Step = ( PI / 180.0f );

for( double i = 0; i < TwoPI; i += Step )
{
float x_offset = ( radiusX * cos( (float) i ) );
float y_offset = ( radiusY * sin( (float) i ) );

int x = int( a_X + x_offset );
int y = int( a_Y + y_offset );

if( prevX == -1 && prevY == -1 )
Plot( x, y, color );
else
Line( prevX, prevY, x, y, color );
}

Answer

Well, one idea could be to just traverse half the circle, and instead compute points that are symmetrically placed around i.e. the y axis. Then draw a horizontal line between them, filling in all the pixels.