Graphvizを使う。

ruby/ioおもしろすぎ。
Linuxの楽しさが、まるごと使える感じだ。
今回は、Graphvizだ。

sudo apt-get install graphviz

参考ページ
http://d.hatena.ne.jp/h-yano/20060727/1154010884

pngファイルをつくるのは

eval = <<EOF
digraph sample1 {
node1 -> node2;
node1 -> node3;
node2 -> node3;
node3 -> node4;
}
EOF
IO.popen("dot -Tpng -o sample.png","r+") {|io|
io.puts(eval)
}

ruby/sdlに乗せるには、


#!usr/bin/env ruby

require 'rubygems'
require 'sdl'
require "stringio"

class Item
def initialize(screen)
@screen = screen
eval = <<EOF
digraph sample1 {
node1 -> node2;
node1 -> node3;
node2 -> node3;
node3 -> node4;
}
EOF
IO.popen("dot | convert - bmp:-","r+") {|io|
io.puts(eval)
io.close_write
StringIO.open(io.read){|s| @surface = SDL::Surface.load_bmp_from_io(s)}
}
@x,@y = 10,10
end

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

class Phase
def initialize(screen)
@screen = screen
@item = Item.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

SDL.init(SDL::INIT_EVERYTHING)
screen = SDL::setVideoMode(180,350,16,SDL::SWSURFACE)
Phase.new(screen).run
[PR]
by gaziya | 2011-02-19 11:02