Xiaolin Wu's line algorithm: Difference between revisions

Content deleted Content added
m code spacing
Algorithm: Follow-up to previous edit: added animation and original symmetrical integer form of Wu's algorithm
 
(101 intermediate revisions by 80 users not shown)
Line 1:
{{Short description|Line algorithm with antialiasing}}
'''Xiaolin Wu's line algorithm''' is an [[algorithm]] for line [[antialiasing]], which 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]]''.
{{multiple|
{{third-party|date=April 2018}}
{{no footnotes|date=January 2013}}
{{Cleanup|reason=Implementation does not provide explanation|date=November 2023}}
}}
[[File:LineXiaolinWu.gif|thumb|336px|Demonstration of Xiaolin Wu's algorithm]]
'''Xiaolin Wu's line algorithm''' is an [[algorithm]] for line [[spatial anti-aliasing|antialiasing]].
[[File:Xiaolin anti-aliased line comparison.png|thumb|Anti-Aliased Lines (blue) generated with Xiaolin Wu's line algorithm alongside standard lines (red) generated with Bresenham's line algorithm]]
 
==Antialiasing technique==
[[Bresenham's line algorithm|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.
Xiaolin Wu's line algorithm was presented in the article "An Efficient Antialiasing Technique" in the July 1991 issue of ''[[Computer Graphics (newsletter)|Computer Graphics]]'', as well as in the article "Fast Antialiasing" in the June 1992 issue of ''[[Dr. Dobb's Journal]]''.
 
[[Bresenham's line algorithm|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.
Here is [[pseudocode]] for the nearly-horizontal case (<math>\Delta x > \Delta y</math>). To extend the algorithm to work for all lines, swap the x and y coordinates when near-vertical lines appear (for reference, see [[Bresenham's line algorithm]]).
 
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.
<code>
'''function''' plot(x, y, c) '''is'''
plot the pixel at (x, y) with brightness c (where 0 &le; c &le; 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'''
'''if''' x2 < x1
swap x1, x2
swap y1, y2
'''end if'''
dx = x2 - x1
dy = y2 - y1
gradient = dy / dx
''// handle first endpoint''
xend = round(x1)
yend = y1 + gradient * (xend - x1)
xgap = rfpart(x1 + 0.5)
xpxl1 = xend '' // this will be used in the main loop''
ypxl1 = ipart(yend)
plot(xpxl1, ypxl1, rfpart(yend) * xgap)
plot(xpxl1, ypxl1 + 1, fpart(yend) * xgap)
intery = yend + gradient ''// first y-intersection for the main loop''
''// handle second endpoint''
xend = round(x2)
yend = y2 + gradient * (xend - x2)
xgap = rfpart(x2 - 0.5)
xpxl2 = xend '' // this will be used in the main loop''
ypxl2 = ipart(yend)
plot(xpxl2, ypxl2, rfpart(yend) * xgap)
plot(xpxl2, ypxl2 + 1, fpart(yend) * xgap)
''// main loop''
'''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'''
'''end function'''
</code>
 
==Algorithm==
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.
 
Like [[Bresenham's line algorithm|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.
 
[[File:Wu-line-animation.gif|thumb|Animation showing symmetry of Wu's line algorithm ]]
 
The pseudocode below assumes a line where <math>x_0 < x_1</math>, <math>y_0 < y_1</math>,
and the slope <math>k = \frac{dy}{dx}</math> satisfies <math>0 \le k \le 1</math>. 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:
 
* It avoids floating point arithmetic in the main loop (only used to initialize d)
* It renders symmetrically from both ends, halving the number of iterations
* The main loop uses only addition and bit shifts — no multiplication or division
 
 
<syntaxhighlight lang="python" line="1">
function draw_line(x0, y0, x1, y1)
N := 8 # brightness resolution (bits)
M := 15 # fixed-point fractional bits
I := maximum brightness value
 
# Compute gradient and convert to fixed-point step
k := float(y1 - y0) / (x1 - x0)
d := floor((k << M) + 0.5)
 
# Start with fully covered pixels at each end
img[x0, y0] := img[x1, y1] := I
 
D := 0 # Fixed-point accumulator
 
while true:
x0 := x0 + 1
x1 := x1 - 1
if x0 > x1:
break
 
D := D + d
if D overflows:
y0 := y0 + 1
y1 := y1 - 1
 
# Brightness = upper N bits of fractional part of D
v := D >> (M - N)
 
img[x0, y0] := img[x1, y1] := I - v
img[x0, y0 + 1] := img[x1, y1 -1] := v
</syntaxhighlight>
 
===Floating Point Implementation===
 
<syntaxhighlight lang="pascal" line="1">
function plot(x, y, c) is
plot the pixel at (x, y) with brightness c (where 0 ≤ c ≤ 1)
 
// fractional part of x
function fpart(x) is
return x - floor(x)
 
function rfpart(x) is
return 1 - fpart(x)
 
function drawLine(x0,y0,x1,y1) is
boolean steep := abs(y1 - y0) > abs(x1 - x0)
if steep then
swap(x0, y0)
swap(x1, y1)
end if
if x0 > x1 then
swap(x0, x1)
swap(y0, y1)
end if
dx := x1 - x0
dy := y1 - y0
 
if dx == 0.0 then
gradient := 1.0
else
gradient := dy / dx
end if
 
// handle first endpoint
xend := floor(x0)
yend := y0 + gradient * (xend - x0)
xgap := 1 - (x0 - xend)
xpxl1 := xend // this will be used in the main loop
ypxl1 := floor(yend)
if steep then
plot(ypxl1, xpxl1, rfpart(yend) * xgap)
plot(ypxl1+1, xpxl1, fpart(yend) * xgap)
else
plot(xpxl1, ypxl1 , rfpart(yend) * xgap)
plot(xpxl1, ypxl1+1, fpart(yend) * xgap)
end if
intery := yend + gradient // first y-intersection for the main loop
// handle second endpoint
xend := ceil(x1)
yend := y1 + gradient * (xend - x1)
xgap := 1 - (xend - x1)
xpxl2 := xend //this will be used in the main loop
ypxl2 := floor(yend)
if steep then
plot(ypxl2 , xpxl2, rfpart(yend) * xgap)
plot(ypxl2+1, xpxl2, fpart(yend) * xgap)
else
plot(xpxl2, ypxl2, rfpart(yend) * xgap)
plot(xpxl2, ypxl2+1, fpart(yend) * xgap)
end if
// main loop
if steep then
for x from xpxl1 + 1 to xpxl2 - 1 do
begin
plot(floor(intery) , x, rfpart(intery))
plot(floor(intery)+1, x, fpart(intery))
intery := intery + gradient
end
else
for x from xpxl1 + 1 to xpxl2 - 1 do
begin
plot(x, floor(intery), rfpart(intery))
plot(x, floor(intery)+1, fpart(intery))
intery := intery + gradient
end
end if
end function
</syntaxhighlight>
 
==References==
* {{cite journal
| author=Abrash, Michael
| url = http://wwwarchive.gamedev.net/archive/reference/articles/article382.asphtml
| title = Fast Antialiasing (Column)
| journal=[[Dr. Dobb's Journal]]
| yeardate=June 1992 | volume=17 | issue=6 | pages=139(7)
}}
* {{cite journal
Line 73 ⟶ 163:
| url = http://portal.acm.org/citation.cfm?id=122734
| title = An efficient antialiasing technique
| journal=[[ACM SIGGRAPH Computer Graphics]]
| yeardate=July 1991 | volume=25 | issue=4 | pages=143–152
| doi = 10.1145/127719.122734
| id = ISBN 0-89791-436-8
| isbn=0-89791-436-8
| url-access=subscription
}}
* {{cite book
Line 81 ⟶ 173:
| year = 1991
| chapter = Fast Anti-Aliased Circle Generation
| editor = James Arvo (Ed.)
| title = Graphics Gems II
| pages = pp. 446–?446–450
| ___location = San Francisco
| publisher = Morgan Kaufmann
| idisbn = ISBN 0-12-064480-0
}}
 
==External links==
* [http://www.ece.mcmaster.ca/~xwu/ Xiaolin Wu's homepage]
* [https://www.eng.mcmaster.ca/ece/faculty/dr-xiaolin-wu Xiaolin Wu's homepage at McMaster University]
 
{{DEFAULTSORT:Xiaolin Wu's Line Algorithm}}
[[Category:Geometric algorithms]]
[[Category:Anti-aliasing algorithms]]
[[Category:Articles with example pseudocode]]