# wu反走样(Anti-aliased)直线 (转)

wu反走样(Anti-aliased)直线 (转)[@more@]

========================================================

##### 它是如何工作的?

.假定像素在它们坐标的中心
.直线是一个像素粗
.直线的末端的形状不重要

.两个平行线段,端点相连接,不能分辨出其中一个,一个长线段
.直线的亮度/透明的可以被定义

##### 最终的算法是怎样的?

calculating it presicely.

##### 绘制端点

.把直线延长(向后或向前)到最近的整数x坐标(p).向计算直线上普通的像素对的亮度一样计算这个像素对.然后,因

##### 特殊的情况

.这是我自己的实现方法,我把线拉伸到一个像素常,然后降低它们的亮度.所以一个非常短的直线看上去很暗,当它

##### 最后,一些伪代码

wu直线的定点数计算需要的一些函数:
function trunc(x)
return integer part of x
end of function

function frac(x)
return fractional part of x
end of function

function invfrac(x)
return 1 - (fractional part of x)
end of function wu直线程序:

procedure WuLine(fixpt x1, fixpt y1, fixpt x2, fixpt y2) variable declerations: fixpt variables: grad, xd, yd, length,xm,ym xgap, ygap, xend, yend, xf, yf brigheness1, brigheness2 integer variables: x, y, ix1, ix2, iy1, iy2 byte variables: c1,c2 code starts here: Width and Height of the line xd = (x2-x1) yd = (y2-y1) if abs(xd) > abs(yd) then check line gradient horizontal(ish) lines if x1 > x2 then if line is back to front swap x1 and x2 then swap it round swap y1 and y2 xd = (x2-x1) and recalc xd & yd yd = (y2-y1) end if grad = yd/xd gradient of the line End Point 1 ----------- xend = trunc(x1+.5) find nearest integer X-coordinate yend = y1 + grad*(xend-x1) and corresponding Y value xgap = invfrac(x1+.5) distance i ix1 = int(xend) calc screen coordinates iy1 = int(yend) brightness1 = invfrac(yend) * xgap calc the intensity of the other brightness2 = frac(yend) * xgap end point pixel pair. c1 = byte(brightness1 * MaxPixelValue) calc pixel values c2 = byte(brightness2 * MaxPixelValue) DrawPixel(ix1,iy1), c1 draw the pair of pixels DrawPixel(ix1,iy1+1), c2 yf = yend+grad calc first Y-intersection for main loop End Point 2 ----------- xend = trunc(x2+.5) find nearest integer X-coordinate yend = y2 + grad*(xend-x2) and corresponding Y value xgap = invfrac(x2-.5) distance i ix2 = int(xend) calc screen coordinates iy2 = int(yend) brightness1 = invfrac(yend) * xgap calc the intensity of the first brightness2 = frac(yend) * xgap end point pixel pair. c1 = byte(brightness1 * MaxPixelValue) calc pixel values c2 = byte(brightness2 * MaxPixelValue) DrawPixel(ix2,iy2), c1 draw the pair of pixels DrawPixel(ix2,iy2+1), c2 MAIN LOOP --------- Loop x from (ix1+1) to (ix2-1) main loop brightness1 = invfrac(yf) calc pixel brightnesses brightness2 = frac(yf) c1 = byte(brightness1 * MaxPixelValue) calc pixel values c2 = byte(brightness2 * MaxPixelValue) DrawPixel(x,int(yf)), c1 draw the pair of pixels DrawPixel(x,int(yf)+1), c2 yf = yf + grad update the y-coordinate end of x loop end of loop else vertical(ish) lines handle the vertical(ish) lines in the same way as the horizontal(ish) ones but swap the roles of X and Y end if end of procedure

• 博文量
6241
• 访问量
2411180