def escapes(cr, ci, it) """ Does iterating z <- z^2 + c escape after it iterations? """ zr = 0.0 zi = 0.0 for i in 0...it # z <- z^2 + c zr,zi = zr*zr - zi*zi + cr, 2*zr*zi + ci if zr*zr + zi*zi > 4 return true end end return false end def toChar(p) if p return " " else return "X" end end def mandel(xmin,xmax,xstep, ymin,ymax,ystep, iterations) for yc in 1...ystep y = yc*(ymax-ymin)/ystep + ymin row = Array.new for xc in 0...xstep x = xc*(xmax-xmin)/xstep + xmin row.push escapes(x, y, iterations) end puts row.collect{|c| toChar(c)}.join end end mandel(-2.0, 1.0, ARGV[0].to_i, -1.0, 1.0, ARGV[1].to_i, ARGV[2].to_i)