Sunteți pe pagina 1din 28

Scan Converting a circle

A circle is a symmetrical figure. Any circle generating algorithm can take advantage of the circles symmetry to plot 8 points for each value that the algorithm calculates. 8 way symmetry is used by reflecting each calculated point around each 45 degree axis.

(-2,8) (-y,x) 3 (-8,2) (-x,y) (-8,-2) (-x,-y) 4 5 6

(2,8) (y,x) 2 1 8 7 (8,2) (x,y) (8,-2) (x,-y)

(-2,-8) (-y,-x)

(2,-8) (y,-x)

8-way symmetry of a circle

If one point is calculated with a circle algorithm ,7 more points can be found by reflection. P1 = (x,y) P5 = (-x,-y) P2 = (y,x) P6 = (-y,-x) P3 = (-y,x) P7 = (y,-x) P4 = (-x,y) P8 = (x,-y)

Defining a Circle
There are 2 standard methods of mathematically defining a circle centered at origin. The first method defines a circle with the secondorder polynomial equation y 2 =r 2 -x 2 where x=x coordinate y=y coordinate r=radius of the circle

(x,(r2 - x2 ) ) y r

Circle defined with a second degree polynomial

This is a very inefficient method, however because for each point both x and r must be squared and subtracted from each other; then the square root of the result must be found.

The second method of defining a circle makes use of trigonometric functions x=rcos and y=rsin where
=current angle r=radius of the circle x=x coordinate y=y coordinate

By this method , is stepped from to / 4 and each value of x and y is calculated. However the computation of the values of sin and cos is even more time consuming than the calculations required by the first method.

1. Bresenhams Circle Algorithm


If a circle is to be plotted efficiently ,the use of trigonometric and power functions must be avoided. As with the generation of a straight line, it is also desirable to perform the calculations necessary to find the scan converted points with only integer addition, subtraction & multiplication by power of 2. This algorithm allows these goals to be met.

If the 8 way symmetry of a circle is used to generate a circle, points will only have to be generated through a 45 angle. And if the points are generated from 90 to 45 ,moves will be made only in the + x and y. The best approximation of the true circle will be described by those pixels in the raster that fall the least distance from the true circle.

(xi, yi )

T (xi+1, yi ) S (xi+1, yi-1 )

If the points are generated from 90 and 45 ,each new point closest to the true circle can be found by taking either of two actions: 1. Move in the x direction one unit. 2. Move in the x direction one unit and move in the -ve y direction one unit. A method of selecting between these two choices is all that is necessary to find the points closest to the true circle.

Assume that (xi ,yi) are the coordinates of the last scan converted pixel upon entering step i Let the distance from the origin to pixel T squared minus the distance to the true circle squared =D(T). Let the distance from the origin to pixel S squared minus the distance to the true circle squared =D(S).

As the coordinates of T are (xi+1,yi) and those of S are (xi+1,yi-1),the following expression can be developed : D(T)=(xi+1)2 + yi2-r2 D(S)=(xi+1)2 + (yi -1 )2-r2

This function D provides a relative measurement of the distance from the centre of a pixel to the true circle. Since D(T) will always be positive (T is outside the true circle) and D(S) will always be ve (S is inside the true circle), a decision variable di may be defined as follows: di = D(T) + D(S)

di = 2 (xi+1)2 + yi 2 + (yi -1) 2-2r2 when di < 0 ,we have |D(T)|< |D(S)| and pixel T is chosen . when di >= 0 ,we have |D(T)|>=|D(S)| and pixel S is chosen . We can also write the decision variable di+1 for the next step: di +1 = (2xi+1+1)2 + yi+1 2 + (yi+1 -1) 2-2r2

hence di +1 - di =2(xi+1+1)2 + yi+1 2 + (yi+1 -1) 2- yi 2 - (yi -1) 2 Since xi+1 = xi + 1,we have di +1 = di + 4xi + 2 (yi+12 yi 2 ) -2(yi+1 yi ) + 6

if T is chosen pixel then yi+1 = yi and so di +1 = di + 4xi + 6 if S is chosen pixel then yi+1 = yi -1 and so di +1 = di + 4(xi - yi )+ 10

finally , we set (0,r) be the staring pixel x coordinates and compute the base case value d1 for this recursive formula from the original definition of di :
d1=2(0+1) 2 + r2 + (r-1) 2 -2r 2 = 3-2r

int x=0,y=r,d=3-2r while(x<=y){ setpixel(x,y); if(d<0) d=d+4x+6; else{ d=d+4(x-y) +10; y--; } x++; }

Mid point circle algorithm


This algorithm is also very similar to Bresenhams approach. It is based on relationship between an arbitrary point (x,y) and a circle of radius r centered at the origin: f(x,y) = x2 + y2 - r2 < 0 for (x,y) inside the circle =0 for (x,y) on the circle > 0 for (x,y) outside the circle

Consider the coordinates of the point halfway between pixel T and pixel S (xi + 1, yi - ). This is called midpoint and we use it to define a decision parameter : Pi = f(xi + 1, yi - ) = (xi + 1)2 + (yi - )2 - r2

If pi is ve then the midpoint is inside the circle and we choose pixel T. If pi is +ve(or =0) then the midpoint is outside the circle and we choose pixel S. Similarly the decision parameter for the next step is : Pi+1 = (xi+1 + 1)2 + (yi+1 - )2 - r2

since we have xi+1 = xi +1 ,we have pi+1- pi =[(xi +1) +1]2 - (xi +1)2 + (yi+1- )2 (yi -- ) 2

hence pi+1 = pi + 2(xi +1) +1+(y2i+1-yi2 ) (yi+1 yi )

if pixel T is chosen (pi < 0).we have yi+1 = yi if pixel S is chosen (pi >= 0).we have yi+1 = yi -1. Thus pi+1 = pi +2(xi +1) + 1 if pi < 0 pi+1 = pi +2(xi +1) + 1 -2(yi - 1) if pi >= 0

we can continue to simplify this is terms of (xi , yi) and get pi+1 = pi +2xi + 3 if pi < 0 pi+1 = pi +2(xi yi ) + 5 if pi >= 0 or we can write in terms of (xi+1 , yi+1 ) and we have pi+1 = pi +2xi+1 + 3 if pi < 0 pi+1 = pi +2(xi+1 yi+1) + 5 if pi >= 0

Finally we compute the initial value for the decision parameter using the original definition of pi and (0,r): pi =(0+1) 2 + (r- 1/2)2 r2 = 5/4 - r

Algorithm
int x=0,y=r,p=1-r while(x<=y){ setpixel(x,y); if(p<0) p=p+2x+3; else{ p=p+2(x-y) +5; y--; } x++; }

S-ar putea să vă placă și