Archive for the 'cellular automata' Category

City From Fire (Cellular Automata)

Saturday, September 27th, 2008

This is another sketch dug up from my Cellular Automata folder. As I mentioned in my previous post about Cellular Automata - I like to base the rules on the color of the current pixel and its neighbors. This example looks like a city being built from fire:





Click either of the above images to view flash version

If you watch this for about 20 or 30 seconds nearly all the fire should disappear - leaving red pixels wandering through gray corridors.

Once you've got a basic template for this type of CA... you can just play with the code. After awhile you can even begin to predict some of the results and really control what your CA does. The following code snippet should help you get started... it creates 300 vertically wandering red pixels:

Actionscript:
  1. // current pixel
  2. var pix:uint;
  3.  
  4. var size:Number = 200;
  5. // read from the pixels BitmapData and write to the buffer BitmapData
  6. var pixels:BitmapData = new BitmapData(size,size, false, 0x000001);
  7. var buffer:BitmapData = new BitmapData(size,size, false, 0x000001);
  8.  
  9. var frame:Bitmap = new Bitmap(pixels);
  10. addChild(frame);
  11.  
  12. frame.scaleX= 2;
  13. frame.scaleY = 2;
  14.  
  15. // place 300 red pixels
  16. for (var i = 0; i<300; i++) {
  17.     buffer.setPixel(100 + Math.random()*40-20, 100 + Math.random()*40-20, 0xFF0000);
  18. }
  19.  
  20. addEventListener(Event.ENTER_FRAME, onLoop);
  21. function onLoop(evt:Event):void {
  22.     for (var i:int = 0; i<size; i++) {
  23.         for (var j:int = 0; j<size; j++) {
  24.             pix = pixels.getPixel(j, i);
  25.             // add additional logic here:
  26.             if (pix == 0xFF0000) {
  27.                   buffer.setPixel(j, i, 0x000001);
  28.                   buffer.setPixel(j, i+Math.random()*4-2, 0xFF0000);
  29.                   // and here
  30.             }
  31.         }
  32.     }
  33.     pixels.draw(buffer);
  34. }

Cellular Automata - Machine Hull

Tuesday, September 16th, 2008

I have a folder on my computer filled with strange variations of cellular automata. I've spent many hours playing with the life lexicon - but most of the cellular automata experiments I have in this folder are similar (at least in their simplicity) to your standard sand CA. I usually base the rules on the color of each pixel. For instance:

- scatter a few randomly colored pixels here and there
- if a pixel is not black
- calculate a slightly darker variation of the current pixels color
- surround the current pixel with this color

Here is a variation on that idea - it's a bit more complex than the above description, but the principal is the same:

Click above image to see flash version

It's a bit easier to understand what's happening here in the following example:


Click above image to see flash version

I'll be posting things from my Cellular Automata folder regularly...