赞 | 3 |
VIP | 300 |
好人卡 | 28 |
积分 | 5 |
经验 | 86206 |
最后登录 | 2023-8-1 |
在线时间 | 1552 小时 |
Lv2.观梦者 永无止境的旅程
- 梦石
- 0
- 星屑
- 503
- 在线时间
- 1552 小时
- 注册时间
- 2012-6-19
- 帖子
- 1226
|
- class Bitmap
- #--------------------------------------------------------------------------
- # ● 描绘直线
- # x1,y1,x2,y2: 直线两端的坐标
- # width: 宽度
- # color: 颜色
- #--------------------------------------------------------------------------
- def drawline(x1, y1, x2, y2, width, color)
- x1 = x1.to_f
- y1 = y1.to_f
- x2 = x2.to_f
- y2 = y2.to_f
- width = width.to_f
- k = (y2 - y1) / (x2 - x1)
- if k.abs > 1
- drawline_x(x1, y1, x2, y2, width, color)
- else
- drawline_y(x1, y1, x2, y2, width, color)
- end
- end
- def drawline_x(x1, y1, x2, y2, width, color)
- l = ((x1 - x2) ** 2 + (y1 - y2) ** 2) ** 0.5 * width / (y1 - y2)
- length = l.abs * 2
- k = (x2 - x1) / (y2 - y1) #x=ky+b
- b = x1 - k * y1
- if l > 0
- for ty in y2.to_i..y1.to_i
- tx = ty * k + b
- fill_rect(tx - l, ty, length, 1, color)
- end
- else
- for ty in y1.to_i..y2.to_i
- tx = ty * k + b
- fill_rect(tx + l, ty, length, 1, color)
- end
- end
- end
- def drawline_y(x1, y1, x2, y2, width, color)
- l = ((x1 - x2) ** 2 + (y1 - y2) ** 2) ** 0.5 * width / (x1 - x2)
- height = l.abs * 2
- k = (y2 - y1) / (x2 - x1) #y=kx+b
- b = y1 - k * x1
- if l > 0
- for tx in x2.to_i..x1.to_i
- ty = tx * k + b
- fill_rect(tx, ty - l, 1, height, color)
- end
- else
- for tx in x1.to_i..x2.to_i
- ty = tx * k + b
- fill_rect(tx, ty + l, 1, height, color)
- end
- end
- end
- end
复制代码
- 使用方式
- @sprite = Sprite.new
- @sprite.bitmap = Bitmap.new(100,100)
- @sprite.bitmap.drawline(0,0,100,100,Color.new(255,255,255))
复制代码 |
评分
-
查看全部评分
|