- This tells us how much the error changes from the current point to the predictor for the next point.
D = F(x1+1, y1+1/2) -F(x1,y1)
= A(x1+1) + B(y1+1/2) + C - (Ax1 +By1+C)
= Ax1 - Ax1 +A +By1 -By1 + 1/2B +C-C
= A + 1/2 B
- D will give us the same indication as before.
- Since (x0,y0) is on the line, F(x0, y0) = 0;
- So we initialize D to be 0
- The starting point has no error
- The first point will be off by A + 1/2 B or (Δy-1/2Δx)
- The next difference will be either
If D was negative in the previous step
D = F(x0+2, y0+1/2) - F(x0+1,y0+1/2) = A = Δy
If D was positive in the previous step
D = F(x0+2, y0+3/2) - F(x0+1,y0+1/2) = A+B = Δy - Δx
- So we need to change D by adding Δy or by adding Δy-Δx
- But we really don't care about the value of D, just the sign.
- So we can multiply everything by 2, and deal with d = 2D
- d0 = 2Δy - Δx
- dn = dn-1+2Δy or dn-1+2Δy-2Δx
- So the algorithm is
dx = x2 - x1
dy = y2 - y1
d = 2*dy - dx
y = y1
x = x1
while x <= x2
WritePixel(x,y,c)
if d > 0
y = y + 1
d = d - 2*dx
end if
d = d + 2*dy
x++