- Xiaolin Wu's line algorithm
Xiaolin Wu's line algorithm is an
algorithm for lineantialiasing , which was presented in the article "An Efficient Antialiasing Technique" in the July1991 issue of "Computer Graphics ", as well as in the article "Fast Antialiasing" in the June1992 issue of "Dr. Dobb's Journal ".Bresenham's algorithm draws lines extremely quickly, but it cannot perform anti-aliasing. In addition, it cannot handle the case where the line endpoints do not lie exactly on integer points of the pixel grid. A naïve approach to anti-aliasing the line would take an extremely long time, but Wu's algorithm is quite fast (it is still slower than Bresenham's, though). The basis of the algorithm is to draw pairs of pixels straddling the line, coloured according to proximity. Pixels at the line ends are handled separately. Lines less than one pixel long should be handled as a special case.
An extension to the algorithm for circle drawing was presented by Xiaolin Wu in the book "
Graphics Gems II". Just like the line drawing algorithm is a replacement for Bresenham's line drawing algorithm, the circle drawing algorithm is a replacement for Bresenham's circle drawing algorithm.Pseudocode implementation
Here is
pseudocode for the nearly-horizontal case (). To extend the algorithm to work for all lines, swap the x and y coordinates when near-vertical lines appear (for reference, seeBresenham's line algorithm ). This implementation is only valid for x, y ≥ 0.function plot(x, y, c) is plot the pixel at (x, y) with brightness c (where 0 ≤ c ≤ 1) function ipart(x) is return "integer part of x" function round(x) is return ipart(x + 0.5) function fpart(x) is return "fractional part of x" function rfpart(x) is return 1 - fpart(x) function drawLine(x1,y1,x2,y2) is dx = x2 - x1 dy = y2 - y1 if abs(dx) > abs(dy) then "//handle "horizontal" lines" if x2 < x1 swap x1, x2 swap y1, y2 end if gradient = dy / dx "// handle first endpoint" xend = round(x1) yend = y1 + gradient * (xend - x1) xg
for x from xpxl1 + 1 to xpxl2 - 1 do plot(x, ipart(intery), rfpart(intery)) plot(x, ipart(intery) + 1, fpart(intery)) intery = intery + gradient repeat else "//handle "vertical" lines same code as above but X takes the role of Y end functionReferences
* cite journal
author=Abrash, Michael
url = http://www.gamedev.net/reference/articles/article382.asp
title = Fast Antialiasing (Column)
journal=Dr. Dobb's Journal
month=June | year=1992 | volume=17 | issue=6 | pages=139(7)
* cite journal
author=Wu, Xiaolin
url = http://portal.acm.org/citation.cfm?id=122734
title = An efficient antialiasing technique
journal=Computer Graphics
month=July | year=1991 | volume=25 | issue=4 | pages=143–152
id = ISBN 0-89791-436-8
doi = 10.1145/127719.122734
* cite book
author = Wu, Xiaolin
year = 1991
chapter = Fast Anti-Aliased Circle Generation
editor = James Arvo (Ed.)
title = Graphics Gems II
pages = pp. 446–?
location = San Francisco
publisher = Morgan Kaufmann
id = ISBN 0-12-064480-0External links
* [http://www.ece.mcmaster.ca/~xwu/ Xiaolin Wu's homepage]
Wikimedia Foundation. 2010.