Xiaolin Wu's line algorithm: Difference between revisions

Content deleted Content added
SmackBot (talk | contribs)
m Delink months and days of week - manual - clean up using AWB
format code
Line 6:
 
<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)
'''returnfunction''' ipart(x) ''integer part of x'is'''
'''return''' ipart(x''integer +part 0.5)of x''
'''function''' round(x)
 
'''return''' ipart(x + 0.5)
'''function''' fpartround(x) '''is'''
'''return''' ''fractionalipart(x part+ of x''0.5)
 
'''function''' rfpart(x)
'''returnfunction''' 1 - fpart(x) '''is'''
'''return''' ''fractional part of x''
'''function''' drawLine(x1,y1,x2,y2)
 
''// check that x1 < x2''
'''function''' ipartrfpart(x) '''is'''
'''if x2 < x1
'''return''' 1 - fpart(x)
swap x1, x2
 
swap y1, y2
'''function''' drawLine(x1,y1,x2,y2) '''is'''
dx = x2 - x1
 
dy = y2 - y1
'''if''' x2 < x1
gradient = dy / dx
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''' round(x)
</code>