In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

Munching Square

c.f.  http://mathworld.wolfram.com/MunchingSquares.html
Get Adobe Flash player
by tai2 25 Apr 2013
/**
 * Copyright tai2 ( http://wonderfl.net/user/tai2 )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/ldgk
 */

package {
    import flash.display.Sprite;
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.events.Event;
    
    public class MunchingSquare extends Sprite {
        private var canvas:BitmapData;
        private var canvasBitmap:Bitmap;
        private var n:int = 1;
        private var d:int = 1;
        
        public function MunchingSquare() {
            canvas = new BitmapData(stage.stageWidth, stage.stageHeight, false, 0x000000);
            canvasBitmap = new Bitmap(canvas);       
            addChild(canvasBitmap);
            
            addEventListener(Event.ENTER_FRAME, enterFrame);
        }
        
        private function enterFrame(event:Event) : void {
            canvas.lock();
            var noplot:int = 0;
            var plot:int = 0;
            var x:int;
            var y:int;
            for (y = 0; y < stage.stageHeight; y++) {
                for (x = 0; x < stage.stageWidth; x++) {
                    if ((x^y) < n) {
                        var v:int = 1 < n ? 255 * (n - 1 - (x^y)) / (n - 1) : 0;
                        canvas.setPixel(x, y, (v<<16) | (v<<8) | (v<<0));
                        plot = 1
                    } else {
                        canvas.setPixel(x, y, 0x000000);
                        noplot = 1;
                    }
                }
            }
            canvas.unlock();
            
            n += d;
            if ((d == 1 && !noplot) || (d == -1) && !plot) {
                d = -d;
            }           
        }

    }
}