forked from: Clifford Attractor
Clifford Attractor
* refer to http://local.wasp.uwa.edu.au/~pbourke/fractals/clifford/
* ...
* @author sph62
*
// forked from sph62's Clifford Attractor
/**
* Clifford Attractor
* refer to http://local.wasp.uwa.edu.au/~pbourke/fractals/clifford/
* ...
* @author sph62
*
*/
package {
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.display.Stage;
import flash.geom.Point;
import flash.geom.ColorTransform;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.filters.BlurFilter;
[SWF(backgroundColor="0x000000", frameRate="60")]
public class Attractor extends Sprite {
private const FADE:ColorTransform = new ColorTransform(1, 1, 1, 1, -0.001, 0, 0);
private const BLUR:BlurFilter = new BlurFilter(4,4);
private const POINT:Point = new Point();
private const PARTICLES_NUM:int = 1000;
private const RANGE:Number = 2.5;
private var canvas:Bitmap;
private var params:Vector.<Number>;
private var particleList:Vector.<Particle> = new Vector.<Particle>;
public function Attractor():void {
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:* = null):void {
removeEventListener(Event.ADDED_TO_STAGE, init);
var info:TextField = new TextField();
info.textColor = 0x999999;
info.autoSize = "left";
info.appendText("だんだんできてきますよー \nClick to Reset");
addChild(info);
setParams();
setParticles();
setCanvas();
addEventListener(Event.ENTER_FRAME, draw);
stage.addEventListener(MouseEvent.CLICK, reset)
}
private function reset(e:MouseEvent):void {
setParams();
setParticles();
canvas.bitmapData.dispose();
var canvasData:BitmapData = new BitmapData(stage.stageWidth, stage.stageHeight, false, 0x00000000);
canvas.bitmapData = canvasData;
}
private function draw(e:Event):void {
var bd:BitmapData = canvas.bitmapData;
bd.colorTransform(bd.rect, FADE);
var i:int;
var c:uint, r:uint, g:uint, b:uint;
bd.lock();
for (i = 0 ; i < PARTICLES_NUM; i++) {
particleList[i].update();
c = bd.getPixel(particleList[i].pos.x, particleList[i].pos.y);
if((c & 0xFF) < 0xFF){
r = (((c >> 16) & 0xFF) + 0x40);
g = (((c >> 8) & 0xFF) + 0x80);
b = ((c & 0xFF) + 0x80);
c = (r << 16) | (g << 8) | b;
bd.setPixel(particleList[i].pos.x, particleList[i].pos.y, c);
}
}
bd.applyFilter(bd, bd.rect, POINT, BLUR);
bd.unlock();
}
private function setCanvas():void{
var canvasData:BitmapData = new BitmapData(stage.stageWidth, stage.stageHeight, false, 0x00000000);
canvas = new Bitmap(canvasData);
addChildAt(canvas,0);
}
private function setParticles():void {
var i:int;
for (i = 0 ; i < PARTICLES_NUM; i++) {
particleList[i] = new Particle(params,stage.stageWidth,stage.stageHeight);
}
}
private function setParams():void {
params = new Vector.<Number>(4,true);
params[0] = Math.random() * RANGE + 1;
params[1] = Math.random() * RANGE + 1;
params[2] = Math.random() * RANGE + 1;
params[3] = Math.random() * RANGE + 1;
}
}
}
import flash.geom.Point;
class Particle {
private var nx:Number;
private var ny:Number;
private var ox:Number;
private var oy:Number;
private var _pos:Point;
private var _params:Vector.<Number>;
private var _stageWidth:int;
private var _stageHeight:int;
public function Particle(params:Vector.<Number> , stageWidth:int,stageHeight:int) {
_params = params;
_stageWidth = stageWidth;
_stageHeight = stageHeight;
_pos = new Point();
ox = Math.random() * - 0.5
oy = Math.random() * - 0.5
}
public function update():void {
nx = Math.sin(_params[0] * oy) - Math.cos(_params[1] * ox);
ny = Math.sin(_params[2] * ox) - Math.cos(_params[3] * oy);
ox = nx;
oy = ny;
_pos.x = _stageWidth/4 * nx + _stageWidth/2;
_pos.y = _stageHeight/4 * ny + _stageHeight/2;
}
public function get pos():Point { return _pos; }
}