Robots w/Lasers

August 22, 2008

Simple Cellular Automata Example in AS3

To get a little more experience with ActionScript 3, I wrote up a very simple class which will take a rule number, and then generate a cellular automata based on that.

The movie below requires Flash 9 or above to be installed. Some rules to try out: 90, 30, 73, 110, 150. Valid numbers are 0-255, but not all produce interesting results.

This movie requires a newer Flash Player version

Direct link to flash movie (got some people complaining the SWFObject script doesn't work under Vista)

See Wikipedia for more info, and Wolfram.com has a list of all 256 possible results
Here's the code:

Actionscript:

  1. /**
  2. * Simple Cellular Automata example
  3. * by David Rorex, 2008
  4. *//// Holds the data we draw onto
  5. /// Bitmap container for displaying our rendering
  6. /// Text field for typing in rule numbers
  7. /// Button to click to render
  8. /// Constructor
  9. // set up the input textfield
  10. "90"// set up the render button
  11. "Render"// set the stage to not scale, and to get an event if the window changes size
  12. // initialize the size
  13. /**
  14.          * Event handler for when the stage changes size
  15.          * @param e
  16.          *//**
  17.          * Event handler to catch the user pressing ENTER in the text field
  18.          * @param e
  19.          *//**
  20.          * Simple method to call the render function
  21.          *//**
  22.          * Adjust the positioning of our elements based on a given size
  23.          * @param w
  24.          * @param h
  25.          *//**
  26.          * Render cellular automata.
  27.          * @param ruleNum - the number of the rule to draw. Some interesting numbers are '30' and '90'
  28.          */// clean up previous bitmap
  29. // get width and height of the stage
  30. // build lookup table based on the rule number
  31. // create bitmap data that we will draw into
  32. // lock the bitmap data, provides speedups when you are doing a lot of manipulations
  33. // set the starting data - a single pixel in the center
  34. // loop row by row
  35. // loop pixel by pixel
  36. // use lookup table based up 3 neighboring pixels to calculate new pixel
  37. // map the pixels, 0-white, 1-black
  38. // unlock the bitmap data
  39.            
  40.             // create the bitmap to hold the data
  41. // add it to our display list so we can see it
  42.         }
  43.     }
  44. }

Filed under: Flash,Software — davr @ 11:33 am

3 Comments »

  1. looks like your flash detection is broken for FP10/FF/vista

    Comment by felix — August 22, 2008 @ 12:40 pm

  2. It works for me with FP10/FF/XP…dunno why vista would would make a difference. It’s using swfobject which is supposed to be pretty good about that sort of thing. I can’t be bothered to debug a flash detection script for vista, so I added a direct link to the swf in the post.

    Comment by davr — August 22, 2008 @ 1:03 pm

  3. […] public links >> positioning Simple Cellular Automata Example in AS3 Saved by RiN on Wed 22-10-2008 Advising Agencies That Get Positioning Saved by andrewwilles on […]

    Pingback by Recent Links Tagged With "positioning" - JabberTags — October 23, 2008 @ 5:04 am

RSS feed for comments on this post. TrackBack URI

Leave a comment

Powered by WordPress

Bad Behavior has blocked 3822 access attempts in the last 7 days.