forked from: ベジェをスムーズにつないでいく
bitmapFilterできれいに見えるようにした。
しまった。forkにするべきだった。
// forked from dalmacija's ベジェをスムーズにつないでいく
package {
// bitmapFilterできれいに見えるようにした。
// しまった。forkにするべきだった。
import flash.display.Sprite;
import flash.display.Shape;
import flash.display.Bitmap;
import flash.display.Stage;
import flash.display.BitmapData;
import flash.display.DisplayObjectContainer;
import flash.display.LineScaleMode;
import flash.display.BlendMode;
import flash.geom.Rectangle;
import flash.geom.Point;
import flash.events.Event;
import flash.utils.ByteArray;
import flash.filters.BlurFilter;
public class curve extends Sprite {
var shape1:Shape;
var shape2:Shape;
var bmp:Bitmap;
var curves:Object;
var rotationColor:Number;
var stg:DisplayObjectContainer;
var mouseRad:Number;
var mouseRadScale:Number;
public function curve() {
shape1=new Shape();//主線描画用
shape2=new Shape();//補助線描画用
bmp=new Bitmap(new BitmapData(stage.stageWidth,stage.stageHeight,true,0x00000000),"auto",true);//bitmap処理用
shape2.graphics.lineStyle(0, 0x0000ff, .2, true , LineScaleMode.NONE);
addChild(bmp);
addChild(shape2);
addChild(shape1);
curves=new Object();
init();
//mouseRad=0;
//mouseRadScale=20;
addEventListener(Event.ENTER_FRAME,update);
}
function init(){
rotationColor=Math.random()*0xffffff;
shape1.graphics.clear();
shape2.graphics.clear();
curves.x=100;//Math.random()*550;
curves.y=100;//Math.random()*400;
shape1.graphics.moveTo(curves.x,curves.y);
shape1.graphics.lineStyle(0, rotationColor, .5, true , LineScaleMode.NONE);
curves.before=clone(curves);
curves.x=120+Math.random()*20;
curves.y=120+Math.random()*20;
var cx=(curves.before.x+curves.x)/2;
var cy=(curves.before.y+curves.y)/2
var line=new Rectangle(curves.x,curves.y,curves.before.x,curves.before.y);
var turnLi:Point=getCrossLine(line);
var lineL=getLineLength(line);
trace(cx+" "+cy+" "+turnLi.x+" "+lineL);
var cpS=cy-turnLi.x*cx;
curves.ControlX=Math.sqrt(Math.abs(Math.pow(1/turnLi.x,2)))*lineL/2*(Math.round(Math.random())*2-1)+cx;
curves.ControlY=turnLi.x*curves.ControlX+cpS;
trace(curves.ControlX+":"+curves.ControlY);
curves.before=clone(curves);
shape1.graphics.curveTo(curves.ControlX,curves.ControlY,curves.x,curves.y);
shape2.graphics.lineStyle(0, 0x00ff00, .25, true , LineScaleMode.NONE);
shape2.graphics.drawCircle(curves.ControlX,curves.ControlY,5);
}
function update(e:Event){
var vec1=new Object();
vec1.vx=curves.before.x-curves.before.ControlX;
vec1.vy=curves.before.y-curves.before.ControlY;
vec1.vl=Math.sqrt(Math.pow(vec1.vx,2)+Math.pow(vec1.vy,2));
vec1.ux=vec1.vx/vec1.vl;
vec1.uy=vec1.vy/vec1.vl;
vec1.t=vec1.vy/vec1.vx;
vec1.s=curves.before.y-vec1.t*curves.before.x;
//shape2.graphics.moveTo(0,vec1.s);
//shape2.graphics.lineTo(550,vec1.t*550+vec1.s);
//---
var cnt=0;
mouseRad+=.1;
do{
/* var tagetpointX=this.mouseX+Math.cos(mouseRad)*mouseRadScale;
var tagetpointY=this.mouseY+Math.sin(mouseRad)*mouseRadScale;
var vectpX=tagetpointX-(curves.before.x);//+vec1.vx);
var vectpY=tagetpointY-(curves.before.y);//+vec1.vy);
var vectpL=Math.sqrt(Math.pow(vectpX,2)+Math.pow(vectpY,2));
trace(vectpL);
var vectpUX=vectpX/vectpL;
var vectpUY=vectpY/vectpL;
*/
curves.x=curves.before.x+vec1.vx+Math.random()*100-50;
curves.y=curves.before.y+vec1.vy+Math.random()*100-50;
var vl=Math.sqrt(Math.pow(curves.x-curves.before.x,2)+Math.pow(curves.y-curves.before.y,2));
cnt++;
if(cnt>10000){trace("行き詰った");init();return;}// "+vl+":"+curves.x+":"+curves.y);init();return;}
}while(vl<50 || vl>200 || curves.x<0 || curves.x>stage.stageWidth || curves.y<0 || curves.y>stage.stageHeight);
rotationColor=(rotationColor+7)*int(rotationColor<0xdddddd);
shape1.graphics.lineStyle(0, rotationColor, .5, true , LineScaleMode.NONE);
//curves.ControlX=Math.random()*(Number(vec1.vx>0)*450-curves.before.x)+curves.before.x+(Number(vec1.vx>0)*2-1)*100;
curves.ControlX=curves.before.x+vec1.vx;
curves.ControlY=vec1.t*curves.ControlX+vec1.s;
shape1.graphics.curveTo(curves.ControlX,curves.ControlY,curves.x,curves.y);
shape2.graphics.drawCircle(curves.x,curves.y,2);
shape2.graphics.drawCircle(curves.ControlX,curves.ControlY,5);
shape2.graphics.moveTo(curves.before.x,curves.before.y);
shape2.graphics.lineTo(curves.ControlX,curves.ControlY);
shape2.graphics.lineTo(curves.x,curves.y);
shape2.graphics.lineTo(curves.before.x,curves.before.y);
curves.before=null;
curves.before=clone(curves);
//bmp.bitmapData.draw(shape1,null,null,"screen");
//bmp.bitmapData.applyFilter(bmp.bitmapData,new Rectangle(0,0,this.width,this.height),new Point(0,0),new BlurFilter(4,4));
}
function getLineLength(p:Rectangle):Number{
var vx=p.x-p.width;
var vy=p.y-p.height;
var vl=Math.sqrt(Math.pow(vx,2)+Math.pow(vy,2));
return vl;
}
function getLine(p:Rectangle):Point{
var vx=p.x-p.width;
var vy=p.y-p.height;
//var vl=Math.sqrt(Math.pow(vx,2)+Math.pow(vy,2));
var ts=new Point();
ts.x=vy/vx;
ts.y=p.height-ts.x*p.width;
return ts;
}
function getCrossLine(p:Rectangle):Point{
var vx=p.x-p.width;
var vy=p.y-p.height;
var ts=new Point();
ts.x=-vx/vy;
ts.y=p.y-ts.x*p.x;
return ts;
}
function clone( arg:* ) :*{
var myBA:ByteArray = new ByteArray();
myBA.writeObject(arg);
myBA.position = 0;
return( myBA.readObject() );
}
}
}