Bresenham Circle
Bresenham Circle
Symmetry:
We can find any point's symmetric complement about these lines by permuting the indices. For example the point (x,y)
has a complementary point (y,x) about the line x=y. And the total set of complements for the point (x,y) are get point's
symmetric complement about these lines by permuting the indices :
{(x,-y), (-x,y), (-x,-y), (y,x), (y,-x), (-y,x),(-y,-x)}
The algorithm loops over one-eighth of the circle. Specifically, it starts at the top of the circle and goes to the right by 45
degrees, where the slope of a radial line is 1.
The equation of circle is x2 + y2 = r2, where r is the radius of the circle. So using this, we can write a simple circle drawing
algorithm by solving the above equation for y at unit x intervals using:
Suppose the first pixel drawn is P(xk,yk). Now to get the next pixel we will increment xk+1 but for y we have two options,
either we can select point N(xk+1,yk) or point S (xk+1,yk-1).
Now the distance between point N and arc of the circle is d1 & between point S and arc of the circle is d2 & which ever
distance is less we will select that point. For that we will derive a decision parameter and on the basis of it we will decide
which distance is less.
f(N) = (xk+1)2 + (yk)2 - r2 value of this function will be +ve, since distance of point N is greater than radius in above fig
f(S) = (xk+1)2 + (yk-1)2 - r2 value of this function will be –ve, since distance of point S is less than radius.
Pk = 2(xk+1)2 + (yk)2 + (yk-1)2 - 2r2 this equation will be used later to derive initial decision parameter
Now calculate Pk+1: simply replace xk with xk+1 and since we still don’t know what to take yk or yk+1 for the next pixel
represent next y as yk+1 and not as (yk+1) only in the above equation of Pk
Pk+1 – Pk = 2((xk)2+4xk+4)2 + (yk+1)2 + (yk+1)2 –2yk+1 +1 – 2r2 – 2((xk)2+2xk +1) – (yk)2 – ((yk)2 –2yk + 1) + 2r2
Pk+1 – Pk = 2(xk)2 + 8xk + 8 + (yk+1)2 + (yk+1)2 – 2yk+1 + 1 – 2r2 – 2(xk)2 – 4xk – 2 – (yk)2 – (yk)2 +2yk – 1 + 2r2
Pk+1 = Pk + 4xk + 6
Else
Select point S(xk+1,yk-1) -> replace yk+1 with yk-1 in above derived equation (x)
Pk = 2 + r2 + r2 – 2r + 1 – 2r2
Pk = 3 + 2r2 – 2r – 2r2
P0 = 3 – 2r
Bresenham Circle Drawing Algorithm:
Example:
Plot a circle for Bresenham algorithm for radius = 8 & center (xc, yc) = (30, 40)
x= 0, y = 8
xc = 30, yc = 40 //center
x y P Remark x+xc y+yc
0 8 p = 3-2r = 3 – 2(8) = 3-16 = -13 p<0, x++ & change p 30 48
1 8 p = p + 4x + 6 = -13 + 4(1) +6 = -3 p<0, x++ & change p 31 48
2 8 p = p + 4x + 6 = -3 + 4(2) +6 = 11 p>0, x++, y-- & change p 32 48
3 7 p = p + 4(x-y) + 10 = 11+4(-4)+10=5 p>0, x++, y-- & change p 33 47
4 6 p = p + 4(x-y) + 10 = 5+4(-2)+10=7 p>0, x++, y-- & change p 34 46
5 5 p = p + 4(x-y) + 10 = 7+4(0)+10=17 Since x has become equal to y so 35 45
stop
Program:
#include <stdio.h>
#include <dos.h>
#include <graphics.h>
// driver function
int main()
{
int xc = 50, yc = 50, r2 = 30;
int gd, gm;
detectgraph(&gd,&gm);
initgraph(&gd, &gm, "c:\\Turboc4\\tc\\bgi"); // initialize graph
circleBres(xc, yc, r); // function call
return 0;
}
Output: