forked from: forked from: BrownianMotion
// forked from ton's forked from: BrownianMotion
// forked from ton's BrownianMotion
package {
import flash.display.*;
import flash.events.*;
import frocessing.core.F5BitmapData2D;
import net.hires.debug.Stats;
[SWF(frameRate="60", backgroundColor="#000000")]
public class BrownianMotion extends Sprite {
private const N:int = 500;
private const MAX_R:int = 50;
private const W:int = 465;
private const H:int = 465;
private var list:Vector.<Dot>;
private var fg:F5BitmapData2D;
function BrownianMotion()
{
fg = new F5BitmapData2D(W, H, false, 0xff000000);
addChild( new Bitmap(fg.bitmapData) );
fg.strokeAlpha = 0.1;
list = new Vector.<Dot>(N, true);
for (var i:int = 0; i < N; i++) {
var d:Dot = new Dot(Math.random()*0xffffff);
var r:Number = Math.sqrt(Math.random()) * MAX_R;
var angle:Number = Math.random() * (2 * Math.PI);
d.x = Math.cos(angle) * r;
d.y = Math.sin(angle) * r;
d.vx = 0;
d.vy = 0;
list[i] = d;
}
//addChild( new Stats() );
addEventListener(Event.ENTER_FRAME, onEnterFrameHandler);
}
private function onEnterFrameHandler(event:Event):void
{
fg.beginDraw();
fg.translate( W>>1, H>>1 );
for (var i:int = 0; i < N; i++)
{
var d:Dot = list[i];
d.update();
fg.strokeColor = 0x000000;
fg.pixel( d.x+1, d.y+1 );
fg.strokeColor = d.color;
fg.pixel( d.x, d.y );
}
fg.endDraw();
}
}
}
class Dot{
public var vx:Number;
public var vy:Number;
public var x:Number;
public var y:Number;
public var color:uint;
private const X:Number = 0.2;
private const F:Number = 0.95;
function Dot(color:uint) {
this.color = color;
}
public function update():void {
vx += Math.random() * X * 2 - X;
vy += Math.random() * X * 2 - X;
vx *= F;
vy *= F;
x += vx;
y += vy;
}
}