Sei sulla pagina 1di 3

The Midpoint circle drawing algorithm works on the same midpoint concept as the Bresenhams line algorithm.

In the Midpoint circle drawing algorithm, you determine the next pixel to be plotted based on the position of the midpoint between the current and next consecutive pixel. The following shows the current pixel (xn, yn) and the possible next pixels, (xn+1, yn) and (xn+1, yn-1) to be determined using the Midpoint circle drawing algorithm:

You use the Midpoint circle drawing algorithm to determine the next pixel to be plotted depending on the position of the midpoint D. If D is inside the circle, choose (xn+1, yn) as the next pixel. If D is outside the circle, choose (xn+1, yn-1) as the next pixel. If D lies on the circumference of the circle, choose either pixel. The equation of the circle is given by g(x, y) as: g(x, y) = x2 + y2 - R2 = 0 where R is the radius of the circle. g(x, y) is positive for a point outside the circle, negative for a point inside the circle, and zero for a point on the circumference of the circle. These relations help confirm the position of the midpoint D to determine the next pixel to be plotted on the circle. To determine the next point using the Midpoint algorithm: 1. Enter the center of the circle, (xc, yc), and its radius R. 2. Plot the pixel (xc, R) as the initial pixel. 3. To determine the next pixel, check the sign of g(D). You can determine this from the value of g(xn+1, yn-1/2) denoted by pn: pn = g(xn+1, yn-1/2) = (xn+1)2 + (yn- )2 - R2. 4. Choose the next pixel depending on the value of pn. If pn < 0, choose pixel (xn+1, yn), if pn > 0, choose pixel (xn+1, yn-1), and if pn = 0, choose either pixel. 5. If you chose pixel (xn+1, yn), determine the next pixel from the sign of (xn+2, yn- ) denoted by pn+1: pn+1 = g(xn+2, yn-1/2) = (xn+2)2 + (yn-1/2)2 - R2

=> The increment in p is p1 = pn+1 - pn = 2xn + 3. If you choose pixel (xn+1, yn-1), determine the next pixel from the sign of (xn+2, yn- 3/2 ) denoted by pn+1: pn+1 = g(xn+2, yn-3/2) = (xn+2)2 + (yn-3/2)2 - R2 => The increment in p is p2 = pn+1 - pn = 2xn - 2yn + 5. 6. Calculate the initial condition, p0. The first point on the circle is (0, R) and the first midpoint D is at (1, R - ). Calculate p0 as: => p0 = g(1, R - ) = 12 + (R - )2 - R2 = 5/4 - R. If all the attributes of the circle, such as the center coordinates and the radius, are given as integers, you can round p0 to p0 = 1 - R. 7. To draw the circle, start with the pixel (0, R) and initialize p to 0. After plotting the pixel (0, R), increment the value of p by p0. For subsequent pixels, determine the next pixel depending upon the value of p. If p < 0, choose (xn+1, yn) as the next pixel and increment p by p1. If p > 0, choose pixel (xn+1, yn+1) and increment p by p2. If p = 0, choose either. Plot the pixels on the circumference of the circle up to x = R/2 or y < x. For each pixel calculated, use the symmetry() function to obtain all the symmetric pixels. Following Example shows how to draw a circle using the Midpoint circle drawing algorithm: #include<stdio.h> #include<conio.h> #include<graphics.h> void draw_circle(int,int,int); void symmetry(int,int,int,int); main() { int xc,yc,R; int driver,mode; clrscr(); printf("Enter the center of the circle:\n"); printf("Xc ="); scanf("%d",&xc); printf("Yc ="); scanf("%d",&yc); printf("Enter the radius of the circle :"); scanf("%d",&R); clrscr();

driver = DETECT; initgraph(&driver,&mode,"\\tc\\bgi"); //the path may be different in your case. draw_circle(xc,yc,R); getch(); closegraph();

void draw_circle(int xc,int yc,int rad) { int x = 0; int y = rad; int p = 1-rad; symmetry(x,y,xc,yc); for(x= 0;y>x;x++) { if(p<0) p += 2*x + 3; else { p += 2*(x-y) + 5; y--; } symmetry(x,y,xc,yc); } } void symmetry(int x,int y,int xc,int { putpixel(xc+x,yc-y,EGA_WHITE);//For putpixel(xc+y,yc-x,EGA_WHITE);//For putpixel(xc+y,yc+x,EGA_WHITE);//For putpixel(xc+x,yc+y,EGA_WHITE);//For putpixel(xc-x,yc+y,EGA_WHITE);//For putpixel(xc-y,yc+x,EGA_WHITE);//For putpixel(xc-y,yc-x,EGA_WHITE);//For putpixel(xc-x,yc-y,EGA_WHITE);//For } yc) pixel pixel pixel pixel pixel pixel pixel pixel (x,y) (y,x) (y,-x) (x,-y) (-x,-y) (-y,-x) (-y,x) (-x,y)

Potrebbero piacerti anche