Tuesday, January 18, 2011

December to Remember

When Winter Break came around this year, I had a good two-and-a-half or three weeks to get my relax on.  True, I had some goals to be "productive" during my time off (like, for example, finishing Chapter 4 in my novel).  However, I spent the first week of break playing solitaire online.  So much for productivity!

Finally, I had enough of my time wasting.  I decided that if I wasn't going to be productive, then I would have something to show for all time I was wasting.  I decided to try once again to program some fractals.

Instead of looking for C code, this time I searched out Java.  I found an applet that looked OK and had source code available.  I downloaded the source code but found important parts of it missing.  On top of that, I couldn't get my Java Virtual Machine to compile it or run it.

Fortunately, I was on Winter Break: I had tons of time to work on these issues.  The problem with Java is that my browser was hogging all the Java resources.  So now I browse Java-free (no more internet solitaire!), but I can run my own programs in Java whenever I want.

The second problem was when I discovered this Fractal Applet.  Created by a former Computer Science professor named David Leberknight, the applet is both an excellent implementation of the Mandelbrot Set as well as a perfect example of object-oriented programming.  In addition to all this, David graciously allows others to use the program if they give credit.  David, I raise my glass to you: Thank you!!

David provides explicit instructions on his download page.  I followed his instructions, turning the applet into a console application.  The next thing I did was to add the Save button.  This was easier than it sounded on the instruction page, and has allowed me to save the images I create.  Without the Save functionality, I wouldn't be doing this blog because there wouldn't be any visuals to go along with my explanations.

Besides David's instructions on how to use his applet/application, the structure of his code made it simple for me to improve.  Actually, improve is not quite the right word here.  I added things to the program, so I "improved" it in the sense that I made it do more things.  But I certainly didn't improve it in the sense of correcting what was wrong, streamlining it, or somehow making it better.  I didn't make it better, I only made it bigger.  Whatever, the point here is that it has been fairly simple for me to add new fractal classes to the code because he implemented the original program so well.

The one thing that continues to amaze me about David's program is how much you can zoom in on a fractal.  I've played with a number of fractal programs in my time, but David's is the fastest, especially when it comes to mega-magnification.  For example, here is a fractal that has been magnified many times.


This image displays one of the features that defines a fractal: self-similarity.  This tiny Mandelbrot Set (or "mini-me") resembles the original M-set but is so small that it is not visible when viewing the original set.  The blue tendrils in this image also exhibit self-similarity.  In fact, this shot was taken out of a much larger field that had similar tendrils of varying sizes all around.  It is the features of David's code that allows this unique shot to be seen.

There are three features in David's code that contributed to the uniqueness of the above shot. I've already mentioned the ability it has to zoom into small portions of the fractal.  The code also has several color schemes available (this happens to be the default, but you will see some of the other colors available if you keep reading my posts).  In addition, the program allows you to specify the number of iterations.

See, the Mandelbrot Set is an example of an "Escape Time" fractal.  What the computer does for each pixel is computes z = z^2 + c (that is, the new number is the square of the old number, plus the original pixel we started with).  This is all done on the complex plane.  Each time the value is calculated is called an "iteration."  After each iteration, the number is checked; if it is too large, then the calculations are stopped and the pixel is given a color, based on how quickly the number grew too large.  That's the "escape" part of the computational algorithm.

If the pixel never gets too large -- it never "escapes to infinity" -- then it remains black.  But the question is, how many times do you iterate before you give up and call it good?  This is the "time" part.  Without setting some upper bound (or limit) to the number of iterations, you would have an infinite loop -- worse, you would have no picture to look at.  Fractal artists have found there is a balance between the amount of detail your picture has and the time it takes to create the picture.

For example, here's the Mandelbrot Set at various iterations.
 This is 1 iteration.  It's just a circle of radius 3.  (That's the escape value I use.)
 This is 5 iterations.  It's starting to resemble the Mandelbrot shape.
 Here's 10 iterations.  A lot of definition is starting to become visible.
This is the fully formed Mandelbrot Set, at 33 iterations.  The red color, as well as the fine details, are both due to the additional iterations.

Lately, I have experimented with different iterations.  Initially, I started doing this to vary the colors in the picture, but I found that in certain detailed images, the image itself can vary widely.  For example, here's an animated gif I made from a tiny feature with iterations ranging from 250 to 600.  (You will probably have to click on the image to see the animation.  It's also kind of slowly animated, but it's well worth waiting for.)
I think what's happening here is that as the iterations increase, more and more pixels are "escaping" and fewer remain black.

So David's code allows one to explore in great detail the landscapes within the Mandelbrot Set.  The main reason I adopted and adapted David's code was so I could explore other fractals.  These other fractals are similar to Mandelbrot but have different equations.  Stay tuned as we explore some of these other equations.

3 comments:

  1. Does the code implement an infinite precision math library like Fractint or is it limited to Java's numerical representation? This becomes a major issue at deeper zoom levels.

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Hi MandelScott, I like what you have done to my old code. Please email me at MyFirstNameMyLastName @ gmail.com THANKS. David L.

    ReplyDelete