![]() | This article has multiple issues. Please help improve it or discuss these issues on the talk page . (Learn how and when to remove these messages)
|
Xiaolin Wu's line algorithm is an algorithm for line antialiasing.
Xiaolin Wu's line algorithm was presented in the article "An Efficient Antialiasing Technique" in the July 1991 issue of Computer Graphics , as well as in the article "Fast Antialiasing" in the June 1992 issue of Dr. Dobb's Journal .
Bresenham's algorithm draws lines extremely quickly, but it does not perform anti-aliasing. In addition, it cannot handle any cases where the line endpoints do not lie exactly on integer points of the pixel grid. A naive approach to anti-aliasing the line would take an extremely long time. Wu's algorithm is comparatively fast, but is still slower than Bresenham's algorithm. The algorithm consists of drawing pairs of pixels straddling the line, each coloured according to its distance from the line. Pixels at the line ends are handled separately. Lines less than one pixel long are 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 as 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.
Like Bresenham’s line algorithm, this method steps along one axis and considers the two nearest pixels to the ideal line. Instead of choosing the nearest, it draws both, with intensities proportional to their vertical distance from the true line. This produces smoother, anti-aliased lines.
The pseudocode below assumes a line where , , and the slope satisfies . This is a standard simplification — the algorithm can be extended to all directions using symmetry.
The algorithm is well-suited to older CPUs and microcontrollers because:
functiondraw_line(x0,y0,x1,y1)N:=8# brightness resolution (bits)M:=15# fixed-point fractional bitsI:=maximumbrightnessvalue# Compute gradient and convert to fixed-point stepk:=float(y1-y0)/(x1-x0)d:=floor((k<<M)+0.5)# Start with fully covered pixels at each endimg[x0,y0]:=img[x1,y1]:=ID:=0# Fixed-point accumulatorwhiletrue:x0:=x0+1x1:=x1-1ifx0>x1:breakD:=D+difDoverflows:y0:=y0+1y1:=y1-1# Brightness = upper N bits of fractional part of Dv:=D>>(M-N)img[x0,y0]:=img[x1,y1]:=I-vimg[x0,y0+1]:=img[x1,y1-1]:=v
functionplot(x,y,c)isplotthepixelat(x,y)withbrightnessc(where0≤c≤1)// fractional part of xfunctionfpart(x)isreturnx-floor(x)functionrfpart(x)isreturn1-fpart(x)functiondrawLine(x0,y0,x1,y1)isbooleansteep:=abs(y1-y0)>abs(x1-x0)ifsteepthenswap(x0,y0)swap(x1,y1)endififx0>x1thenswap(x0,x1)swap(y0,y1)endifdx:=x1-x0dy:=y1-y0ifdx==0.0thengradient:=1.0elsegradient:=dy/dxendif// handle first endpointxend:=floor(x0)yend:=y0+gradient*(xend-x0)xgap:=1-(x0-xend)xpxl1:=xend// this will be used in the main loopypxl1:=floor(yend)ifsteepthenplot(ypxl1,xpxl1,rfpart(yend)*xgap)plot(ypxl1+1,xpxl1,fpart(yend)*xgap)elseplot(xpxl1,ypxl1,rfpart(yend)*xgap)plot(xpxl1,ypxl1+1,fpart(yend)*xgap)endifintery:=yend+gradient// first y-intersection for the main loop// handle second endpointxend:=ceil(x1)yend:=y1+gradient*(xend-x1)xgap:=1-(xend-x1)xpxl2:=xend//this will be used in the main loopypxl2:=floor(yend)ifsteepthenplot(ypxl2,xpxl2,rfpart(yend)*xgap)plot(ypxl2+1,xpxl2,fpart(yend)*xgap)elseplot(xpxl2,ypxl2,rfpart(yend)*xgap)plot(xpxl2,ypxl2+1,fpart(yend)*xgap)endif// main loopifsteepthenforxfromxpxl1+1toxpxl2-1dobeginplot(floor(intery),x,rfpart(intery))plot(floor(intery)+1,x,fpart(intery))intery:=intery+gradientendelseforxfromxpxl1+1toxpxl2-1dobeginplot(x,floor(intery),rfpart(intery))plot(x,floor(intery)+1,fpart(intery))intery:=intery+gradientendendifendfunction