赞 | 95 |
VIP | 77 |
好人卡 | 113 |
积分 | 69 |
经验 | 147345 |
最后登录 | 2024-9-22 |
在线时间 | 7028 小时 |
Lv4.逐梦者 (版主)
- 梦石
- 0
- 星屑
- 6901
- 在线时间
- 7028 小时
- 注册时间
- 2013-11-2
- 帖子
- 1344
|
正好我有一个(原本是C++码,我改成Ruby了)画圆环的码(空心)。要弄成实心的话,大概要调整一下吧。
class Bitmap def draw_ring(x,y,diameter,thickness,color) outer_radius = diameter / 2 inner_radius = outer_radius - thickness + 1 draw_circle_w_thickness(x,y,inner_radius,outer_radius,color) end def xLine(x1,x2,y,color) while x1 <= x2 self.set_pixel(x1,y,color) x1 += 1 end end def yLine(x,y1,y2,color) while y1 <= y2 self.set_pixel(x,y1,color) y1 += 1 end end def draw_circle_w_thickness(xc,yc,inner,outer,color) xo = outer xi = inner y = 0 erro = 1 - xo erri = 1 - xi while(xo >= y) xLine(xc + xi, xc + xo, yc + y, color) yLine(xc + y, yc + xi, yc + xo, color) xLine(xc - xo, xc - xi, yc + y, color) yLine(xc - y, yc + xi, yc + xo, color) xLine(xc - xo, xc - xi, yc - y, color) yLine(xc - y, yc - xo, yc - xi, color) xLine(xc + xi, xc + xo, yc - y, color) yLine(xc + y, yc - xo, yc - xi, color) y += 1 if erro < 0 erro += 2 * y + 1 else xo -= 1 erro += 2 * (y - xo + 1) end if y > inner xi = y else if erri < 0 erri += 2 * y + 1 else xi -= 1 erri += 2 * (y - xi + 1) end end end end end
class Bitmap
def draw_ring(x,y,diameter,thickness,color)
outer_radius = diameter / 2
inner_radius = outer_radius - thickness + 1
draw_circle_w_thickness(x,y,inner_radius,outer_radius,color)
end
def xLine(x1,x2,y,color)
while x1 <= x2
self.set_pixel(x1,y,color)
x1 += 1
end
end
def yLine(x,y1,y2,color)
while y1 <= y2
self.set_pixel(x,y1,color)
y1 += 1
end
end
def draw_circle_w_thickness(xc,yc,inner,outer,color)
xo = outer
xi = inner
y = 0
erro = 1 - xo
erri = 1 - xi
while(xo >= y)
xLine(xc + xi, xc + xo, yc + y, color)
yLine(xc + y, yc + xi, yc + xo, color)
xLine(xc - xo, xc - xi, yc + y, color)
yLine(xc - y, yc + xi, yc + xo, color)
xLine(xc - xo, xc - xi, yc - y, color)
yLine(xc - y, yc - xo, yc - xi, color)
xLine(xc + xi, xc + xo, yc - y, color)
yLine(xc + y, yc - xo, yc - xi, color)
y += 1
if erro < 0
erro += 2 * y + 1
else
xo -= 1
erro += 2 * (y - xo + 1)
end
if y > inner
xi = y
else
if erri < 0
erri += 2 * y + 1
else
xi -= 1
erri += 2 * (y - xi + 1)
end
end
end
end
end
|
|