赞 | 0 |
VIP | 10 |
好人卡 | 49 |
积分 | 10 |
经验 | 22958 |
最后登录 | 2020-8-1 |
在线时间 | 2161 小时 |
Lv3.寻梦者 酱油的
- 梦石
- 0
- 星屑
- 1020
- 在线时间
- 2161 小时
- 注册时间
- 2007-12-22
- 帖子
- 3271
|
本帖最后由 禾西 于 2010-9-13 02:29 编辑
- class Bitmap
- #// Wu algorithm
- #--------------------------------------------------------------------------
- # ● 描绘直线
- # x1,y1,x2,y2: 直线两端的坐标
- # color: 颜色
- #--------------------------------------------------------------------------
- def drawline(x1, y1, x2, y2, color)
- x1 = x1.to_f
- y1 = y1.to_f
- x2 = x2.to_f
- y2 = y2.to_f
- draw_a_wuline(x1,y1,x2,y2,color)
- end
- def plot(x,y,c, ori_color)
- color = ori_color.clone
- color.alpha = 255 * c
- set_pixel(x, y, color)
- end
- def ipart(x)
- return x.floor
- end
- def fpart(x)
- return (x - x.floor)
- end
- def rfpart(x)
- return 1 - fpart(x)
- end
- def draw_a_wuline(x1,y1,x2,y2,color)
- dx = x2 - x1 #horizontal distance
- dy = y2 - y1 #perpendicular distance
- if dx.abs < dy.abs
- x1, x2 = x2, x1
- y1, y2 = y2, y1
- dx, dy = dy, dx
- end
- if x2 < x1
- x1, x2 = x2, x1
- y1, y2 = y2, y1
- end
- # find slope
- gradient = dy/dx
- # find initial endpoint
- xend = x1.round
- yend = y1 + gradient * (xend - x1) # y + k(xend - x)
- xgap = rfpart(x1 + 0.5)
- xpxl1 = xend # this will be used in the main loop
- ypxl1 = ipart(yend)
- plot(xpxl1, ypxl1, rfpart(yend) * xgap, color)
- plot(xpxl1, ypxl1 + 1, fpart(yend) * xgap, color)
- intery = yend + gradient # first y-intersection for the main loop
-
- # find terminal endpoint
- xend = x2.round
- 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, color)
- plot(xpxl2, ypxl2 + 1, fpart(yend) * xgap, color)
-
- # main loop
- for x in (xpxl1 + 1) ... (xpxl2 - 1)
- plot(x, ipart(intery), rfpart(intery), color)
- plot(x, ipart(intery) + 1, fpart(intery), color)
- intery = intery + gradient
- end
- end
- end
复制代码 |
|