Tuesday, September 3, 2013

Algorithm To Draw Ellipse

//This code is Written in  --->   C++ Builder

//---------------------------------------------------------------------------
#include <vcl\vcl.h>
#pragma hdrstop

#include "ellipse.h"
//---------------------------------------------------------------------------
#pragma resource "*.dfm"
TForm1 *Form1;
void eplot(float, float);
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
     /*float xc=StrToFloat(Edit1->Text);
     float yc=StrToFloat(Edit2->Text);*/
     float rx=StrToFloat(Edit3->Text);
     float ry=StrToFloat(Edit4->Text);
     float x=0,y=ry;
     eplot(x,y);
     float p1=(ry*ry)-(rx*rx*ry)+((rx*rx)/4);
     while((2*ry*ry*x)<=(2*rx*rx*y))
     {
       x++;
       if(p1<=0)
       {
         p1=p1+(2*ry*ry*x)+(ry*ry);
       }
       else
       {
         y--;
         p1=p1+(2*ry*ry*x)+(ry*ry)-(2*rx*rx*y);
       }
       eplot(x,y);
     }
     float p2=(rx*rx)+(2*ry*ry*rx)+((ry*ry)/4);
     x++;
     y++;
     while(y>=0)
     {
       y--;
       if(p2>0)
       {
         p2=p2+(rx*rx)-(2*rx*rx*y);
       }
       else
       {
         x=x++;
         p2=p2+(2*ry*ry*x)-(2*rx*rx*y)+(rx*rx);
       }
       eplot(x,y);
     }
}
//---------------------------------------------------------------------------

void eplot(float x, float y)
{
  float xc=StrToFloat(Form1->Edit1->Text);
  float yc=StrToFloat(Form1->Edit2->Text);
  Form1->Image1->Canvas->Pixels[xc+x][yc-y]=clRed;
  Form1->Image1->Canvas->Pixels[xc-x][yc+y]=clRed;
  Form1->Image1->Canvas->Pixels[xc+x][yc+y]=clRed;
  Form1->Image1->Canvas->Pixels[xc-x][yc-y]=clRed;
}
 
                                                                                                   DOWNLOAD CODE




                                                               DOWNLOAD  *.exe application format files to draw ellipse
Note:  If you want to see how does it work then see the demonstration downloading following
           Application (*.exe) file. Click "Download"
                                                                                                          DOWNLOAD
    
        

No comments:

Post a Comment