曲線の練習 うずまき
curveTo 二次ベジェ曲線の練習 うずまき
始点終点以外の●はコントロールポイント
// forked from hRommy's 曲線の練習 ランダム
package {
/**
* curveTo 二次ベジェ曲線の練習 うずまき
* 始点終点以外の●はコントロールポイント
*/
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.events.Event;
public class Curve extends Sprite {
private var numPt:uint=100;
private var points:Array = new Array();
public function Curve() {
init();
}
private function init():void{
//ポイント配置
trace(stage.stageWidth,stage.stageHeight);
for (var i:uint = 0; i < numPt; i++){
points.push( maru );
points[i].x = i*3*Math.cos(2*Math.PI*i/20)+stage.stageWidth/2;
points[i].y = i*3*Math.sin(2*Math.PI*i/20)+stage.stageHeight/2;
addChild(points[i]);
}
addEventListener(Event.ENTER_FRAME, _onEnterFrame);
}
private function _onEnterFrame(evt:Event):void{
drawCurve();
}
//ライン描画
private function drawCurve():void{
graphics.clear();
//コントロールポイントを結ぶ直線
graphics.lineStyle(1,0xAAFFCC);
graphics.moveTo(points[0].x,points[0].y);
for (var i:uint = 1; i < numPt; i++){
graphics.lineTo(points[i].x, points[i].y);
}
//曲線
graphics.lineStyle(1,0xFFCC00);
graphics.moveTo(points[0].x,points[0].y);
for (i = 1; i < numPt-1; i++){
if(i < numPt-2){
var apX:Number = (points[i].x + points[i+1].x)/2;
var apY:Number = (points[i].y + points[i+1].y)/2;
}else{
apX = points[i+1].x;
apY = points[i+1].y;
}
graphics.curveTo(points[i].x, points[i].y, apX, apY);
}
}
//●
private function get maru():Sprite{
var _maru:Sprite=new Sprite();
_maru.graphics.beginFill(0xFF0000, 0.4);
_maru.graphics.lineStyle(1,0xFF6666);
_maru.graphics.drawCircle(0,0,3);
_maru.buttonMode = true;
_maru.addEventListener(MouseEvent.MOUSE_DOWN, _onMouseDown);
_maru.addEventListener(MouseEvent.MOUSE_UP, _onMouseUp);
return _maru;
}
//ドラッグ
private function _onMouseDown(evt:MouseEvent):void{
evt.target.startDrag();
evt.target.addEventListener(MouseEvent.MOUSE_MOVE, _onMouseMove);
}
private function _onMouseMove(evt:MouseEvent):void{
evt.updateAfterEvent();
//drawCurve();
}
private function _onMouseUp(evt:MouseEvent):void{
evt.target.removeEventListener(MouseEvent.MOUSE_MOVE, _onMouseMove);
evt.target.stopDrag();
}
}
}