Bresenham Line Drawing Algorithm For Negative Slope Examples PDF
Bresenham Line Drawing Algorithm For Negative Slope Examples PDF
Example 1:
Table 3.7 can be used to determine the octant of the slope. Given a
line segment from (x1, y1) to (x2, y2), first reorder the points, if necessary,
such that x1 ≤ x2, then use the table. The top row of the table reads: If Δy
≥ 0 and Δx ≥ Δy, then the slope is positive and is less than or equal 1.
The octant is either 1 or, if the points had to be swapped, it is 5.
ΔY ΔX?ΔY slope octant
≥0 ≥ Pos ≤ 1 1 (5)
≥0 < Pos >1 2 (6)
<0 ≤ Neg ≥ -1 7 (3)
<0 > Neg < -1 8 (4)
General Bresenham's algorithm for all Octants
Inputs: Start point ( X1 , Y1 ) , End point ( X2 , Y2 )
Begin
X=X1
Y=Y1
ΔX =Abs (X2-X1)
ΔY =Abs(Y2-Y1)
S1=Sign (X2-X1)
S2=Sign (Y2-Y1)
If ΔY > ΔX Then
T= ΔX , ΔX = ΔY , ΔY =T , Interchange=1
Else
Interchange =0
End If
E= 2 ΔY - ΔX
A= 2 ΔY
B= 2ΔY - 2ΔX
Plot (X,Y)
For i=1 to ΔX If
( E < 0)
If Interchange=1 Then Y= Y + S2
Else X=X+S1
E= E +A
Else Y=Y + S2
X=X + S1
E=E+B
End If
SetPixel (X,Y)
End for
End
0 0 0 (0,0)
1 -8 -1 -1 (-1,-1)
2 0 -2 -1 (-2,-1)
3 -8 -3 -2 (-3,-2)
4 0 -4 -2 (-4,-2)
5 -8 -5 -3 (-5,-3)
6 0 -6 -3 (-6,-3)
7 -8 -7 -4 (-7,-4)
8 0 -8 -4 (-8,-4)