Friday, 29 June 2012

c++ program to draw ellipse.

#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
#include<dos.h>
class myellipse
{
 public:
 int x,y,xcenter,ycenter;
 float rx,ry,p;
 void input()
  { 


 
cout<<"\n enter redius value of x = ";
   cin>>rx;
   cout<<"\n enter redius value of y = ";
   cin>>ry;
   cout<<"\n enter xcenter and ycenter =";
   cin>>xcenter>>ycenter;
  }
 void plotpoints(int x,int y);
 void draw_ellipse()
  {
   float rx2 = rx*rx;
   float ry2 = ry*ry;
   float trx2 = 2*rx2;
   float try2 = 2*ry2;
   x=0;
   y=ry;
   float px = 0;
   float py = trx2 * y;
   plotpoints(x,y);
  // REGION 1
   p=(ry2-(rx2*ry)+(0.25*rx2));
   while(px<py)
    {
     x++;
     px=px+try2;
     if(p<0)
       p= p+ry2+px;
     else
      {
       y--;
       py=py-trx2;
       p= p+ry2+px-py;
      }
     plotpoints(x,y);
    }// WHILE LOOP
   // REGION 2
   p=(ry2*(x+0.5)*(x+0.5))+(rx2*(y-1)*(y-1))-(rx2*ry2);
   while(y>0)
    {
     y--;
     py=py-trx2;
     if(p>0)
      p= p+rx2-py;
     else
      {
       x++;
       px=px+try2;
       p= p+rx2-py+px;
      }
     plotpoints(x,y);
    } //WHILE LOOP
   }
 };
 void myellipse :: plotpoints(int x,int y)
  {
   delay(25);
   putpixel(xcenter+x,ycenter+y,4);
   putpixel(xcenter-x,ycenter+y,13);
   putpixel(xcenter+x,ycenter-y,12);
   putpixel(xcenter-x,ycenter-y,10);
  }
 void main()
 {
  clrscr();
  myellipse me;
  int gdriver = DETECT,gmode;
  initgraph( &gdriver, &gmode," ");
  me.input();
  me.draw_ellipse();
  getch();
 }