Sunday, November 25, 2012

DRAW A CIRCLE USING MIDPOINT ALGORITHM


DRAW A CIRCLE USING MIDPOINT ALGORITHM


#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include<dos.h>

void main()
{
   /* request auto detection */
   int gdriver = DETECT, gmode, errorcode;
   int x1,x2,x3,x4,xc,yc,r,y1,y2;
   char msg[15];
   /* initialize graphics and local variables */
   initgraph(&gdriver, &gmode, "");
   /* read result of initialization */
   errorcode = graphresult();
   /* an error occurred */
   if (errorcode != grOk)
   {
      printf("Graphics error: %s\n", grapherrormsg(errorcode));
      printf("Press any key to halt:");
      getch();
      exit(1);
   }
   printf("Enter the value for xc,yc,r:");
   scanf("%d%d%d",&xc,&yc,&r);
   circlemidpoint(xc,yc,r);
   getch();
   closegraph();
}
void circlemidpoint(int xc,int yc,int r)
{
int x=0,y=r,p=1-r;
void circleplotpoints(int,int,int,int);
circleplotpoints(xc,yc,x,y);
while(x<y)
{
x++;
if(p<0)
p+=2*x+1;
else
{
y--;
p+=2*(x-y)+1;
}
circleplotpoints(xc,yc,x,y);
}
}
 void circleplotpoints(int xc,int yc,int x,int y)
{
putpixel(xc+x,yc+y,YELLOW);
putpixel(xc-x,yc+y,YELLOW);
putpixel(xc+x,yc-y,GREEN);
putpixel(xc-x,yc-y,GREEN);
putpixel(xc+y,yc+x,GREEN);
putpixel(xc-y,yc+x,RED);
putpixel(xc+y,yc-x,RED);
putpixel(xc-y,yc-x,RED);
}


































OUTPUT:-



Enter the value for xc, yc, r:
250
150
125


         




0 comments:

Post a Comment