argius note

プログラミング関連

練習:樹木曲線

早速、お絵かきクラスで練習。

require "painter"
include Math

RATE = 6
TURN = 15
DECAY = 0.8

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

def tree(p, n, length, angle)
  x = length * sin(to_radian(angle)) * RATE
  y = length * cos(to_radian(angle)) * RATE * -1
  p.draw(x, y)
  if n > 0
    tree(p, n - 1, length * DECAY, angle + TURN)
    tree(p, n - 1, length * DECAY, angle - TURN)
  end
  p.move(-x, -y)
end

p = Painter.new(186, 270)
tree(p, 8, 10, 0)

Tk.mainloop