"Bresenham's" circle algorithm filling question
I've implemented "Bresenham's" (midpoint) algorithm to plot circles, and everything went well (C++ and OpenGL).
I am wondering now if it is possible to use the same algorithm to fill the circles?
Take the following few circles 开发者_高级运维for example: http://imgur.com/S0Qy6 which are plotted with the following algorithm:
void circle(Point p, int r) {
int x = 0;
int y = r;
int f = 1-r;
// plot vert/horiz points indepedently
while (x<y) {
x++;
if (f<0) {
f += 2*x+1;
} else {
y--;
f += 2*(x-y+1);
}
glRecti(p.x+x, p.y+y, p.x+x+1, p.y+y+1);
// plot other points using 8 way symmetry
// attempt to fill the circle - didn't go well
plotLine(Point(p.x, p.y+x), Point(p.x+x, p.y+x));
}
}
I think the best place to put the filling bit would be in my main loop, as I want to essentially fill each quadrant again via 8-way symmetry. So I added my plotLine call, but what I ended up with was a strangely filled circle instead: http://imgur.com/jQW7B
If I then color the circles differently, I end up with horizontal banded stripes rather than actual filled circles - my approach is obviously wrong, but I'm not sure how to fix it.
I'm not certain, but from squinting at your algorithm, I was expecting the last line to look more like
plotLine(Point(p.x+x, p.y+x), Point(p.x+x, p.y+y));
(that is, start on a northeast 45 degree line from p.x, then go up to the point on the circle that you just drew).
As I recall the usual thing it to fill using either horizontal or vertical lines.
That is for each iteration you plot from Quadrant I to Quadrant II and from Quadrant III to Quadrant IV (where I chose horizontal lines). Notice that you get a total of four lines out of each iteration.
( p.x+x, p.y+y ) to ( p.x-x, p.y+y )
( p.x+y, p.y+x ) to ( p.x-y, p.y+x )
( p.x+x, p.y-y ) to ( p.x-x, p.y-y )
( p.x+y, p.y-x ) to ( p.x-y, p.y-x )
All of these lines are horizontal, and the start and end on all eight points. When your done the entire circle will be filled with no unnecessary redrawing (except possibly a double drawing of the last two lines when).
The choice of horizontal or vertical may be completely arbitrary, but if you underlying graphics system draws lines faster in one direction than the other it could make a difference.
精彩评论