Content deleted Content added
No edit summary |
No edit summary |
||
Line 3:
[[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 line algorithm|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.
Here is [[pseudocode]] for the nearly-horizontal case (<math>\
{{wikicode}}
<code>
'''function''' plot(x, y, c)
plot the pixel at (x, y) with brightness c ''// where
▲ '''function''' ipart(x) {
'''return''' ''integer part of x''
▲ '''function''' round(x) {
'''return''' ipart(x + 0.5)
▲ '''function''' fpart(x) {
'''return''' ''fractional part of x''
▲ '''function''' rfpart(x) {
'''return''' 1 - fpart(x)
'''if x2 < x1
▲ ''// check that x<sub>1</sub> < x<sub>2</sub>''
swap x1, x2
dx = x2 - x1
dy = y2 - y1
gradient = dy / dx
''// handle first endpoint''
xend = round(
yend =
xgap = rfpart(
xpxl1 = xend '' // this will be used in the main loop''
ypxl1 = ipart(yend)
▲ intery = yend + gradient ''// first y-intersection for later''
''// handle second endpoint''
xend = round(
yend =
xgap = rfpart(
xpxl2 = xend '' // this will be used in the main loop''
ypxl2 = ipart(yend)
''// main loop''
'''for''' x '''from''' xpxl1 + 1 '''to''' xpxl2 - 1 {
intery = intery + gradient
}
Line 66 ⟶ 60:
==External links==
* [http://www.ece.mcmaster.ca/~xwu/ Xiaolin Wu's homepage]
[[Category:Geometric algorithms]]
|