Clifford Attractor
Clifford Attractor
* refer to http://local.wasp.uwa.edu.au/~pbourke/fractals/clifford/
* ...
* @author sph62
*
/**
* 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.events.Event;
import flash.events.MouseEvent;
import flash.text.TextField;
[SWF(backgroundColor="0x000000", frameRate="60")]
public class Attractor extends Sprite {
private const PARTICLES_NUM:int = 200;
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 i:int;
var c:uint, r:uint, g:uint, b:uint;
canvas.bitmapData.lock();
for (i = 0 ; i < PARTICLES_NUM; i++) {
particleList[i].update();
c = canvas.bitmapData.getPixel(particleList[i].pos.x, particleList[i].pos.y);
if((c & 0xFF) < 0xFF){
r = (((c >> 16) & 0xFF) + 0x05*Math.random());
g = (((c >> 8) & 0xFF) + 0x10*Math.random());
b = ((c & 0xFF) + 0x25*Math.random());
c = (r << 16) | (g << 8) | b;
canvas.bitmapData.setPixel(particleList[i].pos.x, particleList[i].pos.y, c);
}
}
canvas.bitmapData.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; }
}