Cluster Cluster
Copyright uwi ( http://wonderfl.net/user/uwi )
MIT License ( http://www.opensource.org/licenses/mit-license.php )
Downloaded from: http://wonderfl.net/c/m89x
/**
* Copyright uwi ( http://wonderfl.net/user/uwi )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/m89x
*/
/**
* Copyright uwi ( http://wonderfl.net/user/uwi )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/m89x
*/
package {
import flash.text.TextField;
import flash.display.*;
import flash.geom.*;
import flash.events.*;
public class Test extends Sprite {
private var _tf : TextField;
private var _bmd : BitmapData;
private var _p : Vector.<Number>; // 0:x, 1:y
private const N : uint = 2000;
public function Test() {
/*
_tf = new TextField();
_tf.width = 465;
_tf.height = 465;
addChild(_tf);
*/
_p = new Vector.<Number>(N*2);
for(var i : uint = 0;i < 2*N;i+=2){
_p[i] = Math.random() * 465;
_p[i+1] = Math.random() * 465;
}
addEventListener(Event.ENTER_FRAME, onEnterFrame);
_bmd = new BitmapData(465, 465, false, 0x000000);
addChild(new Bitmap(_bmd));
}
// 引力の及ぶ半径
private var _r : Number = 1.0;
private function onEnterFrame(e : Event) : void
{
_r += 0.1;
_bmd.lock();
_bmd.fillRect(_bmd.rect, 0);
for(var i : uint = 0;i < 2*N;i+=2){
for(var u : uint = 0;u < 20;u++){
var j : uint = Math.random() * N;
j *= 2;
if(i == j)continue;
var dx : Number = _p[j] - _p[i];
var dy : Number = _p[j+1] - _p[i+1];
var d2 : Number = dx * dx + dy * dy;
// 引力
if(d2 < _r * _r){
_p[i] += (_p[j] - _p[i]) > 0 ? 1 : -1;
_p[i+1] += (_p[j+1] - _p[i+1]) > 0 ? 1 : -1;
}
// 斥力
if(d2 < _r * _r / 16){
_p[i] -= (_p[j] - _p[i]) > 0 ? 1 : -1;
_p[i+1] -= (_p[j+1] - _p[i+1]) > 0 ? 1 : -1;
}
}
_bmd.setPixel(_p[i], _p[i+1], 0xffffff);
}
_bmd.unlock();
}
private function tr(...o : Array) : void
{
_tf.appendText(o + "\n");
}
}
}