Friday, December 29, 2017

C/C++ program to print the ellipse by mid point method

//program to print the ellipse by mid point method
Source code

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

void plotellipse(int x,int y)
{
int xmax=getmaxx();
int ymax=getmaxy();
putpixel(x+xmax/2,ymax/2-y,15);
putpixel(x+xmax/2,ymax/2+y,15);
putpixel(xmax/2-x,ymax/2-y,15);
putpixel(xmax/2-x,ymax/2+y,15);
}
int main(void)
{
  /* request auto detection */
  int gdriver = DETECT, gmode, errorcode;
  int xmax, ymax,x,y;
  float d;
  int a,b,i;
  printf("Enter the value a&b ");
  scanf("%d%d",&a,&b);
  clrscr();
  /* 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);
  }

  setcolor(getmaxcolor());
  xmax = getmaxx();
  ymax = getmaxy();
  x=xmax/2;
  y=ymax/2;
  d=pow(b,2)-pow(a,2)*b+pow(a,2)/4;
  for(i=0;i<xmax;i++)
   putpixel(i,y,15);
  for(i=0;i<ymax;i++)
   putpixel(x,i,15);
  x=0;
  y=b;
  while((pow(a,2)*(y-.5))>(pow(b,2)*(x+1)))
    {
     if(d<0)
      {
d+=(pow(b,2)*(2*x+3));
x++;
      }
     else
      {
d+=pow(b,2)*(2*x+3)+pow(a,2)*(-2*y+2);
x++;
y--;
      }
     plotellipse(x,y);
     delay(10);
    }
  d=pow(b,2)*pow((x+.5),2)+pow(a,2)*pow((y-1),2)-pow(a,2)*pow(b,2);
  while(y>0)
    {
     if(d<0)
      {
d+=pow(b,2)*(2*x+2)+pow(a,2)*(-2*y+3);
x++;
y--;
      }
     else
      {
d+=pow(a,2)*(-2*y+3);
y--;
      }
     plotellipse(x,y);
     delay(100);
    }
  /* clean up */
  getch();
  closegraph();

  return 0;}

No comments:

Post a Comment