class Fx
include Math
def initialize
[url=home.php?mod=space&uid=114926]@sprite[/url] = Sprite.new
@sprite.bitmap = Bitmap.new(640,480)
@sprite.ox,@sprite.oy = 320,240
@sprite.x,@sprite.y = 320,240
end
def clear
@sprite.bitmap.clear
end
# text = f(x) = "2*x"
def draw(text)
for x in -320..320
y = eval(text)
@sprite.bitmap.draw_text(x + 320, 240 - y, 22, 22, '.')
end
end
# args = [[x1,y1],[x2,y2]...]
def lines(*args)
for i in 1..args.size
i == args.size ? line(args[i-1], args[0]) : line(args[i-1], args[i])
end
end
def line(pos1, pos2)
x1,y1 = *pos1
x2,y2 = *pos2
(x2-x1).abs.times do | x |
@sprite.bitmap.draw_text(x1+x+320, 240-x*(y2-y1)/(x2-x1)-y1, 22, 22, '.')
end
end
def transition(color1, color2)
for x in 0..640
r = (color1.red*(640-x) + color2.red*x )/640
g = (color1.green*(640-x) + color2.green*x)/640
b = (color1.blue*(640-x) + color2.blue*x )/640
a = (color1.alpha*(640-x) + color2.alpha*x)/640
@sprite.bitmap.fill_rect(x, 0, 1, 480, Color.new(r,g,b,a))
end
end
# r = 角度 = 30 中心旋转 矩阵变换公式 会造成像素丢失 不如angle
def turn(r)
a = @sprite.bitmap
b = Bitmap.new(a.width*3/2, a.height*3/2)
for x in 0...a.width
for y in 0...a.height
c = a.get_pixel(x, y)
x1 = (x-a.width/2)*cos(r*PI/180)-(y-a.height/2)*sin(r*PI/180)+a.width/2
y1 = (x-a.width/2)*sin(r*PI/180)+(y-a.height/2)*cos(r*PI/180)+a.height/2
b.set_pixel(x1, y1, c)
end
end
@sprite.bitmap = b
end
def angle=(r)
@sprite.angle = 360-r
end
end
f = Fx.new
f.transition(Color.new(0,0,0,255), Color.new(255,255,255,0))
f.draw("sin(x*PI/180)*100")
loop{Graphics.update}
class Fx
include Math
def initialize
[url=home.php?mod=space&uid=114926]@sprite[/url] = Sprite.new
@sprite.bitmap = Bitmap.new(640,480)
@sprite.ox,@sprite.oy = 320,240
@sprite.x,@sprite.y = 320,240
end
def clear
@sprite.bitmap.clear
end
# text = f(x) = "2*x"
def draw(text)
for x in -320..320
y = eval(text)
@sprite.bitmap.draw_text(x + 320, 240 - y, 22, 22, '.')
end
end
# args = [[x1,y1],[x2,y2]...]
def lines(*args)
for i in 1..args.size
i == args.size ? line(args[i-1], args[0]) : line(args[i-1], args[i])
end
end
def line(pos1, pos2)
x1,y1 = *pos1
x2,y2 = *pos2
(x2-x1).abs.times do | x |
@sprite.bitmap.draw_text(x1+x+320, 240-x*(y2-y1)/(x2-x1)-y1, 22, 22, '.')
end
end
def transition(color1, color2)
for x in 0..640
r = (color1.red*(640-x) + color2.red*x )/640
g = (color1.green*(640-x) + color2.green*x)/640
b = (color1.blue*(640-x) + color2.blue*x )/640
a = (color1.alpha*(640-x) + color2.alpha*x)/640
@sprite.bitmap.fill_rect(x, 0, 1, 480, Color.new(r,g,b,a))
end
end
# r = 角度 = 30 中心旋转 矩阵变换公式 会造成像素丢失 不如angle
def turn(r)
a = @sprite.bitmap
b = Bitmap.new(a.width*3/2, a.height*3/2)
for x in 0...a.width
for y in 0...a.height
c = a.get_pixel(x, y)
x1 = (x-a.width/2)*cos(r*PI/180)-(y-a.height/2)*sin(r*PI/180)+a.width/2
y1 = (x-a.width/2)*sin(r*PI/180)+(y-a.height/2)*cos(r*PI/180)+a.height/2
b.set_pixel(x1, y1, c)
end
end
@sprite.bitmap = b
end
def angle=(r)
@sprite.angle = 360-r
end
end
f = Fx.new
f.transition(Color.new(0,0,0,255), Color.new(255,255,255,0))
f.draw("sin(x*PI/180)*100")
loop{Graphics.update}