DRAW AN ELLIPSE
The
midpoint algorithm is applied to the first quadrant in two parts,
region 1 and region 2. We process by taking unit step in x-coordinate
direction and finding the closest value for y for each x steps in
region 1.
In
the first quadrant at region 1 we start at position (0,Ry) and
incrementing X and calculating Y closer to the path along clockwise
direction. When the slope becomes -1 then shift unit step in X to Y
and compute corresponding X closest to ellipse path at region 2 at
the same direction.
ALGORITHM:
- Start
- Input centre co-ordinates of the ellipse
- Input x-radius and y-radius
- For region 1, Pk=Ry2(Xk+1)2+Rx2(Yk-0.5)2-Rx2Ry2
- While px<py, increment x and set Px=Px+2Ry2
- If Pk<0, set Pk+1=Pk+Ry2+Px
- else set y=y-1, Py=Py-2Rx2 and Pk+1=Pk+Ry2+Px-Py
- For region 2, Pk=Ry2(X+0.5)2+Rx2(Y-1)2-Rx2Ry2
- While y>0, decrement the value of y and set Py=Py-2Rx2
- If p>0, set Pk+1=Pk+Rx2-Py
- Else increment Px=Px+2Ry2and set Pk+1=Pk+Rx2+Px-Py
- Display ellipse
- StopSOURCE CODE://---------------------------------------------------------------------------#include <vcl\vcl.h>#pragma hdrstop
#include "ellipse.h"//---------------------------------------------------------------------------#pragma resource "*.dfm"TForm1 *Form1;//---------------------------------------------------------------------------__fastcall TForm1::TForm1(TComponent* Owner): TForm(Owner){}//---------------------------------------------------------------------------void __fastcall TForm1::Button1Click(TObject *Sender){int Xc=StrToInt(Edit1->Text);int Yc=StrToInt(Edit2->Text);int rx=StrToInt(Edit3->Text);int ry=StrToInt(Edit4->Text);int rxSq = rx * rx;int rySq = ry * ry;int X = 0, Y = ry, p;int px = 0, py = 2 * rxSq * Y;Canvas->Pixels[Xc+X][Yc+Y]=RGB(0,0,255);Canvas->Pixels[Xc-X][Yc+Y]=RGB(0,255,0);Canvas->Pixels[Xc+X][Yc-Y]=RGB(255,255,0);Canvas->Pixels[Xc-X][Yc-Y]=RGB(255,0,255);p = rySq - (rxSq * ry) + (0.25 * rxSq);while (px < py){X++;px = px + 2 * rySq;if (p < 0)p = p + rySq + px;else{Y--;py = py - 2 * rxSq;p = p + rySq + px - py;}Canvas->Pixels[Xc+X][Yc+Y]=RGB(0,0,255);Canvas->Pixels[Xc-X][Yc+Y]=RGB(0,255,0);Canvas->Pixels[Xc+X][Yc-Y]=RGB(255,255,0);Canvas->Pixels[Xc-X][Yc-Y]=RGB(255,0,255);}p = rySq*(X+0.5)*(X+0.5) + rxSq*(Y-1)*(Y-1) - rxSq*rySq;while (Y > 0){Y--;py = py - 2 * rxSq;if (p > 0)p = p + rxSq - py;else{X++;px = px + 2 * rySq;p = p + rxSq - py + px;}Canvas->Pixels[Xc+X][Yc+Y]=RGB(0,0,255);Canvas->Pixels[Xc-X][Yc+Y]=RGB(0,255,0);Canvas->Pixels[Xc+X][Yc-Y]=RGB(255,255,0);Canvas->Pixels[Xc-X][Yc-Y]=RGB(255,0,255);}
}//---------------------------------------------------------------------------Ouput:
No comments:
Post a Comment