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 Set

Mandelbrot Set
Needs a lot of work on optimizing for speed...
Arrow keys to move the center
Page Up/Down to Zoom
Shift + Page Up/Down to change number of iterations
Numpad 2 Anti-alias render (also multiplies # of iterations by 10)
Numpad 1 Normal render
Numpad 0 Fast render
+ Make screen larger
- Make Screen smaller
Get Adobe Flash player
by runouw 22 Mar 2009
// Mandelbrot Set
// Needs a lot of work on optimizing for speed...
// Arrow keys to move the center
// Page Up/Down to Zoom
// Shift + Page Up/Down to change number of iterations
// Numpad 2 Anti-alias render (also multiplies # of iterations by 10)
// Numpad 1 Normal render
// Numpad 0 Fast render
// + Make screen larger
// - Make Screen smaller
package {
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.utils.ByteArray;
	import flash.ui.*;
	import flash.utils.*;
	import flash.geom.Rectangle;
	import flash.display.*;
	import flash.events.*;
	import flash.display.Stage;
	public class Mandelbrot extends MovieClip {
                private var RenderSize:Number = 2;
		public function Mandelbrot() {
			stage.scaleMode = StageScaleMode.NO_SCALE;
			StartGame();
			Draw();
		}
                private var initialized:Boolean = false;
		private var keysDown:Object = new Object();
                private function isDown(keyCode:uint):Boolean {
			return Boolean(keyCode in keysDown);
		}
		private function keyPressed(event:KeyboardEvent):void {
			keysDown[event.keyCode] = true;
		}
		private function keyReleased(event:KeyboardEvent):void {
			if (event.keyCode in keysDown) {
				delete keysDown[event.keyCode];
			}
		}
		private function clearKeys(event:Event):void {
			keysDown = new Object();
		}
		private function StartGame():void {
			addChild(bm1);
			addEventListener(Event.ENTER_FRAME,enterFrameHandler);

                        stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
			stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
			stage.addEventListener(Event.DEACTIVATE, clearKeys);
		}
		private var MouseDown:Boolean=false;
		private var MouseDownX:Number=0;
		private var MouseDownY:Number=0;
		private var centerX:Number=0;
		private var centerY:Number=0;
		private var centerZoom:Number=100;
		private var Loops:uint=150;
		private var timer:uint=0;
		private var timer2:uint=0;
		private var timer3:uint=0;
		private var oldStageW=stage.stageWidth;
		private var oldStageH=stage.stageHeight;
		private var startStageW=stage.stageWidth;
		private var startStageH=stage.stageHeight;
		//
		private var bmd1:BitmapData=new BitmapData(stage.stageWidth,stage.stageHeight,true,0xFF000000);
		private var rect:Rectangle=new Rectangle(0,0,stage.stageWidth,stage.stageHeight);
		private var bytes:ByteArray=bmd1.getPixels(rect);
		private var color:uint=0xFFFFFF;
		private var colorArray:Array=[];
		private var bm1:Bitmap=new Bitmap(bmd1);
		private var detailswitch:uint=0;
		private var currentX:uint=0;
		private var currentY:uint=0;
		private var C1:Number=0;
		private var C2:Number=0;
		private function enterFrameHandler(event:Event):void {
			if (isDown(Keyboard.LEFT)) {
				if (isDown(Keyboard.SHIFT)) {
					centerX-=0.1;
					timer2=0;
				} else {
					centerX-=5;
					timer2=0;
				}
			}
			if (isDown(Keyboard.RIGHT)) {
				if (isDown(Keyboard.SHIFT)) {
					centerX+=0.1;
					timer2=0;
				} else {
					centerX+=5;
					timer2=0;
				}
			}
			if (isDown(Keyboard.UP)) {
				centerY-=5;
				timer2=0;
			}
			if (isDown(Keyboard.DOWN)) {
				centerY+=5;
				timer2=0;
			}
			if (isDown(Keyboard.PAGE_UP)) {
				if (isDown(Keyboard.SHIFT)) {
					timer2=Math.min(timer2,8);
					Loops++;
				} else {
					timer2=0;
					centerZoom*=1.1;
					centerX*=1.1;
					centerY*=1.1;
				}
			}
			if (isDown(Keyboard.PAGE_DOWN)) {
				if (isDown(Keyboard.SHIFT)) {
					timer2=Math.min(timer2,8);
					Loops--;
				} else {
					timer2=0;
					centerZoom/=1.1;
					centerX/=1.1;
					centerY/=1.1;
				}
			}
			if (isDown(Keyboard.SPACE)) {
				trace(centerX);
				trace(centerY);
				trace(centerZoom);
			}
			if (isDown(Keyboard.NUMPAD_0)) {
				detailswitch=0;
				timer2=0;
			}
			if (isDown(Keyboard.NUMPAD_1)) {
				detailswitch=1;
				timer2=0;
			}
			if (isDown(Keyboard.NUMPAD_2)) {
				detailswitch=2;
				timer2=0;
			}
                        if(isDown(187)){
                            for (var X:int=0; X<stage.stageWidth; X+=1) {
	                        for (var Y:int=0; Y<stage.stageHeight; Y+=1) {
	                        	color=uint(0<<16|0<<8|0);
		                        bmd1.setPixel(X,Y,color);
	                        }
                            }
			    timer2=0;
                            RenderSize -= 0.2;
                            //
                            centerZoom*=1.1;
			    centerX*=1.1;
			    centerY*=1.1;
                        }
                        if(isDown(189)){
                            for (var X:int=0; X<stage.stageWidth; X+=1) {
	                        for (var Y:int=0; Y<stage.stageHeight; Y+=1) {
	                        	color=uint(0<<16|0<<8|0);
		                        bmd1.setPixel(X,Y,color);
	                        }
                            }
			    timer2=0;
                            RenderSize += 0.2;
                            //
                            centerZoom/=1.1;
			    centerX/=1.1;
			    centerY/=1.1;
                        }
                        Draw();
                        timer2=Math.min(timer2,8);
			
		}
		public function Draw() {
			var X:uint=0;
			var Y:uint=0;
			/*
			timer++;
			if(timer > 1){
			timer2++
			if(timer2 > 1){
			timer2 = 0
			}
			timer = 0;
			}
			*/
			//trace(timer+"-"+timer2);
			if (oldStageW!==stage.stageWidth||oldStageH!==stage.stageHeight) {
				timer2=0;

				removeChild(bm1);
				bmd1=new BitmapData(stage.stageWidth,stage.stageHeight,true,0xFFFFFFFF);
				rect=new Rectangle(0,0,stage.stageWidth/2,stage.stageHeight/2);

				bytes=bmd1.getPixels(rect);
				bm1=new Bitmap(bmd1);
				timer2=0;
				addChild(bm1);
				bm1.x = (startStageW-stage.stageWidth)/2;
				bm1.y = (startStageH-stage.stageHeight)/2;
			}
			oldStageW=stage.stageWidth;
			oldStageH=stage.stageHeight;
			if (detailswitch==0) {
				if (timer2<18) {
                                        // stage.stageWidth
                                        // stage.stageHeight
					for (X=int(stage.stageWidth/2-(stage.stageWidth/2)/RenderSize); X<stage.stageWidth/2+(stage.stageWidth/2)/RenderSize; X+=1) {
						for (Y=int(stage.stageHeight/2-(stage.stageHeight/2)/RenderSize); Y<stage.stageHeight/2+(stage.stageHeight/2)/RenderSize; Y+=1) {
							if ((X+Y+timer)%4 == 0 || (X == 0|| Y == 0)) {
								colorArray = getpixel((X+centerX-stage.stageWidth/2)/centerZoom,(Y+centerY-stage.stageHeight/2)/centerZoom);
								color=uint(Number(colorArray[0])<<16|Number(colorArray[1])<<8|Number(colorArray[2]));
							}
							//color = 0xFF000000+ 0xFFFFFF*getpixel((X+centerX-100)/centerZoom,(Y+centerY-100)/centerZoom);

							if ((X+Y+timer) %4 == 0) {
								bmd1.setPixel(X, Y, color);
							} else {

								if (timer2+(timer %2) <4) {
								    bmd1.setPixel(X, Y, color);
								}
							}
							//timer++;

						}
					}
				}
				timer+=1;
				timer2++;
			} else if (detailswitch == 1) {
				timer=0;
				if (timer2==0) {
					//
					for (X=int(stage.stageWidth/2-(stage.stageWidth/2)/RenderSize); X<stage.stageWidth/2+(stage.stageWidth/2)/RenderSize; X+=1) {
					    for (Y=int(stage.stageHeight/2-(stage.stageHeight/2)/RenderSize); Y<stage.stageHeight/2+(stage.stageHeight/2)/RenderSize; Y+=1) {
							colorArray = getpixel((X+centerX-stage.stageWidth/2)/centerZoom,(Y+centerY-stage.stageHeight/2)/centerZoom);
							color=uint(Number(colorArray[0])<<16|Number(colorArray[1])<<8|Number(colorArray[2]));
							bmd1.setPixel(X, Y, color);
						}
					}
				}
				timer2++;
			} else {
				timer=0;
				if (timer2==0) {

					currentX=int(stage.stageWidth/2-(stage.stageWidth/2)/RenderSize);
					currentY=int(stage.stageHeight/2-(stage.stageHeight/2)/RenderSize);
					/*
					for (X=0; X<stage.stageWidth; X+=1) {
					for (Y=0; Y<stage.stageHeight; Y+=1) {
					bmd1.setPixel(X, Y, 0xFF000000);
					}
					}
					*/
				}
				timer2++;
				X=currentX;
				Y=currentY;
				//
				timer3=getTimer();
				while (currentX<stage.stageWidth/2+(stage.stageWidth/2)/RenderSize) {
					currentY=int(stage.stageHeight/2-(stage.stageHeight/2)/RenderSize);
					while (currentY<stage.stageHeight/2+(stage.stageHeight/2)/RenderSize) {
						colorArray = getpixel((X+centerX-stage.stageWidth/2)/centerZoom,(Y+centerY-stage.stageHeight/2)/centerZoom);
						var colorArray1 = getpixel((X+0.5+centerX-stage.stageWidth/2)/centerZoom,(Y+centerY-stage.stageHeight/2)/centerZoom);
						var colorArray2 = getpixel((X-0.5+centerX-stage.stageWidth/2)/centerZoom,(Y+centerY-stage.stageHeight/2)/centerZoom);
						var colorArray3 = getpixel((X+centerX-stage.stageWidth/2)/centerZoom,(Y+0.5+centerY-stage.stageHeight/2)/centerZoom);
						var colorArray4 = getpixel((X+centerX-stage.stageWidth/2)/centerZoom,(Y-0.5+centerY-stage.stageHeight/2)/centerZoom);
						color = uint(Number((colorArray[0]+colorArray1[0]+colorArray2[0]+colorArray3[0]+colorArray4[0])/5) << 16 | Number((colorArray[1]+colorArray1[1]+colorArray2[1]+colorArray3[1]+colorArray4[1])/5) << 8 | Number((colorArray[2]+colorArray1[2]+colorArray2[2]+colorArray3[2]+colorArray4[2])/5));
						bmd1.setPixel(X, Y, color);
						Y=++currentY;

					}
					X=++currentX;
					if (getTimer()-timer3>60) {
						break;
					}
				}
			}
		}
		public function getpixel(X:Number, Y:Number) {

			// Z = C+Z*Z;

			var ar:Number = 0;

			//var ai:Number = 0;

			var e:Number = 0;

			var f:Number = 0;

			var g:Number = 0;

			var h:uint = 64;

			var Loops2:uint = 0;

			var z:uint = 0;

			var Return:Boolean = false;

			//var oldg:Number = 0;

		if ((X+1)*(X+1)+(Y)*(Y) < 0.06 || (X+1.31)*(X+1.31)+(Y)*(Y) < 0.003 || (1.3*X+0.3)*(1.3*X+0.3)+(Y)*(Y) < 0.38) {

		

				return [0,0,0];

		} else {

				//var i:uint = 0;

				//while(i<Loops){

				if (detailswitch == 2) {

					Loops2 = Loops*10;

				} else {

					Loops2 = Loops;

				}

				for (var i:uint=0; i<Loops2; i++) {

					e = X+ar*ar-f*f;

					f = Y+2*ar*f;

					//e = X+ar*ar-Math.sin(f*f);

					

					//e = ar*ar*ar-3*(ar*f*f)-X;

					//f = Y+f*3*(ar*ar-f*f);

					ar = e;

					//ai = f;



					//g = Math.pow(e,2)+Math.pow(f,2)

					// if(e>h)

					//if (e*e+f*f>h) {

					//oldg = g

					g = e*e + f*f

					if (g>=h) {

						var n = i+1-Math.log(g)/Math.log(h);



						//var brightness = 255-(255/(i+1-Math.log(Math.abs(g))/Math.log(5)));

						//brightness = Math.min(brightness,255)

						//brightness = Math.max(brightness,0)

						//var red:Number = 128+Math.sin(n/5)*128;

						//var blue:Number = 128+Math.sin(n/5+Math.PI/3)*128;

						//var green:Number = 128+Math.sin(n/5+2*Math.PI/3)*128;

						n *= 0.05;

						var red:Number = 128+Math.sin(n+4.5)*128;

						var blue:Number = 128+Math.sin(n+4.5+Math.PI/2)*128;

						var green:Number = 128+Math.sin(n+4.5+Math.PI)*128;



						//red = Saw(n/16 + (3/3)*2)*255

						//blue = Saw(n/16 + (1/3)*2)*255

						//green = Saw(n/16 + (2/3)*2)*255

						





						return [red,blue,green];

						//return brightness

						Return = true;

						break;

					}

				}

				// (r << 16 | g << 8 | b);

				if (Return == false) {

					//var n = Math.pow(g,0.25)+1-Math.log(Math.pow(g,0.25))/Math.log(h);

					//var red1:Number = 64+Math.sin(n*10+4.5)*64;

					//var blue1:Number = 64+Math.sin(n*10+4.5+Math.PI/2)*64;

					//var green1:Number = 64+Math.sin(n*10+4.5+Math.PI)*64;

					//return [red1,blue1,green1];

					return [0,0,0];

				}

			}

		}
	}
}