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

Fractal Tree

Get Adobe Flash player
by RobotCaleb 26 Sep 2009
/**
 * Copyright RobotCaleb ( http://wonderfl.net/user/RobotCaleb )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/6m8f
 */

package
{
	import flash.display.Sprite;
	import flash.display.StageScaleMode;

	[SWF(width="400", height="400", backgroundColor="#d1d1d1")]
	public class Fractal extends Sprite
	{
                // Don't increase this number any higher than 6!
                private const MAXSTEPS:Number = 4;
                private const angleMult:Number = 0.4;
                private const lengthMult:Number = 0.45;
                
		public function Fractal()
		{
			stage.scaleMode = StageScaleMode.NO_SCALE;
			DrawFractal();
		}
	
		public function DrawFractal():void
		{
                        var angle:Number = Math.PI / 180 * 90;
			DrawPiece(angle, angle * angleMult, 200, 400, 200, 0);
		}
		
		public function DrawPiece(angle:Number, angleMod:Number, x:Number, y:Number, length:Number, count:Number):void
		{
			if (count < MAXSTEPS)
			{
				var newLength:Number = length * lengthMult;
                                var newX:Number = x - Math.cos(angle) * length;
				var newY:Number = y - Math.sin(angle) * length;
				
			        graphics.lineStyle((MAXSTEPS - count), 0xff000000);
				graphics.moveTo(x, y);
				graphics.lineTo(newX, newY);
				
				DrawPiece(angle + angleMod * 2, angleMod * angleMult, newX, newY, newLength, count + 1);
				DrawPiece(angle - angleMod * 2, angleMod * angleMult, newX, newY, newLength, count + 1);
				DrawPiece(angle + angleMod, angleMod * angleMult, newX, newY, newLength, count + 1);
				DrawPiece(angle - angleMod, angleMod * angleMult, newX, newY, newLength, count + 1);
				DrawPiece(angle, angleMod * angleMult, newX, newY, newLength, count + 1);
			}
		}
	}
}