LoginSignup
3
3

More than 5 years have passed since last update.

Ruby-Processing アナログ時計

Posted at

Ruby-Processing でアナログ時計を作ってみました。

clock2.png

clock.rb
# アナログ時計のサンプル

def setup
  size 300, 300
end

def draw
  background 0

  time = Time.now
  fill 255
  text(sprintf("%d:%d:%d", time.hour, time.min, time.sec), 20, 20)

  translate width/2, height/2 # 原点を画面中央にする
  stroke(100); fill(100); stroke_weight(1)
  line(-width, 0, width, 0)   # X 軸
  line(0, -height, 0, height) # Y 軸

  radius = 100 # 時計の半径

  for i in 0..60-1
    rad = (90.0 / 15 * i).radians
    len = radius - 10
    xx = len * cos(rad)
    yy = len * sin(rad)
    sz = i % 5 == 0 ? 7 : 1
    ellipse(xx, -yy, sz, sz) # 秒ごとの丸マーク
  end

  stroke 255
  no_fill
  ellipse(0, 0, radius*2, radius*2) # 時計の外枠

  # 針の描画関数
  draw_hand = ->(weight, len, deg) {
    rad = (90 - deg).radians
    x = len * cos(rad)
    y = len * sin(rad)
    stroke_weight(weight)
    line(0, 0, x, -y)
  }

  # 秒針
  draw_hand.(1, radius - 10, time.sec * 6)

  # 長針(分)
  draw_hand.(3, radius - 20, time.min * 6 + 6 * time.sec / 60.0)

  # 短針(時)
  draw_hand.(5, radius - 50, time.hour * 30 + 30 * time.min / 60.0)
end

コマンドラインから次のように実行します。

$ rp5 run clock.rb
3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3