ベジェ曲線でうねうね
ベジェ曲線でうねうね
左クリックで追加
//
// ベジェ曲線でうねうね
// 左クリックで追加
//
package {
import flash.display.Sprite;
import flash.events.*;
[SWF(width="465", height="465", backgroundColor="0xFFFFFF", frameRate="60")]
public class FlashTest extends Sprite {
public function FlashTest() {
Main = this;
initialize();
stage.addEventListener(Event.ENTER_FRAME,update);
stage.addEventListener(MouseEvent.MOUSE_UP, MouseCheckUp);
stage.addEventListener(MouseEvent.MOUSE_DOWN, MouseCheckDown);
}
}
}
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.geom.*;
var Main:Sprite;
var SCREEN_W:Number = 465;
var SCREEN_H:Number = 465;
var Text:TextField
var PointAry:Vector.<ControlPoint> = new Vector.<ControlPoint>;
var Bezier3Ary:Vector.<Bezier3> = new Vector.<Bezier3>;
function initialize():void{
Text = new TextField();
Text.text = "左クリックで追加";
Text.autoSize = "left";
Main.addChild(Text);
graphicClear();
}
function update(e :Event):void{
graphicClear();
if( MouseData & MOUSE_LEFT_TRG ) {
addBezierLine( SCREEN_W/2, SCREEN_H/2, Main.stage.mouseX, Main.stage.mouseY );
}
for each( var pnt:ControlPoint in PointAry ) pnt.update();
for each( var b3:Bezier3 in Bezier3Ary ) b3.draw();
updateMouse();
}
function addBezierLine( sx:Number, sy:Number, ex:Number, ey:Number ):void{
var vx:Number = ex - sx;
var vy:Number = ey - sy;
var rx:Number = -vy;
var ry:Number = vx;
var r:Number = 100.0;
var range:Number = 2.0;
var pa:Vector.<ControlPoint> = new Vector.<ControlPoint>;
pa[0] = new ControlPoint( sx, sy, 0, 0 );
pa[1] = new ControlPoint( sx + rx*range*(Math.random()-0.5), sy + vy*range*(Math.random()-0.5), r, 0.05 );
pa[2] = new ControlPoint( sx + rx*range*(Math.random()-0.5), sy + vy*range*(Math.random()-0.5), r, 0.07 );
pa[3] = new ControlPoint( ex, ey, r*0.1, 0.1 );
Bezier3Ary.push( new Bezier3( pa[0], pa[1], pa[2], pa[3] ) );
for each( var p:ControlPoint in pa ) PointAry.push( p );
}
class ControlPoint{
public var Sp:Sprite;
public var isEnable:Boolean = false;
public var Pos:Point = new Point(), Bak:Point = new Point(), PosAdd:Point = new Point();
public var RotX:Number, RotY:Number, RotSpd:Number, Range:Number;
public function ControlPoint( x:Number, y:Number, range:Number, rotspd:Number ){
Sp=new Sprite();
Sp.x = Pos.x = Bak.x = x;
Sp.y = Pos.y = Bak.y = y;
Range = range;
RotX = Math.random() * 2.0 * Math.PI;
RotY = Math.random() * 2.0 * Math.PI;
RotSpd = rotspd;
}
public function update():void{
RotX += Math.random() * RotSpd + RotSpd * 0.4;
RotY += Math.random() * RotSpd + RotSpd * 0.4;
PosAdd.x = Range * Math.cos( RotX );
PosAdd.y = Range * Math.sin( RotY );
Sp.x = Pos.x + PosAdd.x;
Sp.y = Pos.y + PosAdd.y;
}
}
class Bezier3{
public var Pnt:Vector.<ControlPoint> = new Vector.<ControlPoint>;
public var DivNum:int = 20;
public var p4_x:Number, p4_y:Number, p5_x:Number, p5_y:Number, p6_x:Number, p6_y:Number;
public var p7_x:Number, p7_y:Number, p8_x:Number, p8_y:Number, p9_x:Number, p9_y:Number;
public function Bezier3( p0:ControlPoint, p1:ControlPoint, p2:ControlPoint, p3:ControlPoint ){
Pnt[0] = p0; Pnt[1] = p1; Pnt[2] = p2; Pnt[3] = p3;
}
public function draw():void{
var t:Number = 0;
var tAdd:Number = 1.0 / DivNum;
var pp_x:Number = Pnt[0].Sp.x, pp_y:Number = Pnt[0].Sp.y;
var size:Number = 10;
var colR:int = 0;
var colG:int = 0;
var colB:int = 0;
for( var lp:int=0; lp<DivNum; lp++,t+=tAdd ){
colR = (0x80 * lp / DivNum)<<16;
colG = (0x60 * lp / DivNum)<<8;
colB = 0x40 * lp / DivNum;
getPosition( t );
drawLine( pp_x, pp_y, p9_x, p9_y, size, 0xa03010+colG+colB );
pp_x = p9_x;
pp_y = p9_y;
size *= 0.94;
}
}
public function getPosition( t:Number ):void{
p4_x = Pnt[0].Sp.x + (Pnt[1].Sp.x - Pnt[0].Sp.x) * t;
p4_y = Pnt[0].Sp.y + (Pnt[1].Sp.y - Pnt[0].Sp.y) * t;
p5_x = Pnt[1].Sp.x + (Pnt[2].Sp.x - Pnt[1].Sp.x) * t;
p5_y = Pnt[1].Sp.y + (Pnt[2].Sp.y - Pnt[1].Sp.y) * t;
p6_x = Pnt[2].Sp.x + (Pnt[3].Sp.x - Pnt[2].Sp.x) * t;
p6_y = Pnt[2].Sp.y + (Pnt[3].Sp.y - Pnt[2].Sp.y) * t;
p7_x = p4_x + (p5_x - p4_x) * t;
p7_y = p4_y + (p5_y - p4_y) * t;
p8_x = p5_x + (p6_x - p5_x) * t;
p8_y = p5_y + (p6_y - p5_y) * t;
p9_x = p7_x + (p8_x - p7_x) * t;
p9_y = p7_y + (p8_y - p7_y) * t;
}
}
function graphicClear():void{
Main.graphics.clear();
Main.graphics.lineStyle(1.2,0x80c040);
Main.graphics.moveTo( SCREEN_W/2, 0 );
Main.graphics.lineTo( SCREEN_W/2, SCREEN_H );
Main.graphics.moveTo( 0, SCREEN_H/2 );
Main.graphics.lineTo( SCREEN_W, SCREEN_H/2 );
}
function drawLine( sx:Number, sy:Number, ex:Number, ey:Number, size:Number, col:int ):void{
Main.graphics.lineStyle(size,col);
Main.graphics.moveTo( sx, sy );
Main.graphics.lineTo( ex, ey );
}
var MOUSE_LEFT:int = 0x01;
var MOUSE_LEFT_TRG:int = 0x02;
var MouseData:int;
function MouseCheckDown(event:MouseEvent):void{
MouseData |= MOUSE_LEFT;
MouseData |= MOUSE_LEFT_TRG;
}
function MouseCheckUp(event:MouseEvent):void{
MouseData &= ~MOUSE_LEFT;
}
function updateMouse():void{
MouseData &= ~MOUSE_LEFT_TRG;
}