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 first part of the circle using Mid Point Algorithm.#include<graphics.h>
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
/* ****************************** */
/* A C prgram for drawing a Circle */
/* Using: Mid Point Algorithm */
/* Written By: Ritesh Kumar Jain */
/* ****************************** */
void circlePlotPoints(int, int, int, int);
void initialize_graph(void)
{
/* Select a driver and mode that supports */
/* set also the bkcolor */
/* 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;
/* read the center and the radius of the circle from user */
printf("********* Welcome to Circle Program ********* \n\n");
printf("Enter the central point \n");
printf(" X= "); scanf("%d",&xCenter);
printf(" Y= "); scanf("%d",&yCenter);
printf("Enter the radius ?"); scanf("%d",&radius);
x = 0;
y = radius;
p = 1- radius;
/* plot 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 */
}
void circlePlotPoints(int xCenter, int yCenter, int x, int y)
{
setpixel(xCenter + x, yCenter + y);
}
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
Post a Comment