fc2ブログ

直線の交点

2008-05-20 | 13:40

頂点U0,U1を結ぶ直線と 頂点V0,V1を結ぶ直線の交点は、媒介変数s,tを使用して
U0 + s(U1-U0) = V0 + t(V1-V0)
と表現できる。

s,tは

で求まる。
((x,y) = (y, -x))

緑の点をマウスドラッグで移動できます。青い丸が上記のやり方で求めた交点。



package{
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.utils.*;
    import flash.filters.DropShadowFilter;

    [SWF(width="500", height="500",backgroundColor="0xfcfcfc")]

        public class CrossPoint extends Sprite{
            private var greenBall1:Ball = new Ball(0xff00, 5);
            private var greenBall2:Ball = new Ball(0xff00, 5);
            private var greenBall3:Ball = new Ball(0xff00, 5);
            private var greenBall4:Ball = new Ball(0xff00, 5);

            public function CrossPoint(){
                greenBall1.x = greenBall1.y = 30;
                greenBall2.x = greenBall2.y = 300;
                greenBall3.x = 200;
                greenBall3.y = 20;
                greenBall4.x = 50;
                greenBall4.y = 200;
                addChild(greenBall1);
                addChild(greenBall2);
                addChild(greenBall3);
                addChild(greenBall4);

                initListener(greenBall1);
                initListener(greenBall2);
                initListener(greenBall3);
                initListener(greenBall4);

                cal();
            }

            private function initListener(b:Ball):void{
                b.addEventListener( MouseEvent.MOUSE_DOWN, pickup );
                b.addEventListener( MouseEvent.MOUSE_UP, place );
                b.addEventListener( MouseEvent.MOUSE_MOVE, function(e:*):void{ cal(); });
            }

            public function pickup( event:MouseEvent ):void {
                // ドラッグ処理を開始して、影フィルターをつける
                event.target.startDrag( );
                event.target.filters = [ new DropShadowFilter( ) ];
                // 最前面に表示されるようにする
                setChildIndex( DisplayObject( event.target ), numChildren - 1 );
            }

            public function place( event:MouseEvent ):void {
                // ドラッグ処理を終了して影フィルターをオフ
                event.target.stopDrag( );
                event.target.filters = null;

                cal();
            }

            private function cal() :void{
                var v0u0:Point = new Point(greenBall3.x - greenBall1.x,
                        greenBall3.y - greenBall1.y);
                var v1v0:Point = new Point(greenBall4.x - greenBall3.x,
                        greenBall4.y - greenBall3.y);
                var u1u0:Point = new Point(greenBall2.x - greenBall1.x,
                        greenBall2.y - greenBall1.y);
                var v1v0t:Point = new Point(v1v0.y, -v1v0.x);
                var u1u0t:Point = new Point(u1u0.y, -u1u0.x);

                var s:Number = (v0u0.x*v1v0t.x + v0u0.y*v1v0t.y) /
                    (u1u0.x*v1v0t.x + u1u0.y*v1v0t.y) ;
                var t:Number = (v0u0.x*u1u0t.x + v0u0.y*u1u0t.y) /
                    (u1u0.x*v1v0t.x + u1u0.y*v1v0t.y) ;

                var crossPoint:Point = new Point(greenBall1.x + s*u1u0.x,
                        greenBall1.y + s*u1u0.y);

                graphics.clear();
                graphics.beginFill(0xff);
                graphics.drawCircle(crossPoint.x, crossPoint.y, 10);
                graphics.endFill();
                graphics.lineStyle(3, 0x0, 0.5);
                graphics.moveTo(greenBall1.x, greenBall1.y);
                graphics.lineTo(greenBall2.x, greenBall2.y);
                graphics.moveTo(greenBall3.x, greenBall3.y);
                graphics.lineTo(greenBall4.x, greenBall4.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();
    }
}

スポンサーサイト