fc2ブログ

Subdivision Curves3, 4

2009-11-24 | 10:29

昨日の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 SubdivisionCurves3 extends Sprite{
            private var _balls:Array = [];
            private var _vertices:Array = [];

            public function SubdivisionCurves3():void
            {
                var points:Array = [];

                for(var j:int = 0 ; j < 12 ; j++){
                    var p:Point = new Point(Math.cos(Math.PI/6*j) * 100 + 200, Math.sin(Math.PI/6*j) * 100 + 100);
                    points.push(p);
                }

                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();
        }
}

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 SubdivisionCurves4 extends Sprite{
            private var _balls:Array = [];
            private var _vertices:Array = [];

            public function SubdivisionCurves4():void
            {
                var points:Array = [];

                for(var j:int = 0 ; j < 12 ; j++){
                    var p:Point = new Point(Math.cos(Math.PI/6*j) * 100 + 200, Math.sin(Math.PI/6*j) * 100 + 100);
                    points.push(p);
                }

                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();

                addEventListener(Event.ENTER_FRAME, loop);

            }

            private function loop(event:*):void{
                for each (var b:Ball in _balls){
                    b.move();
                }
                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(10,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 var vx:int = 5*Math.random();
    public var vy:int = 5*Math.random();
    public var radius:int;
    public var currentColor:int;
    public function Ball(c:Number=0, radius:Number = 0){
        graphics.lineStyle(1, 0, 0.5);
        graphics.beginFill(c);
        graphics.drawCircle(0,0,radius);
        graphics.endFill();
        this.radius = radius;
        this.alpha = 0.1;
    }

    public function set color(c:int):void{
        if(currentColor != c){
            currentColor = c;
            graphics.clear();
            graphics.lineStyle(1, 0, 0.5);
            graphics.beginFill(currentColor);
            graphics.drawCircle(0,0,radius);
            graphics.endFill();
        }
    }

    public function move():void{
        x += vx;
        y += vy;
        if(x < 0 || x > 500)
            vx *= -1;
        if(y < 0 || y > 500)
            vy *= -1;
    }
}

Comment

Post a comment

Secret