Courses/CS 461/Winter 2006/Rick Strom/Week One
From CSWiki
Contents |
[edit] Week One
[edit] Cellular Automata
There are four examples here: Plasma Fractal, Smoke, Fire, and Water.
(all images can be viewed larger by clicking)
[edit] Example 1: Plasma Fractal
[edit] Screenshots
|
[edit] Algorithm
The plasma fractal is built using a recursive midpoint displacement algorithm.
The four corners of the window are colored randomly, then the rectangle is broken into four sub-rectangles. The midpoints of the edges of the parent rectangle are colored using the average color value of the endpoints of the edge segment. The center point (midpoint) of the parent rectangle is colored using the average of the four corners, plus a displacement value. If the displacement value is fixed, the effect is a patterned fractal. If it is random, the effect is more random.
In order to get a nice animation out of this, the displacement value is added to the color value of the midpoint at each iteration, and then the recursion is performed. When the color value exceeds 140, it is reset back to 0 and the process continues. This generates some really nice plasma effects.
[edit] Source Code
[edit] Applet
Plasma Applet (at rick-strom.com)
[edit] Example 2 & 3: Smoke and Fire
[edit] Screenshots
These are smoke and fire simulations based on an old 2D fire algorithm I remember from the early 90s. The smoke algorithm uses the grayscale pallette (NetLogo colors 0-10) and the fire algorithm is identical, but uses a custom pallette of reds, yellows and blues.
|
|
The fire simulation doesn't look as good as it should, because it really should have at least a 256 color pallette. In this example, I have a switch that allows either a 10 color pallette or a 100 color pallette. The difference is significant, but still pretty bad with only 100 colors.
[edit] Algorithm
Each patch has two values: heatvalue and next-heatvalue.
Initially, both values are set to 0 (cooled) for each patch, except for the bottom row, which is seeded with random values from 0 to [palletteSize]. On each iteration, the next-heatvalue is calculated by taking the average of the north, south, east and west cells' heatvalue. A cooling constant is also subtracted from this value, which causes the flame to reduce to smoke at the top of the view area -- the higher this value, the quicker the fire disappears.
When all next-heatvalues are calculated, each cell's heatvalue is assigned the value of next-heatvalue for the cell directly below it (which causes the flame to move upwards).
And finally, the bottom row is again seeded with random values.
[edit] Source Code
[edit] Applet
Smoke Applet (at rick-strom.com)
Fire Applet (at rick-strom.com)
[edit] Example 4: Water
This is the classic 2D water drop algorithm, also from the early 90s (or at least that's when I saw it first).
[edit] Screenshots
|
[edit] Algorithm
Each cell is assigned a height value, and keeps track of its previous two states. Initially, these are all set to 0 (lowest height).
If a drop is added to the pond, the height at the drop location switches to 7. Then, the next-height of each cell is calculated as follows:
- Sum the neighboring cells' height values and divide by 2
- Subtract the current cell's previous-height value from (1)
- Multiply (2) by a dampening factor
Once the next-height values are calculated, all cells are updated by setting previous-height to height, and height to next-height for each cell.
[edit] Source Code
[edit] Applet
Water Applet (at rick-strom.com)
[edit] Final Notes
NetLogo is pretty cool. I can definitely see it being a good little tool for testing out 2D graphics ideas like I just did -- and ABMs, of course. The language itself, though, is an abomination. Constructs like ask patches are just so awkward. Something like PHP's foreach (patches AS $key => $value) with a patch class, for example, would seem so much easier to work with. Also, whoever thought identifying object members by tacking on -of was a good idea must not have realized how difficult it would be to read the resulting code.
[edit] NetLogo Files
stromplasma.nlogo
stromsmoke.nlogo
stromfire.nlogo
stromwater.nlogo

