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

Mandelbrot fractal

Get Adobe Flash player
by splashdust 10 Nov 2009
/**
 * Copyright splashdust ( http://wonderfl.net/user/splashdust )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/h22p
 */

package {
    import flash.display.Sprite;
    import flash.events.TimerEvent;
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.utils.Timer;
    import flash.text.TextField;
    
    [SWF(width="320", height="240", frameRate="60")]
    
    public class FlashTest extends Sprite {
        public static var width:int = 320;
        public static var height:int = 320;
        public function FlashTest() {
           var setBitmap:Bitmap = new Bitmap();
            var bitmapData:BitmapData = new BitmapData( stage.stageWidth , stage.stageHeight , false , 0x000000 );
            setBitmap.bitmapData = bitmapData;

            addChild( setBitmap );
            var maxIterations:int = 128;
            var pixels:Array = new Array();

            var beforeTime:int = flash.utils.getTimer();
            var xtemp:Number = 0;
            var x0:Number = 0;
            var y0:Number = 0;
            var iteration:int = 128;
            
            for(var ix:int=0; ix<width; ix++) {
	            pixels[ix] = new Array();
	            for(var iy:int=0; iy<height; iy++) {
			
			x0 = 0;
			y0 = 0;
			iteration = 128;
			
			while ( x0*x0 + y0*y0 <= 4  &&  iteration > 0 ) 
  			{
				xtemp = x0*x0 - y0*y0 + (ix-14*5000)/50000;
				y0 = 2*x0*y0 + (iy-(height/0.6))/50000;
				x0 = xtemp;
				
				iteration--;
			}
			
			pixels[ix][iy] = iteration;
		}
            }

            var afterTime = flash.utils.getTimer();

            var tf = new TextField();
            tf.width = 400;
            tf.text = "Generating fractal took "+(afterTime-beforeTime)+" ms";
            addChild(tf);

            var fps = new TextField();
            fps.width = 400;
            fps.y = 10;
            fps.text = "FPS: ";
            addChild(fps);

            var colorModifier:int = 2;
            var timer:Timer = new Timer(10);
            var pixel;
            timer.addEventListener(TimerEvent.TIMER, function(){
	    var r:int=0, b:int=0, g:int=0;
	
	    var beforeTime:int = flash.utils.getTimer();
	    for(ix=0; ix<stage.stageWidth; ix++) {
		for(iy=0; iy<stage.stageHeight; iy++) {
				pixel = pixels[ix][iy];
				r = pixel + colorModifier;
				g = pixel + colorModifier + r;
				b = pixel + colorModifier + g;
				
				bitmapData.setPixel(ix, iy, (r<<16 | g<<8 | b));
			}
	}
	var afterTime = flash.utils.getTimer();
	
	fps.text = "FPS: "+Math.round(1000/(afterTime-beforeTime));
	
	colorModifier += 2;
	if(colorModifier > 65530)
			colorModifier = 0;
});
timer.start();
            
        }
    }
}