Tuesday, March 1, 2011

CG-Bresenham’s circle drawing algorithm.

Bresenham's circle algorithm calculates the locations of the pixels in the first 45 degrees. It assumes that the circle is centered on the origin. So for every pixel (x,y) it calculates we draw a pixel in each of the 8 octants of the circle :

PutPixel(Center X + X, Center Y + Y)
PutPixel(Center X + X, Center Y - Y)
PutPixel(Center X - X, Center Y + Y)
PutPixel(Center X - X, Center Y - Y)
PutPixel(Center X + Y, Center Y + X)
PutPixel(Center X + Y, Center Y - X)
PutPixel(Center X - Y, Center Y + X)
PutPixel(Center X - Y, Center Y - X)

So let's get into the actual algorithm. Given a radius for the circle we perform this initialisation:

d := 3 - (2 * RADIUS)
x := 0
y := RADIUS
 Now for each pixel we do the following operations:

Draw the 8 circle pixels
if d < 0 then
    d := d + (4 * x) + 6
else
  begin
    d := d + 4 * (x - y) + 10
    y := y - 1;
  end;
 And we keep doing this until x = y. Note that the values added to the decision variable in this algorithm (x and y) are constantly changing, so we cannot precalculate them. The muliplications however are by 4, and we can accomplish this by shifting left twice.

No comments:

Post a Comment