Task

Implement the   Xiaolin Wu's line algorithm   described in Wikipedia.



This algorithm draws anti-aliased lines.



Related task

   See   Bresenham's line algorithm   for aliased lines.


      PROCdrawAntiAliasedLine(100, 100, 600, 400, 0, 0, 0)
      END
 
      DEF PROCdrawAntiAliasedLine(x1, y1, x2, y2, r%, g%, b%)
      LOCAL dx, dy, xend, yend, grad, yf, xgap, ix1%, iy1%, ix2%, iy2%, x%
 
      dx = x2 - x1
      dy = y2 - y1
      IF ABS(dx) < ABS(dy) THEN
        SWAP x1, y1
        SWAP x2, y2
        SWAP dx, dy
      ENDIF
 
      IF x2 < x1 THEN
        SWAP x1, x2
        SWAP y1, y2
      ENDIF
 
      grad = dy / dx
 
      xend = INT(x1 + 0.5)
      yend = y1 + grad * (xend - x1)
      xgap = xend + 0.5 - x1
      ix1% = xend
      iy1% = INT(yend)
      PROCplot(ix1%, iy1%, r%, b%, g%, (INT(yend) + 1 - yend) * xgap)
      PROCplot(ix1%, iy1% + 1, r%, b%, g%, (yend - INT(yend)) * xgap)
      yf = yend + grad
 
      xend = INT(x2 + 0.5)
      yend = y2 + grad * (xend - x2)
      xgap = x2 + 0.5 - xend
      ix2% = xend
      iy2% = INT(yend)
      PROCplot(ix2%, iy2%, r%, b%, g%, (INT(yend) + 1 - yend) * xgap)
      PROCplot(ix2%, iy2% + 1, r%, b%, g%, (yend - INT(yend)) * xgap)
 
      FOR x% = ix1% + 1 TO ix2% - 1
        PROCplot(x%, INT(yf), r%, b%, g%, INT(yf) + 1 - yf)
        PROCplot(x%, INT(yf) + 1, r%, b%, g%, yf - INT(yf))
        yf += grad
      NEXT
      ENDPROC
 
      DEF PROCplot(X%, Y%, R%, G%, B%, a)
      LOCAL C%
      C% = TINT(X%*2,Y%*2)
      COLOUR 1, R%*a + (C% AND 255)*(1-a), \
      \         G%*a + (C% >> 8 AND 255)*(1-a), \
      \         B%*a + (C% >> 16 AND 255)*(1-a)
      GCOL 1
      LINE X%*2, Y%*2, X%*2, Y%*2
      ENDPROC