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;