You would have to fill in each line individually as you are doing, but note that you have to adjust the bounds of x
for the radius. This is somewhat similar to doing a 2D (discrete) integration. The basic idea is that x^2 + y^2 = r^2
at the outer bounds, and both y
and r
are fixed, so...:
for(int y = -r; y < r; y++)
{
int bound = (int)(sqrt(r * r - y * y) + 0.5);
for(int x = -bound; x < bound; x++)
{
raster.setPixel(x,y,color);
}
}
... + 0.5
is a safe way to round to the nearest integer (instead of just taking the floor
with casting) since bound
will always be positive.