Computer Graphics - Lecture
Computer Graphics - Lecture
Start
X=X1
Y=Y1
DX=X2-X1
DY=Y2-Y1
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
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.
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
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
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
- ١٦ -