Wednesday, 12 October 2011

MIDPOINT ELLIPSE DRAWING PROGRAM IN C

midpoint ellipse drawing program in c

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#define Round(a)((int)(a+0.5))

void emidpoint(int xc, int yc, int a, int b);
void eplotpoints(int, int ,int,int);
void main()
{
   int gd=DETECT,gm,x1,y1,a1,b1,xc,yc,x,y;
   initgraph(&gd,&gm,"../bgi");
   printf("enter the center coordinates:");
   scanf("%d%d",&x1,&y1);
   printf("enter a and b such that b>>a:");
   scanf("%d%d",&a1,&b1);
   emidpoint(x1,y1,a1,b1);
   eplotpoints(xc,yc,x,y);
   getch();
}
void emidpoint(int xc, int yc, int a, int b)
{
  int a2=a*a;
  int b2=b*b;
  int twoa2=2*a2;
  int twob2=2*b2;
  int p;
  int x=0;
  int y=b;
  int px=0;
  int py=twoa2*b;
  eplotpoints(xc,yc,x,y);

    /*Region 1*/

  p=Round(b2-(a2*b)+(0.25*a2));
  while(px<py)
  {
     x++;
     px+=twoa2;
     if(p<0)
     p+=b2+px;
     else
     {
    y--;
    py-=twoa2;
    p+=b2+px-py;
     }
    eplotpoints(xc,yc,x,y);
  }
 /*Region 2*/

  p=Round(b2*(x+0.5)*(x+0.5)+a2*(y-1)*(y-1)-(a2*b2));
  while(y>0)
  {
    y--;
    py-=twoa2;
    if(p>0)
    p+=a2-py;
    else
    {
      x++;
      px+=twob2;
      p+=a2-py+px;
    }
    eplotpoints(xc,yc,x,y);
  }
}
void eplotpoints(int xc, int yc, int x, int y)
{
    delay(100);
    putpixel(xc+x,yc-y,58);
    putpixel(xc-x,yc-y,58);
    putpixel(xc+x,yc+y,58);
    putpixel(xc-x,yc+y,58);
}

0 comments:

Post a Comment

 

Copyright @ 2013 Appychip.

Designed by Appychip & YouTube Channel