拡散方程式
/**
* Copyright tikal ( http://wonderfl.net/user/tikal )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/eD1y
*/
package {
import flash.text.TextField;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
[SWF(frameRate="30", width="465", height="465")]
public class Diff extends Sprite {
private var isMouseDown_:Boolean;
private var lastX:int;
private var lastY:int;
private var length:Number = stage.stageWidth;
private var pt : Array = new Array(length);
private var sp:Sprite;
private var lb:TextField;
public function Diff() {
stage.addEventListener(MouseEvent.MOUSE_DOWN,
function():void {
isMouseDown_ = true;
lastX = mouseX;
lastY = mouseY;
});
stage.addEventListener(MouseEvent.MOUSE_UP,
function():void {
isMouseDown_ = false;
});
stage.addEventListener(MouseEvent.MOUSE_OUT,
function():void {
isMouseDown_ = false;
});
stage.addEventListener(MouseEvent.MOUSE_OVER,
function():void {
isMouseDown_ = false;
});
addEventListener(Event.ENTER_FRAME, update );
for(var i:int=0;i<pt.length;++i){
pt[i] = 0;
}
sp = new Sprite();
sp.graphics.lineStyle(5, 0x00ffffff);
}
// アップデート
private function update(event:Event):void {
// マウスダウン中はパーティクル生成
var i:int;
if (isMouseDown_) {
var len:Number = Math.abs(mouseX-lastX);
for(i=0; i<=len; ++i ) {
pt[lastX + (mouseX-lastX)*i/len] = stage.stageHeight - (lastY + (mouseY-lastY)*i/len);
}
lastX = mouseX;
lastY = mouseY;
}else{
var f:Function = function(idx:int):Number{
if(idx < 0 || idx >= pt.length){
return 0;
}else{
return pt[idx];
}
}
var alpha:Number = 0.1;
for(var j:int=0;j<200;++j){
var npt:Array = new Array(pt.length);
for(i=0; i<pt.length; ++i ) {
npt[i] = (alpha * f(i-1)) + ((1-(2*alpha)) * f(i)) + (alpha * f(i+1));
}
this.pt = npt;
}
}
this.graphics.clear();
this.graphics.lineStyle(10, 0x00ff0000);
this.graphics.moveTo(0,stage.stageHeight);
for(i=0;i<pt.length;++i){
this.graphics.lineTo(i, stage.stageHeight - pt[i]);
}
}
}
}