Last active
July 23, 2016 13:39
-
-
Save rhenium/fa95e0620c03e4e98ef87aaa12896117 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# usage: | |
# | |
# gem install rb-inotify | |
# ruby tsp_visualize.rb tech30000.{in,out} | |
require "tk" | |
require "rb-inotify" | |
RADIUS = 400 | |
canvas = TkCanvas.new do |c| | |
width RADIUS * 2 | |
height RADIUS * 2 | |
background "#ffffff" | |
pack | |
end | |
begin | |
inf = File.open(ARGV[0]) | |
dimension = inf.gets.to_i | |
nodes = dimension.times.map { inf.gets.split.map(&:to_f).map(&:-@) } | |
xs, ys = nodes.transpose | |
dx = xs.max - xs.min | |
dy = ys.max - ys.min | |
ratio = RADIUS * 2 / [dx, dy].max | |
cx = RADIUS - (xs.max + xs.min) * ratio / 2 | |
cy = RADIUS - (ys.max + ys.min) * ratio / 2 | |
nodes.map! { |x, y| [x * ratio + cx, y * ratio + cy] } | |
ensure | |
inf.close if inf | |
end | |
old = TkcPolygon.new(canvas, [], fill: "", width: 1, outline: "#ff0000") | |
new = TkcPolygon.new(canvas, [], fill: "", width: 1, outline: "#000000") | |
update = -> file do | |
puts "rendering: #{file}" | |
File.open(file) do |outf| | |
tour = outf.read.split.map(&:to_i) | |
old.coords(*new.coords) if old | |
new.coords(*tour.flat_map { |i| nodes[i] }) | |
end | |
end | |
notifier = INotify::Notifier.new | |
notifier.watch(ARGV[1], :close_write, :dont_follow) do |e| | |
update.call(ARGV[1]) | |
end | |
update.call(ARGV[1]) | |
Thread.abort_on_exception = true | |
Thread.new { notifier.run } | |
Tk.mainloop |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment