So how does it work?
Assumptions
It would also be nice if:
What would the ultimate algorithm be?
There is no need for such precision, however. We shall now procede to the next method.
Pixel Pairs
Drawing the end points
Special Cases
Finally, some pseudocode
Fixed Point functions required by WuLines: function trunc(x) return integer part of x end of function function frac(x) return fractional part of x end of function function invfrac(x) return 1 - (fractional part of x) end of function |
The WuLine routine: procedure WuLine(fixpt x1, fixpt y1, fixpt x2, fixpt y2) variable declerations: fixpt variables: grad, xd, yd, length,xm,ym xgap, ygap, xend, yend, xf, yf brigheness1, brigheness2 integer variables: x, y, ix1, ix2, iy1, iy2 byte variables: c1,c2 code starts here: Width and Height of the line xd = (x2-x1) yd = (y2-y1) if abs(xd) > abs(yd) then check line gradient horizontal(ish) lines if x1 > x2 then if line is back to front swap x1 and x2 then swap it round swap y1 and y2 xd = (x2-x1) and recalc xd & yd yd = (y2-y1) end if grad = yd/xd gradient of the line End Point 1 ----------- xend = trunc(x1+.5) find nearest integer X-coordinate yend = y1 + grad*(xend-x1) and corresponding Y value xgap = invfrac(x1+.5) distance i ix1 = int(xend) calc screen coordinates iy1 = int(yend) brightness1 = invfrac(yend) * xgap calc the intensity of the other brightness2 = frac(yend) * xgap end point pixel pair. c1 = byte(brightness1 * MaxPixelValue) calc pixel values c2 = byte(brightness2 * MaxPixelValue) DrawPixel(ix1,iy1), c1 draw the pair of pixels DrawPixel(ix1,iy1+1), c2 yf = yend+grad calc first Y-intersection for main loop End Point 2 ----------- xend = trunc(x2+.5) find nearest integer X-coordinate yend = y2 + grad*(xend-x2) and corresponding Y value xgap = invfrac(x2-.5) distance i ix2 = int(xend) calc screen coordinates iy2 = int(yend) brightness1 = invfrac(yend) * xgap calc the intensity of the first brightness2 = frac(yend) * xgap end point pixel pair. c1 = byte(brightness1 * MaxPixelValue) calc pixel values c2 = byte(brightness2 * MaxPixelValue) DrawPixel(ix2,iy2), c1 draw the pair of pixels DrawPixel(ix2,iy2+1), c2 MAIN LOOP --------- Loop x from (ix1+1) to (ix2-1) main loop brightness1 = invfrac(yf) calc pixel brightnesses brightness2 = frac(yf) c1 = byte(brightness1 * MaxPixelValue) calc pixel values c2 = byte(brightness2 * MaxPixelValue) DrawPixel(x,int(yf)), c1 draw the pair of pixels DrawPixel(x,int(yf)+1), c2 yf = yf + grad update the y-coordinate end of x loop end of loop else vertical(ish) lines handle the vertical(ish) lines in the same way as the horizontal(ish) ones but swap the roles of X and Y end if end of procedure |