-1

私は円周率を計算してルビーの絵に入れようとしています。36 行目 (ファイルに pi [小数点以下なし] を書き込まない) と 63 行目 (オフホワイト色のみを pi.png に書き込む) まで機能します。余談ですが、プログラムは pi ( $looptimes)の正しい桁数を作成していないようです。

ここに私のルビーファイルがあります:

pi.rb

#get width and height of picture
p 'width of pi'
$w = gets.chomp.to_i

p 'height of pi'
$h = gets.chomp.to_i

$looptimes = $w*$h

#calculate pi
def pi

  q, r, t, k, n, l = 1, 0, 1, 1, 3, 3
  dot = nil
  $looptimes.times do
    if 4*q+r-t < n*t
      yield n
      nr = 10*(r-n*t)
      n = ((10*(3*q+r)) / t) - 10*n
      q *= 10
      r = nr
    else
      nr = (2*q+r) * l
      nn = (q*(7*k+2)+r*l) / (t*l)
      q *= k
      t *= l
      l += 2
      k += 1
      n = nn
      r = nr
    end
  end
end

#create and write to file
File.new("pi.txt", 'w')
File.open("pi.txt", 'w') { |pitxt|
    pitxt.write(pi {|digit| print digit; $stdout.flush})}

def read_file(file_name)
    file = File.open(file_name, "r")
    data = file.read
    file.close
    return data
end

pinumber = read_file("./pi.txt")

    pinumber.to_s


require 'chunky_png'

#make picture
image = ChunkyPNG::Image.new($w,$h)
image.save('pi.png')
image = ChunkyPNG::Image.from_file('pi.png')

#draw pixels
for $pointw in 0...$w do
    for $pointh in 0...$h do
        for j in 0...$looptimes do
            if pinumber[j] = '0' 
                image[$pointw,$pointh] = ChunkyPNG::Color.rgb(244,244,244) 
                image.save('pi.png')
            elsif pinumber[j] = '1'
                image[$pointw,$pointh] = ChunkyPNG::Color.rgb(231,47,39) 
                image.save('pi.png')
            elsif pinumber[j] = '2'
                image[$pointw,$pointh] = ChunkyPNG::Color.rgb(241,176,102) 
                image.save('pi.png')
            elsif pinumber[j] = '3' 
                image[$pointw,$pointh] = ChunkyPNG::Color.rgb(255,228,15) 
                image.save('pi.png')
            elsif pinumber[j] = '4' 
                image[$pointw,$pointh] = ChunkyPNG::Color.rgb(18,154,47) 
                image.save('pi.png')
            elsif pinumber[j] = '5' 
                image[$pointw,$pointh] = ChunkyPNG::Color.rgb(126,188,209) 
                image.save('pi.png')
            elsif pinumber[j] = '6' 
                image[$pointw,$pointh] = ChunkyPNG::Color.rgb(3,86,155) 
                image.save('pi.png')
            elsif pinumber[j] = '7'
                image[$pointw,$pointh] = ChunkyPNG::Color.rgb(46,20,141) 
                image.save('pi.png')
            elsif pinumber[j] = '8' 
                image[$pointw,$pointh] = ChunkyPNG::Color.rgb(152,152,152) 
                image.save('pi.png')
            elsif pinumber[j] = '9'     
                image[$pointw,$pointh] = ChunkyPNG::Color.rgb(10,10,10) 
                image.save('pi.png')
            end
        end
    end
end

何かアイデアがあれば教えてください。

また、これを行うより効率的な方法はありますか?ご協力ありがとうございました!

4

1 に答える 1

0

writeコマンドは、使用したように、の戻りpiファイルに書き込みます。からの戻り値piは数字の文字列ではなく、ルーチン内で計算された最後の値です (これは常に への最後の代入になりますr) 。

最も近い修正 (つまり、動作させるために必要な最小限の変更) は、生成された数字をファイルに出力することです。これは、stdout への出力で既に行っています。

File.open("pi.txt", 'w') do |pitxt|
  pi do |digit| 
    print digit
    $stdout.flush
    pitxt.write digit
  end
end

これにより、少なくとも必要な数字がテキスト ファイルに取り込まれます。

スクリプトにはさらにいくつかの間違いがあります。もっとゆっくりと物事を進めることをお勧めします (実際、画像書き込みコードをそのまま削除して、もう一度やり直すことをお勧めします!) 作業が進むにつれて、SO についてさらに質問してください。次の質問では、スクリプト全体を提示して「これの何が問題なのか」と尋ねるのではなく、何をしようとしているのかを示す簡単な例を書いてみてください。たとえば、円周率の数字は既に正しいので、そのコードについて尋ねる必要はありません。画像書き込み部分は、任意の数字列に対応できるはずです。例をまとめることで、自分の質問に答えることができることに気付くかもしれません。

于 2013-08-23T06:51:36.987 に答える