Bresenham efficient line-Drawing Algorithm

Source: Internet
Author: User


There are many online draw algorithms, but it is not easy to achieve high speed and simplicity. Slope-phase multiplication is one of the simplest methods, but it takes a lot of time to calculate each point for multiplication and division operations. The following describes Bresenham's efficient line-drawing algorithm, you only need to add or subtract the coordinates of each vertex.
The simplified algorithm is described in the pseudo Pascal Language as follows:
Procedure DrawLine (x1, y1, x2, y2: Integer );
Var
X, y, DeltaX, DeltaY, HalfX, ErrorTerm, I: Integer;
Begin
DeltaX: = x2-x1;
DeltaY: = y2-y1;
HalfX: = (x2-x1) shr 1;
ErrorTerm: = 0;
X: = x1;
Y: = y1;
For I: = 0 to DeltaX do
Begin
Plot (X, Y );
Inc (x );
ErrorTerm: = ErrorTerm + DeltaY;
If ErrorTerm> HalfX then
Begin
ErrorTerm: = ErrorTerm-DeltaX;
Inc (y );
End;
End;
End;
To facilitate reading, the above procedures are simplified. The actual program should be slightly corrected to compare the DeltaX and DeltaY values, and to change the start and end points when necessary.
The revised pseudo Pascal algorithm is as follows:
Procedure DrawLine (x1, y1, x2, y2: Integer );
Var
X, y, DeltaX, DeltaY, HalfCount, ErrorTerm, I, Flag: Integer;
Begin
DeltaX: = x2-x1;
DeltaY: = y2-y1;

If Abs (DeltaY) <Abs (DeltaX) then
Begin
If DeltaX <0 then
Begin
I: = x1; x1: = x2; x2: = I;
I: = y1; y1: = y2; y2: = I;
DeltaX: = x2-x1;
DeltaY: = y2-y1;
End;
If DeltaY <0 then Flag: =-1
Else Flag: = 1;
DeltaY: = Abs (DeltaY );
HalfCount: = DeltaX shr 1;
ErrorTerm: = 0;
X: = x1;
Y: = y1;
For I: = 0 to DeltaX do
Begin
Plot (X, Y );
Inc (x );
ErrorTerm: = ErrorTerm + DeltaY;
If ErrorTerm> HalfCount then
Begin
ErrorTerm: = ErrorTerm-DeltaX;
Y: = y + Flag;
End;
End;
End
Else
Begin
If DeltaY <0 then
Begin
I: = x1; x1: = x2; x2: = I;
I: = y1; y1: = y2; y2: = I;
DeltaX: = x2-x1;
DeltaY: = y2-y1;
End;
If DeltaX <0 then Flag: =-1
Else Flag: = 1;
DeltaX: = Abs (DeltaX );
HalfCount: = DeltaY shr 1;
ErrorTerm: = 0;
X: = x1;
Y: = y1;
For I: = 0 to DeltaY do
Begin
Plot (X, Y );
Inc (y );
ErrorTerm: = ErrorTerm + DeltaX;
If ErrorTerm> HalfCount then
Begin
ErrorTerm: = ErrorTerm-DeltaY;
X: = x + Flag;
End;
End;
End;
End;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.