From: Raf Coremans Date: 2007-05-28T04:28:01+09:00 Subject: Re: [QUIZ] Fractals (#125) ------=_Part_184499_8098599.1180294077921 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline I've gone for short but un-ruby-esque solution. Outputs an image in .pbm format to stdout. Regards, Raf $ cat rq125_fractals_rafc.rb #!/usr/bin/ruby -w # 0: North # 1: East # 2: South # 3: West #Transformation: the sequence of segments that a #segment going North is transformed into: t = [ 0, 3, 0, 1, 0] #Starting situation: one segment going East: f = [ 1] #Build the fractal: ARGV[0].to_i.times{ f = f.map{ |g| t.map{ |u| ( g + u) % 4}}.flatten} #Draw the result: dx = [ 0, 1, 0, -1] dy = [ 1, 0, -1, 0] #Determine dimension of drawing: x = 0 y = 0 minx = 0 miny = 0 maxx = 0 maxy = 0 f.each do |g| x += dx[g] y += dy[g] minx = [x, minx].min miny = [y, miny].min maxx = [x, maxx].max maxy = [y, maxy].max end dimx = (maxx - minx) * 2 + 3 dimy = (maxy - miny) * 2 + 3 drawing = Array.new( dimy) { Array.new( dimx, 0)} #Draw: x = -minx * 2 + 1 y = -miny * 2 + 1 drawing[y][x] = 1 f.each{ |g| 2.times { x += dx[g] y += dy[g] drawing[y][x] = 1 } } #Output PBM: puts 'P1' puts "#{dimx} #{dimy}" drawing.reverse_each { |row| puts row.join( ' ')} __END__ $ #Usage: $ ./rq125_fractals_rafc.rb 5 > f5.pbm ------=_Part_184499_8098599.1180294077921--