forked from: Cluster Cluster [Different colors unite]
different colors unite...
left alones will always go to the mass...
simulation will always end with spherical
equilibrium gathering at the center of a given space
food for the thoughts ...
Thx @uwi
@Hasufel
/**
* Copyright Hasufel ( http://wonderfl.net/user/Hasufel )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/YqDt
*/
// forked from uwi's Cluster Cluster
// different colors unite...
// left alones will always go to the mass...
// simulation will always end with spherical
// equilibrium gathering at the center of a given space
// food for the thoughts ...
// Thx @uwi
// @Hasufel
package {
import flash.display.*;
import flash.geom.*;
import flash.events.*;
import flash.filters.BlurFilter;
[SWF(width="465", height="465", backgroundColor="0x0", frameRate="60")]
public class Cluster extends Sprite {
private const N:uint = 1000;
private const M:uint = 20; // attraction factor. The more, the clustier.
private var _filterObj:BlurFilter;
private var _cols:Array = [0xffffff,0xffcdff,0xbebebe,0xffcc00,0xff6532];
private var _bmd:BitmapData;
private var _p:Vector.<Number>; // 0:x, 1:y, 2:color
public function Cluster() {
setProps(stage, {quality:StageQuality.LOW,scaleMode:StageScaleMode.NO_SCALE,align:StageAlign.TOP_LEFT});
_filterObj=new BlurFilter(2,2,2);
_p = new Vector.<Number>(N*3);
for(var i:uint = 0;i < 3*N;i+=3){
_p[i] = Math.random() * 465;
_p[i+1] = Math.random() * 465;
_p[i+2] = _cols[randomNumber(0,4)];
}
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 += .1;
_bmd.applyFilter(_bmd,_bmd.rect,new Point(0,0),_filterObj);
_bmd.lock();
for(var i:uint = 0;i < 3*N;i+=3){
for(var u:uint = 0;u < M ; u++){
var j:uint = Math.random() * N;
j *= 3;
if(i == j)continue;
if (_p[j+2] == _p[i+2]) {
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], _p[i+2]);
}
}
_bmd.unlock();
}
private function setProps(o:*,p:Object):void {
for (var k:String in p) {o[k]=p[k];}
}
private function randomNumber(low:int, high:int):int{
return Math.round(Math.random() * (high - low) + low);
}
}
}