吳小林直線算法

吳小林算法是一種抗鋸齒線性算法,並作為一種快速有效的抗鋸齒算法發表在1991年7月的《 Computer Graphics》和1992年6月的《Dr. Dobb's Journal》上。

布雷森漢姆(Bresenham)直線算法繪製直線非常快,但它不支持抗鋸齒。此外,它不能處理線段端點的坐標不是整數的情況。一個不成熟的反鋸齒畫線方法需要非常長的時間,但吳的算法是相當快的(雖然它仍然較布雷森漢姆直線算法慢)。該算法的基本思想是畫兩個像素點在岔在直線兩邊,並按照直線相近的顏色著色,而線段末端的像素點另外處理。如果線段寬度小於一像素,將會被作為特殊情況考慮。
這裡有一個繪製圓的擴展算法出版在吳小林的《Graphics Gems II》一書中。就像畫線算法是布雷森漢姆直線算法的替代品一樣,圓繪製算法也是布雷森漢姆圓繪製算法的替代品。
注意:如果在程式開始abs(dx) < abs(dy) 為 true,那么所有的繪圖應該做X和Y逆轉。
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
swap x1, y1
swap x2, y2
swap dx, dy
end if
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)
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 = fpart(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
end function
注意:如果在程式開始abs(dx) < abs(dy) 為 true,那么所有的繪圖應該做X和Y逆轉。

相關詞條

熱門詞條

聯絡我們