Fun with Math
September 17th, 2008

Over the weekend, I had some free time, so I decided to try to create some images of the Mandelbrot Set using PHP. To render the image, I had my script generate an HTML table with 1 pixel by 1 pixel cells with the appropriate background color. I ran into two problems doing this. One was that even with a 200 x 200 image, the page was over 2 MB. The other was that I wasn’t getting the right answer.

For reference, I was using this page which does a pretty good job of explaining things. Basically, the Mandelbrot set is a set of complex numbers. The pretty pictures that you’ve probably seen, are plots of this set where members of the set are colored black and points outside the set are either colored white, or some other color (more on that later). The test to see if a point is in the Mandelbrot Set iterates the equation z = z^2 + c, where z starts out at zero and c is the complex number that you are testing. If in this process, the magnitude of z becomes greater than 2, it will continue on to infinity. If it does this then it is not part of the Mandelbrot Set. The more colorful plots use the number of iterations necessary for the magnitude of z to become greater than 2 to color these points. If instead the magnitude of z stays below 2, it is inside the Set, and is colored black.

The code necessary to perform the check isn’t more than a few lines, so I was confused as to how I could have gotten it wrong, until I found this page. Their math is the same, but their code was different from what I had. For some reason, and I can only assume that it is some precision issue, they do each different math operation on a separate line. Once I did this in my code, it worked.

To resolve the image size issue, I did some searching and eventually found an easy to use C++ library (I gave up on PHP for the time being) for creating bitmaps, appropriately titled EasyBMP. I was then able to create some multi-megapixel images that took up about 40 MB. Crunching them down to JPG got them to about 1 MB. In this picture I used a gray scale coloring for points outside the Set:

Since then I’ve been messing around with ways to color the image. In the following image I used the number of iterations to determine the hue and just set the saturation and brightness to a constant:

I’m not entirely satisfied with the results, nor am I entirely sure I got the HSV to RGB conversion right. I’m going to have to mess around with it some.

[Joe]

Tagged as


add a comment permalink




Games that Physicists Like to Play
September 16th, 2008

The professor for my statistical field theory course started class today with a little puzzle: Imagine a game where you have 2N coins (thus an even number of coins) laid out in a line. The coins are of various denominations (e.g. quarters, dimes, nickels, pennies). You and the other player take turns picking up coins from either end of this line. The object of the game is to end up with more money than the other player, and it’s a tie if you end up with the same amount. You go first, how do you guarantee that you never lose?

[Joe]

Tagged as , ,


11 comments permalink