知り合いに、物理シュミレーションの結果を簡単に描画する方法は無いかと相談されていたので、Ruby/Tkでちょっと遊んでみた。(←仕事しれ)
中央にある円のまわりを小さい円が周るだけのたわいのないスクリプト。万有引力の式とか入れればちっとは面白いかも。
#!/usr/bin/env ruby require "tk" SCREEN_SIZE_X = 500 SCREEN_SIZE_Y = 500 class Planet attr_reader :origin_x, :origin_y, :radius def initialize(canvas, origin_x, origin_y, radius, fillcolor="white", linecolor="white") @canvas = canvas @origin_x = origin_x @origin_y = origin_y @radius = radius @fillcolor = fillcolor @linecolor = linecolor @shape = TkcOval.new(canvas, origin_x - radius, origin_y - radius, origin_x + 2 * radius, origin_y + 2 * radius, "fill" => fillcolor, "outline" => linecolor) end def coords(x, y) @shape.coords(x + @origin_y - @radius, y + @origin_y - @radius, x + @origin_y + @radius, y + @origin_y + @radius) end end TkCanvas.new { |c| width SCREEN_SIZE_X height SCREEN_SIZE_Y TkcRectangle.new(c, 0, 0, SCREEN_SIZE_X, SCREEN_SIZE_Y, "fill" => "navy", "outline" => "navy") center_x = SCREEN_SIZE_X / 2 center_y = SCREEN_SIZE_Y / 2 $earth = Planet.new(c, center_x, center_y, 10, "blue", "lightblue") $moon = Planet.new(c, center_x, center_y, 5, "yellow", "white") pack } def time_pass(n) x = 230 * Math.sin(Math::PI * n * 0.02) y = 230 * Math.cos(Math::PI * n * 0.02) $moon.coords(x, y) end def timer(n) Tk.after(50, proc { time_pass(n) timer(n + 1) }) end timer(0) Tk.mainloop