BRESENHAMS LINE:
The Bresenham line algorithm
is an algorithm which determines that in which order one should form
a close approximation to a straight line between two given points. It
is commonly used to draw lines on a computer screen, as it uses only
integer addition, subtraction and bit shifting, all of which are very
cheap operations in standard computer architectures. It is one of the
earliest algorithms developed in the field of computer graphics.
The algorithm is fast, it can
be implemented with integer calculations only and very simple to
describe.
ALGORITHM
- Input the two line end points and store the left end point at (x0,y0)
- Load (x0,y0) into frame buffer i.e. plot first point.
- Calculate constraints 2∆x,2∆y calculating ∆x, ∆y and obtain first decisions parameter value as pk=2(∆y-∆x)
- At each xk along the line, starting at k=0 perform the following test,
If pk<0,
next point is (xk+1,yk)
Pk+1=pk+2∆y
Otherwise, next point to plot
is (xk+1,yk+1)
Pk+1=pk+2∆y-2∆x
- Repeat step 4 ∆x timesSource Code://---------------------------------------------------------------------------#include <vcl\vcl.h>#pragma hdrstop#include "bla.h"//---------------------------------------------------------------------------#pragma resource "*.dfm"TForm1 *Form1;//---------------------------------------------------------------------------__fastcall TForm1::TForm1(TComponent* Owner): TForm(Owner){}//---------------------------------------------------------------------------void __fastcall TForm1::Button1Click(TObject *Sender){int x1=StrToInt(Edit1->Text);int y1=StrToInt(Edit2->Text);int x2=StrToInt(Edit3->Text);int y2=StrToInt(Edit4->Text);int p0,xend,x,y,dy,dx;dy=y2-y1;dx=x2-x1;if(x1>x2){x=x2;y=y2;xend=x1;}else{x=x1;y=y1;xend=x2;}Canvas->Pixels[x][y]=RGB(0,0,368);p0=2*(dy-dx);while(x<=xend){if(p0<0){x++;y=y;p0=p0+2*dy;}else{x++;y++;p0=p0+2*dy-2*dx;}Canvas->Pixels[x][y]=RGB(0,0,368);}}//---------------------------------------------------------------------------
Ouput:
No comments:
Post a Comment