2009-11-23 | 20:11
以前投稿したSubdivisonによる線の円滑化のリアルタイム版。
赤い円をドラッグで移動するとリアルタイムに線も再計算されます。
赤い円をドラッグで移動するとリアルタイムに線も再計算されます。
package { import flash.display.*; import flash.events.*; import flash.text.*; import flash.utils.*; import flash.ui.Keyboard; import flash.geom.*; import flash.filters.*; [SWF(width="500", height="500", frameRate="10", backgroundColor="#ffffff")] public class SubdivisionCurves2 extends Sprite{ private var _balls:Array = []; private var _vertices:Array = []; public function SubdivisionCurves2():void { var points:Array = [new Point(100, 200), new Point(20, 400) , new Point(300, 400), new Point(400, 300) , new Point(300, 100) ]; for(var i:int = 0 ; i < points.length ; i++){ var b:Ball = new Ball(0xff0000, 10); _balls.push(b); addChild(b); b.x = points[i].x; b.y = points[i].y; b.addEventListener( MouseEvent.MOUSE_DOWN, pickup ); b.addEventListener( MouseEvent.MOUSE_UP, place ); b.addEventListener( MouseEvent.MOUSE_MOVE, function(e:*):void{ // calcVolume(); initVertices(); while(_vertices.length <= 100) subdivision(); }); } initVertices(); while(_vertices.length <= 100) subdivision(); } private function initVertices():void{ _vertices = []; for each (var b:Ball in _balls){ _vertices.push(new Point(b.x, b.y)); } drawVertices(); } private function pickup( event:MouseEvent ):void { event.target.startDrag( ); event.target.filters = [ new DropShadowFilter( ) ]; setChildIndex( DisplayObject( event.target ), numChildren - 1 ); } private function place( event:MouseEvent ):void { event.target.stopDrag( ); event.target.filters = null; } private function subdivisionFromTo(from:Point, to:Point):Array{ var v1:Point = new Point(from.x + (to.x - from.x) * (1/4), from.y + (to.y - from.y) * (1/4)); var v2:Point = new Point(from.x + (to.x - from.x) * (3/4), from.y + (to.y - from.y) * (3/4)); return [v1, v2]; } private function subdivision():void{ trace(_vertices.length); if(_vertices.length > 100) return; var newVertices:Array = []; var temp:Array; for (var i:int = 0 ; i < _vertices.length - 1 ; i++){ temp = subdivisionFromTo(_vertices[i], _vertices[i+1]); newVertices = newVertices.concat(temp); } temp = subdivisionFromTo(_vertices[_vertices.length - 1], _vertices[0]); newVertices = newVertices.concat(temp); _vertices = newVertices.concat(); drawVertices(); } private function drawVertices():void{ graphics.clear(); graphics.lineStyle(4,0xff,0.5); graphics.moveTo(this._vertices[0].x, this._vertices[0].y); for (var i:int = 1 ; i < this._vertices.length ; i++){ graphics.lineTo(this._vertices[i].x, this._vertices[i].y); } graphics.lineTo(this._vertices[0].x, this._vertices[0].y); } } } import flash.display.*; internal class Ball extends Sprite{ public function Ball(color:int, radius:int){ graphics.beginFill(color); graphics.drawCircle(0,0,radius); graphics.endFill(); } }