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

Chaos Game

じわじわ
Get Adobe Flash player
by Susisu 08 Feb 2011
package {
    import flash.ui.Keyboard;
    import flash.events.KeyboardEvent;
    import flash.text.TextField;
    import flash.text.TextFieldType;
    import flash.events.Event;
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Sprite;
    import flash.geom.Point;
    public class FlashTest extends Sprite {
        private var points:Vector.<Point>;
        private var n:int=3;
        private const dx:Number=0.5;
        private const dy:Number=0.5;
        private var bmd:BitmapData;
        private var bmp:Bitmap;
        private var now:Point;
        private var tf:TextField;
        public function FlashTest() {
            init();
            addEventListener(Event.ENTER_FRAME,enterFrame);
            addChild(bmp);
            tf=new TextField();
            tf.background=true;
            tf.type=TextFieldType.INPUT;
            tf.height=16;
            tf.width=24;
            tf.text=n.toString();
            addChild(tf);
            tf.addEventListener(KeyboardEvent.KEY_DOWN,keyDown);
        }
        private function init():void{
            points=new Vector.<Point>();
            for(var i:int=0;i<n;i++){
                points.push(new Point(200*Math.cos(2*Math.PI/n*i)+465/2,200*Math.sin(2*Math.PI/n*i)+465/2));
            }
            if(bmd!=null)bmd.fillRect(bmd.rect,0x0);
            else bmd=new BitmapData(465,465,false,0);
            bmp=new Bitmap(bmd);
            now=new Point(Math.random()*465,Math.random()*465);
        }
        private function keyDown(e:KeyboardEvent):void{
             if(e.charCode==Keyboard.ENTER){
                n=parseInt(tf.text);
                if(!isNaN(n)&&n>0)init();
             }
        }

        private function enterFrame(e:Event):void{
            for(var i:int=0;i<10000;i++){
                var c:uint=(1+(bmd.getPixel(now.x,now.y)&0xff));
                c=c>0xff?0xff:c;
                bmd.setPixel32(now.x,now.y,(c<<16)|(c<<8)|c);
                var t:int=Math.random()*n>>0;
                now.x=(now.x+points[t].x)*dx;
                now.y=(now.y+points[t].y)*dy;
            }
        }

    }
}