ユーザ用ツール

サイト用ツール


ruby:curses-graph

グラフの描画

Curses を使ってグラフを描画する。

プログラムの実行時に “warning: rb_safe_level will be removed in Ruby 3.0” と表示される場合は、次のように ruby にオプション “-W0” を付けて実行するとよい。

% ruby -W0 プログラム

関数のグラフ

func(x) の関数のグラフを描く。

graph.rb
  1. #!/usr/koeki/bin/ruby
  2. # -*- coding: utf-8 -*-
  3. # Curses で関数のグラフを描画する
  4. # 作者:山本裕樹
  5.  
  6. require "curses"
  7.  
  8. # 関数
  9. def func(x)
  10. return 0.001 * x * (x - 25) * (x + 25)
  11. end
  12.  
  13. # Curses の初期化
  14. Curses.init_screen
  15.  
  16. begin
  17. # 色の使用開始
  18. Curses.start_color
  19.  
  20. # グラフ用のカラーペア番号を用意
  21. Curses.init_pair(1, Curses::COLOR_RED, Curses::COLOR_BLACK)
  22.  
  23. # 原点の座標
  24. ox = Curses.cols / 2
  25. oy = Curses.lines / 2
  26.  
  27. # x軸の描画
  28. cy = oy
  29. 0.upto(Curses.cols - 1) do |cx|
  30. Curses.setpos(cy, cx)
  31. Curses.addch("-")
  32. end
  33.  
  34. # y軸の描画
  35. cx = ox
  36. 0.upto(Curses.lines - 1) do |cy|
  37. Curses.setpos(cy, cx)
  38. Curses.addch("|")
  39. end
  40.  
  41. # 原点の描画
  42. Curses.setpos(oy, ox)
  43. Curses.addch("+")
  44.  
  45. # 色の指定
  46. Curses.attrset(Curses.color_pair(1))
  47.  
  48. # グラフの描画
  49. 0.upto(Curses.cols - 1) do |cx|
  50. # 関数に与える x の値
  51. x = cx - ox
  52.  
  53. # 関数
  54. y = func(x)
  55.  
  56. # Curses の y座標への変換
  57. cy = oy - y.round * 0.5
  58.  
  59. # 仮想端末画面の範囲内なら描画
  60. if (cy >= 0 && cy <= Curses.lines - 1)
  61. Curses.setpos(cy, cx)
  62. Curses.addch("*")
  63. end
  64. end
  65.  
  66. # 画面表示を更新
  67. Curses.refresh
  68.  
  69. # 標準入力から1文字だけ読み込む(Enter キー不要)
  70. Curses.getch
  71.  
  72. ensure
  73. # Curses の終了処理
  74. Curses.close_screen
  75. end

sin関数のグラフ

sinegraph.rb
  1. #!/usr/koeki/bin/ruby
  2. # -*- coding: utf-8 -*-
  3. # Curses で sin関数 y=a*sin(k*x) のグラフを描画する
  4. # 作者:山本裕樹
  5.  
  6. require "curses"
  7.  
  8. # 使い方(コマンドライン引数がなければ表示する)
  9. if ARGV.length < 2
  10. puts "使い方: singraph.rb a k"
  11. puts "sin関数 y=a*sin(k*x) のグラフを描画する"
  12. puts ""
  13. puts "例:"
  14. puts "\tsingraph.rb 20 0.2"
  15. exit
  16. end
  17.  
  18. # sin関数のパラメータ
  19. a = ARGV[0].to_f
  20. omega = ARGV[1].to_f
  21.  
  22. # Curses の初期化
  23. Curses.init_screen
  24.  
  25. begin
  26. # 色の使用開始
  27. Curses.start_color
  28.  
  29. # カラーペア番号を用意(7色)
  30. Curses.init_pair(1, Curses::COLOR_BLUE, Curses::COLOR_BLACK)
  31. Curses.init_pair(2, Curses::COLOR_CYAN, Curses::COLOR_BLACK)
  32. Curses.init_pair(3, Curses::COLOR_GREEN, Curses::COLOR_BLACK)
  33. Curses.init_pair(4, Curses::COLOR_MAGENTA, Curses::COLOR_BLACK)
  34. Curses.init_pair(5, Curses::COLOR_RED, Curses::COLOR_BLACK)
  35. Curses.init_pair(6, Curses::COLOR_WHITE, Curses::COLOR_BLACK)
  36. Curses.init_pair(7, Curses::COLOR_YELLOW, Curses::COLOR_BLACK)
  37.  
  38. # 原点の座標
  39. ox = Curses.cols / 2
  40. oy = Curses.lines / 2
  41.  
  42. # x軸の描画
  43. cy = oy
  44. 0.upto(Curses.cols - 1) do |cx|
  45. Curses.setpos(cy, cx)
  46. Curses.addch("-")
  47. end
  48.  
  49. # y軸の描画
  50. cx = ox
  51. 0.upto(Curses.lines - 1) do |cy|
  52. Curses.setpos(cy, cx)
  53. Curses.addch("|")
  54. end
  55.  
  56. # 原点の描画
  57. Curses.setpos(oy, ox)
  58. Curses.addch("+")
  59.  
  60. # グラフの描画
  61. 0.upto(Curses.cols - 1) do |cx|
  62. # sin関数に与える x の値
  63. x = cx - ox
  64.  
  65. # sin関数
  66. y = a * Math.sin(omega * x)
  67.  
  68. # Curses の y座標への変換
  69. cy = oy - y.round * 0.5
  70.  
  71. # 仮想端末画面の範囲内なら描画
  72. if (cy >= 0 && cy <= Curses.lines - 1)
  73.  
  74. # ランダムに色の指定
  75. Curses.attrset(Curses.color_pair(rand(1..7)))
  76.  
  77. # カーソルを座標 (cx,cy) に移動
  78. Curses.setpos(cy, cx)
  79.  
  80. # カーソルの位置に文字を挿入
  81. Curses.addch("*")
  82.  
  83. # 画面表示を更新
  84. Curses.refresh
  85.  
  86. # 0.2秒停止
  87. sleep(0.2)
  88. end
  89. end
  90.  
  91. # 標準入力から1文字だけ読み込む(Enter キー不要)
  92. Curses.getch
  93.  
  94. ensure
  95. # Curses の終了処理
  96. Curses.close_screen
  97. end
ruby/curses-graph.txt · 最終更新: 2022/06/27 16:33 by yuki