As in the Bresenham line drawing algorithm we derive a decision parameter that helps us to determine whether or not to increment in the y coordinate against increment of x coordinate or vice versa for slope > 1.



Similarly here we will try to derive decision parameter which can give us closest pixel position.

Let us consider only the first octant of a circle of radius r centered on the origin. We begin by plotting point (r, 0) and end when x < y.



The decision at each step is whether to choose the

pixel directly above the current pixel or the pixel; which is above and to the left (8-way stepping).



Assume:

Pi = (xi, yi) is the current pixel.

Ti = (xi, yi +1) is the pixel directly above

Si = (xi -1, yi +1) is the pixel above and to the left.



To apply the midpoint method, we define a circle function:



fcircle(x, y) = x2 + y2 – r2
This Program draw the Whole Circle using Mid Point Algorithm.


#include<graphics.h>

#include<stdlib.h>

#include<stdio.h>

#include<conio.h>



/* **************************************** */

/* A Prgraom for drawing a complete circle */

/* Using: mid point algorithm */

/* Written by: Ritesh Kumar Jain */

/* **************************************** */



// Function prototype

void circlePlotPoints(int, int, int, int);



void initialize_graph(void)

{

/* select a driver and mode that suppors */

/* set also the background color */

/* for example: int gdriver=EGA, gmode= EGAHI */



int gdriver = DETECT;

int gmode;



initgraph(&gdriver, &gmode, "\\tc");

cleardevice();

setbkcolor(BLUE);

}



void setpixel(int w, int q)

{

/* This function draws a pixel */

/* recorrelate to start from the lower left corner */

putpixel(w, 480-q, RED);

}



void circleMidPoint(void)

{

int xCenter, yCenter, radius;

int x, y, p;



/* Accept inputs from user */

printf("\n *** Welcome to my Circle ***\n\n");

printf("Enter the center point \n");

printf("X = "); scanf("%d",&xCenter);

printf("Y = "); scanf("%d",&yCenter);

printf("The Radius = "); scanf("%d",&radius);



/* intialization */

x = 0;

y = radius;

p = 1 - radius;



/* plot the first set of points */

circlePlotPoints(xCenter, yCenter, x, y);



while(x < y)

{

x++;

if(p<0)

p += 2 * x + 1;

else

{

y--;

p += 2 * (x - y) + 1;

}



circlePlotPoints(xCenter, yCenter, x, y);

}

/* End of circleMidPoint function */



}

void circlePlotPoints(int xCenter, int yCenter, int x, int y)

{

setpixel( xCenter + x, yCenter + y);

setpixel( xCenter - x, yCenter + y);

setpixel( xCenter + x, yCenter - y);

setpixel( xCenter - x, yCenter - y);

setpixel( xCenter + y, yCenter + x);

setpixel( xCenter - y, yCenter + x);

setpixel( xCenter + y, yCenter - x);

setpixel( xCenter - y, yCenter - x);

}



void close_graph(void)

{

/* Clean up */

closegraph();

}



/* Start of main program */

void main(void)

{

initialize_graph();

circleMidPoint();

getch();

close_graph();

         /* End of main program */
}

0 comments