March 2008 Archives
I first got hooked on programming when my dad got us a copy of QuickBasic for our family's Macintosh Plus. Later in high school, I graduated to coding in ThinkPascal on Mac LCs. And one of the first moments when I surprised myself at what I could do with these machines was when I completed my first render of the Mandelbrot set. It took me a week to figure out how to render pixels to screen (we were stuck on console programs at the time in class) and another week to figure out an algorithm that would actually do the calculations correctly.
So I took some time this morning to stroll down memory lane and see how far my programming skills had progressed since then. I decided I would code up a little Mandelbrot set plotter using Windows Forms. It took me about two hours of Googling and Wikipediaing to get here:
This was already further than I ever got in high school--we didn't even have color screens back then.
I realized that this was pretty dull without a zoom feature, so I implemented a little zoom box that you can set by clicking and dragging on the fractal. Then I realized I have a dual-core laptop, so why not parallelize the code a bit? Fractals are perfect for this sort of thing--there aren't any data dependencies so synchronization isn't much of an issue. I moved the actual fractal calculation code to a thread and parametrized the coordinate window it would draw so I could render it in two threads (one for the left half, one for the right). I could subdivide the coordinate window as much as a I wanted depending on the number of cores on the machine, but that's a project for another day. Also, I set pixels directly in a Bitmap object in the calculation thread, so there actually is a little bit of locking there that could probably be optimized away (because we all know how expensive a lock is, right?)
But I digress. My point is, this is what's so fun about a project like this. It helps to keep your love of programming alive when your day job is burning you out. I could optimize and tweak this thing for weeks. Why not implement the calculations as integer math and see if it's any better than the default floating point code? What about checking for periodicity and bailing out early? How about customizable color maps? Different fractals? Real-time zooming? How do we provide progress feedback to the user on a long render using multiple threads? The options are endless and I probably just lost my whole weekend to this thing. Sounds good to me.