LineDrawingAlgorithm
4
•For drawing a line AB with endpoints (x
1, y
1) and (x
2,y
2), the slopeof theline is:
m=(y
2-y
1/x
2-x
1)orm=dy/dx
•Ifpointsare(x
k,y
k)and(x
k+1,y
k+1)thenslope of linewillbe
m=(y
k+1-y
k)/(x
k+1-x
k)
•Fordifferentvaluesofm,differentcalculationsareneeded
BresanhamLineDrawingAlgorithm
18
•From position (2,3)we have to
choosebetween(3,3)and(3,4)
•Wewouldlikethepointthatis
closertotheoriginalline
BresenhamLinedrawingalgorithm
19
•Atsamplepositionx
k+1thevertical
separationsfromthemathematicallineare
labelledd
upperandd
lower
Theycoordinateonthemathematicallineat x
k+1 is:
y m(x
k1)b
m(x
k1)by
kd
l o w e ryy
k
1)yy
k1m(x
k1)b
d
upper2m(x
k1)2y
k2b1
my/x
d
upper(y
k
d
lower
Decisionparameter:
p
kx(d
lowd
uerp)
p2
ey
rx
k2xy
k
BresenhamLinedrawingalgorithm
SuccessiveDecisionParameter
Atstepk+1
p
k+1=2Δy.x
k+1-2Δx.y
k+1+b
Subtractingtwosubsequentdecisionparametersyields:
p
k+1-p
k=2Δy.(x
k+1-x
k)-2Δx.(y
k+1-y
k)
x
k+1=x
k+1so
p
k+1=p
k+2Δy-2Δx.(y
k+1-y
k)
y
k+1-y
kiseither0 or1 dependingonthesignofp
k
Firstparameterp
0
p
0=2Δy-Δx
21
BresenhamLinedrawingalgorithm(|m|<1)
1.Inputthetwolineendpointsand storetheleftendpointin(x
0,y
0).
2.Load(x
0,y
0)intotheframebuffer;thatis,plotthefirstpoint.
3.CalculateconstantsΔx,Δy,2Δy,and2Δy-2Δx,andobtainthestartingvaluefor
thedecisionparameteras
p
0=2Δy-Δx
4.At each x
k along the line, starting at k = 0, perform the following test:
Ifp
k<0, thenextpointtoplotis(x
k+1,y
k)and
22
p
k+1=p
k+2Δy
Otherwise,thenextpointtoplotis (x
k+1,y
k+1)and
p
k+1=p
k+2Δy-2Δx
5.Repeatstep4Δx-1times.