From: "Ball, Donald A Jr (Library)" Date: 2007-05-30T01:11:22+09:00 Subject: Re: [QUIZ] Fractals (#125) My solution is pastied and attached: http://pastie.caboo.se/65680 I opted to render to the browser using the Safari/Firefox canvas object. IE folks are out of luck as I didn't bother to include the javascript canvas implementation. - donald # Ruby Quiz 125 # Donald Ball # version 1.0 DIRECTIONS = [:east, :south, :west, :north] def instructions(level=1, list=[:forward]) if level == 1 list else instructions(level-1, list.map do |item| case item when :forward [:forward, :left, :forward, :right, :forward, :right, :forward, :left, :forward] else item end end.flatten) end end def plot(instructions) p = [0, 0] points = [p] direction = :east instructions.each do |item| case item when :forward p = p.dup case direction when :east: p[0] += 1 when :south: p[1] -= 1 when :west: p[0] -= 1 when :north: p[1] += 1 end points << p when :left: direction = DIRECTIONS[(DIRECTIONS.index(direction) - 1) % 4] when :right: direction = DIRECTIONS[(DIRECTIONS.index(direction) + 1) % 4] end end points end def to_html(points, width=600, height=400) length = [width/points.map {|p| p[0]}.max, height/points.map {|p| p[1]}.max].min points.map! {|p| p.map {|x| x*length}} s = '' s << "\n" s << "" s << "" s << "" s << "" s end if $0 == __FILE__ puts to_html(plot(instructions(ARGV[0].to_i))) if ARGV.length == 1 end