gnuplotを表示。

gnuplot も試した。
まず
sudo apt-get install gnuplot-nox gnuplot-x11

convertを使って縮小した。
こんな感じ

#!usr/bin/env ruby

require 'rubygems'
require 'sdl'
require 'cairo'

class Cell
def initialize(screen)
@screen = screen
IO.popen("gnuplot" ,"r+") do |io|
io.puts("set terminal png")
io.puts("set output")
io.puts("plot sin(x)")
io.close_write
@data = io.read
end
IO.popen("convert - -resize 60% -" ,"r+") do |io|
io.write(@data)
io.close_write
@image = Cairo::ImageSurface.from_png(io)
end
stride = Cairo::Format.stride_for_width(C_FORMAT,@image.width)
@surface = SDL::Surface.new_from(@image.data,@image.width,@image.height,32,stride,R,G,B,A)
@x,@y = 30,30
end

def update
@screen.put(@surface,@x,@y)
end
end

class Phase
def initialize(screen)
@screen = screen
@item = Cell.new(@screen)
end

def update
@item.update
end

def handling(event)
exit if event.kind_of?(SDL::Event::Quit) || (event.kind_of?(SDL::Event::KeyDown) && event.sym == SDL::Key::ESCAPE)
end

def run
loop do
while e=SDL::Event.poll
handling(e)
end
update
@screen.flip
SDL.delay(50)
end
end
end

C_FORMAT = Cairo::FORMAT_ARGB32
R,G,B,A = 0x00FF0000,0x0000FF00,0x000000FF,0xFF000000
SDL.init(SDL::INIT_EVERYTHING)
SDL::WM.set_caption("Rotate","")
screen = SDL::setVideoMode(450,350,16,SDL::SWSURFACE)
Phase.new(screen).run
[PR]
by gaziya | 2011-02-11 12:56