本帖最后由 Sion 于 2013-9-18 10:32 编辑
这是网上能找到的算法 Link,不过刚好我要用就转成Ruby了
另外,要不带抗锯齿的参考这里 Link
# 抗锯齿 class Bitmap def draw_wu_line(x1, y1, x2, y2, r,g,b,a=255) dx = x2 - x1 dy = y2 - y1 if dx.abs < dy.abs t_ = x1; x1 = y1; y1 = t_ t_ = x2; x2 = y2; y2 = t_ t_ = dx; dx = dy; dy = t_ end if x2 < x1 t_ = x1; x1 = x2; x2 = t_ t_ = y1; y1 = y2; y2 = t_ end set_pixel(x1, y1, Color.new(r,g,b,a)) set_pixel(x2, y2, Color.new(r,g,b,a)) gradient = dy.fdiv(dx) intery = y1 + gradient for x in (x1 + 1)..(x2 - 1) y1 = intery.to_i y2 = intery.ceil set_pixel(x, y1, Color.new(r,g,b, intery.rfpart * a)) set_pixel(x, y2, Color.new(r,g,b, intery.fpart * a)) if y1 != y2 intery = intery + gradient end end end
# 抗锯齿
class Bitmap
def draw_wu_line(x1, y1, x2, y2, r,g,b,a=255)
dx = x2 - x1
dy = y2 - y1
if dx.abs < dy.abs
t_ = x1; x1 = y1; y1 = t_
t_ = x2; x2 = y2; y2 = t_
t_ = dx; dx = dy; dy = t_
end
if x2 < x1
t_ = x1; x1 = x2; x2 = t_
t_ = y1; y1 = y2; y2 = t_
end
set_pixel(x1, y1, Color.new(r,g,b,a))
set_pixel(x2, y2, Color.new(r,g,b,a))
gradient = dy.fdiv(dx)
intery = y1 + gradient
for x in (x1 + 1)..(x2 - 1)
y1 = intery.to_i
y2 = intery.ceil
set_pixel(x, y1, Color.new(r,g,b, intery.rfpart * a))
set_pixel(x, y2, Color.new(r,g,b, intery.fpart * a)) if y1 != y2
intery = intery + gradient
end
end
end
忘了还需要这个
class Numeric def fpart; self - self.to_i end #获取小数部分 def rfpart; 1 - fpart end # 1 - 小数部分 end
class Numeric
def fpart; self - self.to_i end #获取小数部分
def rfpart; 1 - fpart end # 1 - 小数部分
end
|