DDA (int x1, int y1, int x2, int y2)
- int x
- float y = y1
- float m = (y2-y1)/(x2-x1)
- for(x = x1; x≤ x2; x+= 1)
- plot(x, round(y))
- y += m
- plot(x2,y2)
- plot(x1,y1)
y-y1 = m(x-x1) m = Δy/Δx Substituting in for m and multiplying through by Δx we get (y-y1)Δx = (x-x1)Δy Δx = x2-x1, Δy = y2-y1 yΔx - xΔy -y1Δx + x1Δy = 0 partially expanding some Δx and Δy yΔx - xΔy -y1(x2-x1) + x1(y2-y1) = 0 yΔx - xΔy -y1x2 +y1x1 + x1y2 -x1y1 = 0 yΔx - xΔy -y1x2 + x1y2 = 0 let x1y2-y1x2 = c yΔx - xΔy + c = 0 D'(i,j) = jΔx - iΔy + c
D(i+2, j+1/2) = 2(j+1/2)Δx - 2(i+2)Δy + 2c = 2(j+1/2)Δx - 2iΔy - 4Δy + 2c = 2(j+1/2)Δx - 2iΔy - 2Δy - 2Δy + 2c = 2(j+1/2)Δx - 2(i + 1)Δy +2c - 2Δy = D(i+1, j+1/2) - 2Δy
D(i+2, j+3/2) = 2(j+3/2)Δx - 2(i+2)Δy + 2c 2(j+3/2)Δx = 2jΔx + 3Δx = 2jΔx + Δx + 2Δx = 2(j+1/2) Δx + 2Δx SO D(i+2, j+3/2) = 2(j+1/2)Δx - 2(i + 1)Δy +2c + 2Δx - 2Δy = D(i+1, j+1/2) + 2Δx - 2Δy
D(x1+1, y1+1/2) = 2yΔx - 2(x+1)Δy + 2c = 2(y1+1/2)Δx - 2(x1+1)Δy + 2(x1y2 - x2y1) = 2y1Δx + Δx - 2x1Δy - 2Δy + 2x1y2 - 2x2y1 = 2x2y1 - 2x1y1 - 2x1y2 + 2x1y1 + 2x1y2 - 2x2y1 + Δx - 2Δy = 2x2y1 - 2x2y1 - 2x1y1 + 2x1y1 - 2x1y2 + 2x1y2 + Δx - 2Δy = Δx - 2Δy
Bressenhams(x1, y1, x2, y2)
- int y = y1
- int dy = y2- y1
- int dx = x2- x1
- int diff1 = -2dy
- int diff2 = diff1 + 2dx
- int D = dx - diff2
- for(int x=x1; x ≤ x2; x++)
- plot(x,y);
- if D > 0
- D+= diff1
- else
- D+= diff2
- y++