Pages

Tuesday, August 20, 2013

Ellipse

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

int main(void)
{
   /* request auto detection */
   int gdriver = DETECT, gmode, errorcode;
   float x,y,rx,ry,xc,yc,p;

   /* initialize graphics and local variables */
   initgraph(&gdriver, &gmode, "C:\\TC\\BGI");

   /* read result of initialization */
   errorcode = graphresult();
   if (errorcode != grOk)  /* an error occurred */
   {
      printf("Graphics error: %s\n", grapherrormsg(errorcode));
      printf("Press any key to halt:");
      getch();
      exit(1); /* terminate with an error code */
   }

   printf("enter the radius of ellipse");
   scanf("%f %f",&rx,&ry);
   printf("cnter the centre of ellipse");
   scanf("%f %f",&xc,&yc);
   x=0;
   y=ry;

   p=ry*ry-rx*rx*ry+1.0/4.0*rx*rx;
   do
   {
   if(p<0)
   {
   x=x+1;
   y=y;
   p=p+2*ry*ry*x+3*ry*ry;
   }
   else
   {
   x=x+1;
   y=y-1;
   p=p+2*ry*ry*x+3*ry*ry-2*rx*rx*y+2*rx*rx;
   }
   putpixel(x+xc,y+yc,100);
   putpixel(xc-x,yc+y,100);
   putpixel(xc-x,yc-y,100);
   putpixel(xc+x,yc-y,100);
   }
   while(2*x*ry*ry<=2*y*rx*rx);



   p=ry*ry*(x+0.5)*(x+0.5)+rx*rx*(y-1)*(y-1)-rx*rx*ry*ry;
   do
   {
   if(p>0)
   {
   x=x;
   y=y-1;
   p=p-2*rx*rx*y+3*rx*rx;
   }
   else
   {
   x=x+1;
   y=y-1;
   p=p-2*rx*rx*y+3*rx*rx+2*ry*ry*x+2*ry*ry;
   }
   putpixel(x+xc,y+yc,100);
   putpixel(xc-x,yc+y,100);
   putpixel(xc-x,yc-y,100);
   putpixel(xc+x,yc-y,100);
   }
   while(y>0);


   getch();
  }

No comments:

Post a Comment