Old Type Rider

I am so turned on by Mandelbrot right now — SEXY UPDATE

February 20, 2008 · Leave a Comment

The Mandelbrot set is pretty dang cool, but I really had no idea just how cool until yesterday when I was playing with Python and wrote up the code for it after a few sips of the Guinness. It’s a simple exercise, even after a few drinks. I’m sure almost anyone who likes pretty things and knows how to program has done it at some point. (If they’re anything like me, then in several different languages.)

We’ve all seen the thing, it looks like this:

Mandelbrot set

(Click me to make me big!)

And this is what it is: {c ∈ C : zi+1 = zi2 + c converges is bounded}, where z0 = 0 and i = 0, 1, 2, 3, … But then I thought, who cares what’s in or out? What are these attractors that are sucking this recursion in? So I calculated a histogram of the complex plane, where each time zi fell in a bucket I added 1 to it, and this is what I got:

Mandelbrot attractors histogram

(No, click me to make me big!)

Fuller buckets are whiter and emptier buckets are darker. Buckets that are hit a lot light up very bright. I applied a logarithmic scale before plotting to bring out some of the dimmer details. I was left wanting more though, and decided that some anti-aliasing would really make this look awesome. And it does.

Mandelbrot attractos histogram Mandelbrot attractos histogram from PIL

(Click us and we’ll beat up that bully who always pushes you in the halls.)

I like how the first image looks really bright and brings out those faint ‘magnetic field’ lines on the right, but I also like how clean the second image looks. (By the by, the circle in the lefthand image is created by the bounding radius of my code.) But the question is, why are these pictures so different? Well, Python’s a funny creature. The image on the left was my first attempt which did using the PyGraphics library, which I became dissatisfied with it, and so I tried the Python Imaging Library (PIL). After realizing that PyGraphics just uses PIL I ended up hacking together another solution that uses both.

I like PyGraphics. It’s great for learning with, but I took two issues with it while playing with this:

1. I had no control over the quality of the output jpeg, and for that matter, I could only output a jpeg.
2. There’s no built-in anti-aliasing, and I didn’t want to really embark on writing up anything that complicated.

I ended up going into pictures.py in PIL and adding a line of code to increase the quality of the jpeg encoding. As for anti-aliasing, I wrote up a simple resampling method that just takes the average over a 3×3 square of 9 pixels and outputs a pixel with that value, but that didn’t do much for maintaining sharpness.

PIL has good anti-aliasing, so I was able to calculate a larger image and then resize it with their anti-aliasing filter, but if you look at the PIL version, there’s no wash of noise over it. The PyGraphics version looks like it cranks up the gain, like doing extreme curving in Photoshop.

I cannot find the reason for this. After some searching I figured out that PyGraphics uses PIL and that the calls I’m making to create images and draw are identical (as far as I can see). So I’m totally stumped at the drastic difference. I also tried to recreate the overexposed quality of the PyGraphics image with PIL so that I could take advantage of its anti-aliasing, but had zero success. Stumped.

However, I was able to hack together a solution with both packages that did all the drawing with PyGraphics and all the anti-aliasing with PIL. This is what I got:

Mandelbort attractors histogram

(You know what to do.)

You can see that we lose some of the external details but gain some details in the bright internal areas. I’m pretty sure those lines are caused by rounding errors. I did this one at a slightly lower resolution.

Here are a few more images with different iteration settings and a somewhat eerie looking zoom to the bottom left of the main circle.

PIL. No anti-aliasing.

(A PIL image without anti-aliasing.)

 

No more titles

(Check out those weird View-Masters on the left side of the big circle.)

 

mandel_attract_hist.jpg

(Oooooh…)

 

Mandelbrot attractor close-up

(Spooky.)

 

Same close up, different program

(Same close up. Different program.)

 

UPDATE: Now with high-resolution pictures here, here and here.

 

Categories: Coding · Math · Pretty things
Tagged: , , , ,

0 responses so far ↓

  • There are no comments yet...Kick things off by filling out the form below.

Leave a Comment