argius note

プログラミング関連

練習:サイン波グラフ

グラフ描画クラスを使って、サイン波のグラフを描いてみた。

require "graph"
include GraphModule

GraphScreen.new.loop(1/2.0) do |x|
  theta = x * 360
  sin(theta) # y = sinθ
end

Tk.mainloop

これも、簡易お絵かきクラスを使った練習。Graphクラスを作ったので書き直した。以下は書き直す前。

require "painter"
include Math

def to_radian(d) d * Math::PI / 180 end

class Painter
  attr_accessor :x, :y
  def dot() draw(1, 0).move(-1, 0) end
end

X = 30
Y = 130
UNIT = 5
RATE = 3

p = Painter.new(0, Y)
p.draw(400, 0).set(X, Y - 100).draw(0, 200)

p.set(X, Y)
(0..300).step do |x|
  y = sin(to_radian(x * UNIT)) * -RATE
  p.set(X + x, p.y + y).dot
end

Tk.mainloop

拡張は、いずれ取り込む?