<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" horizontalScrollPolicy="off" verticalScrollPolicy="off"
                creationComplete="init()" backgroundColor="0" frameRate="120" color="0xaaaaaa" viewSourceURL="srcview/index.html">

    <mx:Script>
        <![CDATA[
            import com.derschmale.bobs.FadingBob;
            
            // an approximation to an infinitisimal time difference
            private const TIME_STEP : Number = 0.005;
            
            private const BOB_SIZE : Number = 4;
            
            // just some random initialization values
            private var _x : Number = 5;
            private var _y : Number = 3;
            private var _z : Number = 2;
            
            // variables influencing the attractor
            private var _sigma : Number = 10;
            private var _rho : Number = 28;
            private var _beta : Number = 8/3;
            
            private var _count : uint = 0;
            
            private function init() : void
            {
                this.addEventListener(Event.ENTER_FRAME, handleEnterFrame);
            }
            
            // creates a new sprite on the next location
            private function nextBob() : void
            {
                var sprite : Sprite;
                var dx : Number, dy : Number, dz : Number;
                var xOld : Number = _x, yOld : Number = _y;
                var r : uint, g : uint, b : uint;
                
                // calculate the delta in position relative to the old position over a time period of TIME_STEP
                // this is essentially an approximation, more correct for lower values of TIME_STEP
                dx = _sigma*(_y - _x)*TIME_STEP;
                dy = (_x*(_rho-_z)-_y)*TIME_STEP;
                dz = (_x*_y-_beta*_z)*TIME_STEP;
                
                // apply the delta
                _x += dx;
                _y += dy;
                _z += dz;
                
                // generate some fancy colour
                r = (0.5+Math.sin(_count*0.00555)*.5)*0xd4;
                g = (0.5+Math.cos(_count*0.00123)*.5)*0xa5;
                b = (0.5+Math.cos(_count*0.00953)*.5)*0x35;
                
                // create blob and position
                sprite = new FadingBob((r<<16)+(g<<8)+b, BOB_SIZE);
                sprite.x = _x*7;
                sprite.y = _y*7;
                target.addChild(sprite);
            }
            
            // draw several new bobs at once
            private function handleEnterFrame(event : Event) : void
            {
                nextBob();
                nextBob();
                nextBob();
                nextBob();
                nextBob();
                nextBob();
            }
        ]]>
    </mx:Script>
    <mx:UIComponent id="target" horizontalCenter="0" verticalCenter="0" />
    <mx:VBox paddingTop="20">
        <mx:HBox>
            <mx:Label text="Beta:" textAlign="right" width="100" />
            <mx:TextInput id="betaValue" restrict="0123456789." text="{2.667}" change="_beta=Number(betaValue.text);" maxChars="5" />
        </mx:HBox>
        <mx:HBox>
            <mx:Label text="Rho:" textAlign="right" width="100" />
            <mx:TextInput id="rhoValue" restrict="0123456789." text="{28}" change="_rho=Number(rhoValue.text);" />
        </mx:HBox>
        <mx:HBox>
            <mx:Label text="Sigma:" textAlign="right" width="100" />
            <mx:TextInput id="sigmaValue" restrict="0123456789." text="{10}" change="_sigma=Number(sigmaValue.text);" />
        </mx:HBox>
    </mx:VBox>
</mx:Application>