#/*********************************************************** # ifs.c -- フラクタルによる画像圧縮 #***********************************************************/ require "window.rb" # シダ(fern)のデータ left = -5 bottom = 0 right = 5 top = 10 a = [ 0 , 0.85, 0.2 , -0.15 ] b = [ 0 , 0.04, -0.26, 0.28 ] c = [ 0 , -0.04, 0.23, 0.26 ] d = [ 0.16, 0.85, 0.22, 0.24 ] e = [ 0 , 0 , 0 , 0 ] f = [ 0 , 1.6 , 1.6 , 0.44 ] N = a.size M = 25 * N ip = Array.new(N).fill(0) tbl = [] p = [] # 確率の計算 s = 0.0 for i in 0...N p[i] = (a[i] * d[i] - b[i] * c[i]).abs s += p[i]; ip[i] = i end # 整列 for i in 0...N k = i for j in i...N if (p[j] < p[k]); k = j; end end t = p[i]; p[i] = p[k]; p[k] = t r = ip[i]; ip[i] = ip[k]; ip[k] = r end # 表作成 r = M for i in 0...N k = (r * p[i] / s + 0.5).to_i; s -= p[i] begin tbl[r -= 1] = ip[i] end while ((k -= 1) > 0) end # IFSのアトラクタをプロット gr_on; gr_window(left, bottom, right, top, 1, 0) gr_plot_start x = y = 0 for i in 0...30000 print "\r", i k = (M * rand).to_i j = tbl[k] t = a[j] * x + b[j] * y + e[j] y = c[j] * x + d[j] * y + f[j] x = t if (i >= 10); $gplot.puts "#{x} #{y}"; end end print "\n" gr_plot_end gr_off exit 0 #if 0 ************************************************* =begin # Sierpinskiの三角形のデータ left = 0 bottom = 0 right = 1 top = 1 a = [ 0.5 , 0.5 , 0.5 ] b = [ 0 , 0 , 0 ] c = [ 0 , 0 , 0 ] d = [ 0.5 , 0.5 , 0.5 ] e = [ 0 , 1 , 0.5 ] f = [ 0 , 0 , 0.5 ] # 木のデータ bottom = 0 right = 1 top = 1 a = [ 0 , 0.1 , 0.42, 0.42 ] b = [ 0 , 0 , -0.42, 0.42 ] c = [ 0 , 0 , 0.42, -0.42 ] d = [ 0.5 , 0.1 , 0.42, 0.42 ] e = [ 0 , 0 , 0 , 0 ] f = [ 0 , 0.2 , 0.2 , 0.2 ] # もっと現実的な木のデータ bottom = 0 right = 1 top = 2 a = [ 0.05, 0.05, 0.46, 0.47, 0.43, 0.42 ] b = [ 0 , 0 , -0.32, -0.15, 0.28, 0.26 ] c = [ 0 , 0 , 0.39, 0.17, -0.25, -0.35 ] d = [ 0.6 , -0.5 , 0.38, 0.42, 0.45, 0.31 ] e = [ 0 , 0 , 0 , 0 , 0 , 0 ] f = [ 0 , 1 , 0.6 , 1.1 , 1 , 0.7 ] #endif =end