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 flash.geom.*;
public class BrownianMotion extends Sprite {
private const N:int = 500;
private const F:Number = 0.95;
private const MAX_R:int = 50;
private const X:Number = 0.2;
private const W:int = 465;
private const H:int = 465;
private var list:Vector.<Dot>;
private var canvas:Sprite = new Sprite();
private var bmd:BitmapData;
private var wipe:ColorTransform = new ColorTransform (1, 1, 1, 1, 1, 1, 1);
function BrownianMotion() {
bmd = new BitmapData(stage.stageWidth, stage.stageHeight);
addChild(new Bitmap(bmd));
list = new Vector.<Dot>(N, true);
for (var i:int = 0; i < N; i++) {
var mc:Dot = new Dot(Math.random()*0xffffff);
var p:Point = newCoords (); mc.x = p.x; mc.y = p.y;
mc.vx = 0;
mc.vy = 0;
canvas.addChild(mc);
list[i] = mc;
}
addEventListener(Event.ENTER_FRAME, onEnterFrameHandler);
}
private function onEnterFrameHandler(event:Event):void {
for (var i:int = 0; i < N; i++) {
var mc:Dot = list[i];
mc.vx += Math.random() * X * 2 - X;
mc.vy += Math.random() * X * 2 - X;
mc.vx *= F;
mc.vy *= F;
mc.x += mc.vx;
mc.y += mc.vy;
// respawn
if ((mc.x < 0) || (mc.x > W) || (mc.y < 0) || (mc.y > H)) {
var p:Point = newCoords (); mc.x = p.x; mc.y = p.y;
}
}
bmd.draw(bmd, null, wipe); bmd.draw(canvas);
}
private function newCoords ():Point {
var p:Point = new Point;
var r:Number = Math.sqrt(Math.random()) * MAX_R;
var angle:Number = Math.random() * (2 * Math.PI);
p.x = W/2 + Math.cos(angle) * r;
p.y = H/2 + Math.sin(angle) * r;
return p;
}
}
}
import flash.display.Sprite;
class Dot extends Sprite{
public var vx:Number;
public var vy:Number;
public var color:uint;
function Dot(color:uint) {
this.color = color;
this.graphics.beginFill(color);
this.graphics.drawCircle(0, 0, 1);
this.graphics.endFill();
}
}