C Program For MidPoint Ellipse Drawing Algorithm

Ram Pothuraju
#include< stdio.h>
#include< graphics.h>
#include< conio.h>
#define ROUND(a) ((int)(a+0.5))
void ellipseMidPoint(int xCen,int yCen,int Rx,int Ry);
void ellipsePlot(int,int,int,int);
void main()
{
 int x,y,rx,ry,gdriver=DETECT,gmode;
 clrscr();
 initgraph(&gdriver,&gmode,"");
 setbkcolor(YELLOW);
 printf("\nEnter point(x,y):");
 scanf("%d %d",&x,&y);
 printf("\nEnter radius(rx,ry):");
 scanf("%d %d",&rx,&ry);
 ellipseMidPoint(x,y,rx,ry);
 getch();

}
void ellipseMidPoint(int xCen,int yCen,int Rx,int Ry)
{
 int Rx2=Rx*Rx;
 int Ry2=Ry*Ry;
 int twoRx2=2*Rx2;
 int twoRy2=2*Ry2;
 int x=0;
 int y=Ry;
 int px=0;
 int py=twoRx2*y;
 float p;



 ellipsePlot(xCen,yCen,x,y);

 p=Ry2-(Rx2*Ry)+ROUND(0.25*Rx2);

 while(px < py)
 {
  x++;
  px+=twoRy2;

  if(p < 0)
   p+=Ry2+px;
  else
  {
   y--;
   py-=twoRx2;
   p+=Ry2+px-py;
  }
  ellipsePlot(xCen,yCen,x,y);
 }

 p=Ry2*ROUND(x+0.5)*ROUND(x+0.5)+Rx2*(y-1)*(y-1)-Rx2*Ry2;

 while(y > 0)
 {
  y--;
  py-=twoRx2;
  if(p > 0)
   p+=Rx2-py;
  else
  {
   x++;
   px+=twoRy2;
   p+=Rx2-py+px;

  }
  ellipsePlot(xCen,yCen,x,y);
 }
}

void ellipsePlot(int xCen,int yCen,int x,int y)
{
 putpixel(xCen+x,yCen+y,BLUE);
 putpixel(xCen-x,yCen+y,BLUE);
 putpixel(xCen+x,yCen-y,BLUE);
 putpixel(xCen-x,yCen-y,BLUE);
}

OUTPUT




Post a Comment

0Comments

Post a Comment (0)