18/11/11 17:52:24.96 tvDFPkAv.net
>>359 Ruby
def hilbert_curve(n)
return [[1, 0, 1], [1, 0, 1], [1, 1, 1]] if n == 1
hc = hilbert_curve(n - 1)
[
*hc.transpose.reverse.zip(hc.reverse.transpose).map{|a, b| [*a, 0, *b]},
[1, *Array.new(2**(n + 1) - 3, 0), 1],
*hc.zip(hc).map.with_index{|(a, b), i| [*a, 1/(i + 1), *b]}
]
end
[1, 3].each{|e| puts "n = #{e}", hilbert_curve(e).map{|a| a.join.gsub(/\d/, ?0 => ?□, ?1 => ?■)}.join($/)}
# => n = 1
■□■
■□■
■■■
n = 3
■□■■■■■□■■■■■□■
■□■□□□■□■□□□■□■
■■■□■■■□■■■□■■■
□□□□■□□□□□■□□□□
■■■□■■■□■■■□■■■
■□■□□□■□■□□□■□■
■□■■■■■□■■■■■□■
■□□□□□□□□□□□□□■
■■■□■■■■■■■□■■■
□□■□■□□□□□■□■□□
■■■□■■■□■■■□■■■
■□□□□□■□■□□□□□■
■□■■■□■□■□■■■□■
■□■□■□■□■□■□■□■
■■■□■■■□■■■□■■■