You are on page 1of 4

Experiment no.

4
Aim: To implement Mid Point circle algorithm. Introduction: In computer graphics, the midpoint circle algorithm is an algorithm used to determine the points needed for drawing a circle. The algorithm is a variant of Bresenham's line algorithm, and is thus sometimes known as Bresenham's circle algorithm, although not actually invented by Bresenham. The algorithm can be generalized to conic sections.

Xk Xk+1 Yk+1

The algorithm starts accordingly with the circle equation x2 + y2 = r2. So, the center of the circle is located at (0,0). We consider first only the first octant and draw a curve which starts at point (r,0) and proceeds upwards and to the left, reaching the angle of 45. The "fast" direction here is the y direction. The algorithm always does a step in the positive y direction (upwards), and every now and then also has to do a step in the "slow" direction, the negative x direction. The frequent computations of squares in the circle equation, trigonometric expressions or square roots can again be avoided by dissolving everything into single steps and recursive computation of the quadratic terms from the preceding ones. Additionally we need to add the midpoint coordinates when setting a pixel. These frequent integer additions do not limit the performance much, as we can spare those square (root) computations in the inner loop in turn. Again the zero in the transformed circle equation is replaced by the error term.

Symmetry of circle

ALGORITHM: 1. 2. 3. 4. 5. Enter the radius & center coordinates of circle Plot center points (xc, yc, 15) Initialize x = 0 & y = r Calculate first decision parameter i.e. p = 1 r Repeat this steps until x is greater than or equal to y a. If p is greater than 0 i. Increment x by 1 ii. p = p + (2*x) +1 b. else i. increment x by 1 ii. decrement y by 1 iii. p = p + (2*(x-y)) + 1 6. With the help of symmetry of circle calculate the remaining pixel of octants Conclusion: By using symmetric of circle we have calculated remaining pixels of octants.

Program: #include<graphics.h> #include<stdio.h> #include<conio.h> #include<iostream.h> void display(int xc,int yc,int x,int y) { putpixel(xc+x,yc+y,15); putpixel(xc+y,yc+x,15); putpixel(xc+y,yc-x,15); putpixel(xc+x,yc-y,15); putpixel(xc-x,yc-y,15); putpixel(xc-y,yc-x,15); putpixel(xc-y,yc+x,15); putpixel(xc-x,yc+y,15); } void main() { clrscr(); int gd=DETECT,gm; initgraph(&gd,&gm,"..//bgi"); int r; cout<<"enter radious of circle"; cin>>r; int xc,yc; cout<<"enter center points"; cin>>xc; cin>>yc; int x=0; int y=r; putpixel(xc,yc,15); int p=1-r; while(x<=y) { if(p<0) { x++; p=p+2*(x+1); } else { x++; y--; p=p+2*(x-y)+1; } display(xc,yc,x,y); } getch(); }

Output: Enter the radius:50 Enter the center co-ordinate:100 100

You might also like