FireApplet - Project Description

Overview
This is a very simple applet that uses a diffusion-type cellular automata to simulate a burning flame. In order to minimize its computational impact on the host machine, the applet cuts a few corners. These areas are discussed below. Because this applet was originally an exercise in optimization, I believe the main loop is fairly compact (from a code point of view - I haven't yet evaluated how high-level Java code gets translated to Java byte-code).

The Algorithm
This algorithm is a specific instance of a more general matrix overlay method commonly used in image processing. This method works by superimposing a pattern over each pixel of the original image, performing some math on the pixels under the pattern, and storing the result in a new image. The pattern for this algorithm is shown at the right (where "C" indicates the current pixel). All pixels marked with a "*" are summed, the result divided by 4 (a mathematical average), a constant value is subtracted (to dim the flame), and the result stored in the original pixel location. A benefit of this method is that everything can be done in-place in the original image (this is not true in general since other pixels may depend on the original value of the current pixel). To compute the next generation, this pattern is applied to the current generation from left-to-right, top-to-bottom, and the new image drawn.
+---+---+---+
|   | C |   |
+---+---+---+
| * | * | * |
+---+---+---+
|   | * |   |
+---+---+---+
Areas of Interest
Conclusion
As this was originally an optimization problem, it taught me a lot about the way Java handles graphics and how to code faster Java routines. However, TANSTATFC (There Ain't No Such Thing As The Fastest Code - Michael Abrash, Zen of Code Optimization), so I'm sure there's plenty of room for improvement. That's what's neat about optimization - as good as you make it, there's always a way to make it even better. As time and interest permit, I may return to this applet in the future to see what else I can learn from it.


The applet is available.
The source code is available for educational purposes.