1-3 : Bresenham's algorithm
Bresenham algorithm seeks to select the optimum raster locations to
represent a straight line. To accomplish this the algorithm always
increment by one unit in either X or Y depending on the slop of the
line. The slop of the line represent the error between the location of
the real line and the location of the drawn line.
The algorithm is cleverly constructed so that only the sign of this
error need be examined.
Bresenham algorithm for the first octant : { 0 ≤ ∆Y ≤ ∆X }
Start
X=X1
Y=Y1
DX=X2-X1
DY=Y2-Y1
E= (DY / DX) – 0.5
For I=1 to DX
Begin
Plot (X,Y)
While (E ≥ 0 )
Y=Y+1
E=E-1
End While
X=X+1
E=E + ( DY / DX )
End
Finish
- ١٢ -
Example 3 : Consider the line from (0,0) to (5,5)
Rasterize the line with Bresenham algorithm
Sol 3 : X=0 ; Y=0 ; DX=5 ; DY=5 ; E=0.5
I Plot E X Y
0.5 0 0
1 (0,0) -0.5 0 1
0.5 1 1
2 (1,1) -0.5 1 2
0.5 2 2
3 (2,2) -0.5 2 3
0.5 3 3
4 (3,3) -0.5 3 4
0.5 4 4
5 (4,4) -0.5 4 5
0.5 5 5
1-4 : Integer Bresenham algorithm
Bresenham algorithm requires the use of floating point arithmetic
and division to calculate the slop of the line and to evaluate the error
term. The speed of the algorithm can be increased by using integer
arithmetic and eliminating the division.
Since only the sign of the error term is important, the simple
transformation
_
E = E * 2 * ∆X
of the error term in the previous algorithm yields an integer
algorithm. This allows the algorithm to be efficiently implemented in
hardware.
So : E={ (DY / DX) – 0.5 } * 2 ∆X Æ E= 2 ∆Y - ∆X
E= { E-1 } * 2 ∆X Æ E= E - 2 ∆X
E= { E + ( DY / DX ) } * 2 ∆X Æ E = E+ 2 ∆Y
- ١٣ -
Bresenham's integer algorithm for the first octant { 0 ≤ ∆Y ≤ ∆X }
i.e. slop between zero and one
Start
X=X1
Y=Y1
dX=X2-X1
dY=Y2-Y1
E= 2 ∆Y - ∆X
For I=1 to dX
Begin
Plot (X,Y)
While (E ≥ 0 )
Begin
Y=Y+1
E= E - 2 ∆X
End While
X=X+1
E = E+ 2 ∆Y
Next I
Finish
- ١٤ -
1-5 : General Bresenham's algorithm
A full implementation of Bresenham algorithm requires
modification for lying in the other octanats. These can easily be
developed by considering quadrant in which the line lies and its
slope. When the absolute magnitude of the slop of the line is > 1 ,
Y is incremented by one end Bresenham error is used to determine
when to increment X.
Whether X or Y incremented by +(-) 1 depends on the quadrant.
General Bresenham's algorithm for all quadrants
X=X1
Y=Y1
dX=Abs (X2-X1)
dY=Abs(Y2-Y1)
S1=Sign (X2-X1)
S2=Sign (Y2-Y1)
If dY > dX Then
Begin
T=dX : dX=dY : dY=T : Interchange=1
End
Else
Interchange =0
End If
E= 2 dy - dx
For I=1 to dX
Plot (X,Y)
While ( E ≥ 0)
Begin
If Interchange=1 Then X=X + S1
Else Y= Y + S2
End If
E= E - 2 dx
End While
If Interchange=1 Then Y=Y + S2
Else X=X + S1
End If
E = E+ 2 dy
Next I
Finish
- ١٥ -
Example 4 : Draw the line from (0,0) to (-8,-4) using General
Bresenham algorithm
Sol 4 : X=0 ; Y=0 ; dX=8 ; dY=4 ; S1=-1 ; S2=-1
Because dX>dY then Interchange=0 ; E=0
I Plot E X Y
0 0 0
1 (0,0)
-16 0 -1
-8 -1 -1
2 (-1,-1)
0 -2 -1
3 (-2,-1)
-16 -2 -2
-8 -3 -2
4 (-3,-2)
0 -4 -2
5 (-4,-2)
-16 -4 -3
-8 -5 -3
6 (-5,-3)
0 -6 -3
7 (-6,-3)
-16 -6 -4
8 -7 -4
8 (-7,-4)
0 -8 -4
- ١٦ -