2007-11-16

ASCII mandelbrot in Python

I was reading Miguel de Icaza's (of Mono fame) blog entry about Luke Hoban's ray tracer one-line in C# 3.0 and LINQ. As Miguel said in the post, "I write the factorial function, Luke writes Ray Tracers". Rather cool.

So I checked out Luke's blog. The latest entry has some code in F# to generate an ASCII mandelbrot. Another cool example, I thought. Shouldn't be hard to reproduce in Python, right?


class ExhaustedException(Exception):
pass

max_iterations = 100

def mod_squared(num):
return num.real * num.real + num.imag * num.imag

def mandelbrot(outer):
def mandelbrot_inner(inner, iterations=0):
if mod_squared(inner) >= 4.0:
return iterations
elif iterations == max_iterations:
raise ExhaustedException
else:
return mandelbrot_inner(inner * inner + outer, iterations+1)
return mandelbrot_inner(outer)
y = -1.0
while y <= 1.0:
x = -2.0
while x <= 1.0:
try:
mandelbrot(complex(x, y))
print " ",
except ExhaustedException:
print "#",
x += 0.05
else:
y += 0.1
print


Leads to the output of:


# # # #
# # # #
# # # # # # # # # # #
# # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # #
# # # # # # # # # # #
# # # #
# # # #




(Sorry about Blogger chopping this off; just run the code to see the full output)

You might notice that compared to Luke's output I am missing the floating # at the very top and very bottom. I don't know why; probably some stupid error on my part.

Anyway, the biggest differences between the two is how the loops work and breaking out of the mandelbrot function. Oh, and that complex numbers are built into Python. =) Otherwise I didn't try very hard at all to optimize it, make it more Pythonic, or shorter.