获取任意位置与圆直径形成的等腰三角形的底边与圆的交点的坐标

  • 本站文章除注明转载外,均为本站原创或者翻译。
  • 本站文章欢迎各种形式的转载,但请18岁以上的转载者注明文章出处,尊重我的劳动,也尊重你的智商;
  • 本站部分原创和翻译文章提供markdown格式源码,欢迎使用文章源码进行转载;
  • 本博客采用 WPCMD 维护;
  • 本文标题:获取任意位置与圆直径形成的等腰三角形的底边与圆的交点的坐标
  • 本文链接:http://zengrong.net/post/1097.htm


原理图

判断鼠标位置与圆直径形成的等腰三角形的底边与圆的交点的坐标原理图

效果


Get Adobe Flash player

源码

package
{
    import flash.display.Sprite;
    import flash.events.MouseEvent;

    [SWF(width=400,height=400)]
    public class SimpleSample5 extends Sprite
    {
        private var _centerX:Number;
        private var _centerY:Number;
        private var _radius:Number = 80;

        public function SimpleSample5()
        {
            _centerX = stage.stageWidth/2;
            _centerY = stage.stageHeight/2;
            draw();
            drawTriangle(0, 0);
            stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
        }

        private function draw():void
        {
            graphics.clear();
            graphics.lineStyle(1);
            graphics.moveTo(0, stage.stageHeight/2);
            graphics.lineTo(stage.stageWidth, _centerY);
            graphics.moveTo(_centerX, 0);
            graphics.lineTo(_centerX, stage.stageHeight);
            graphics.drawCircle(_centerX, _centerY, _radius);
        }

        private function drawTriangle($mouseX:Number, $mouseY:Number):void
        {
            var __dx:Number = _centerX - $mouseX;
            var __dy:Number = _centerY - $mouseY;
            var __distance:Number = Math.sqrt(__dx*__dx + __dy*__dy);
            //计算点击处与圆心夹角的角度并在坐标系中旋转90度
            var __radian:Number = Math.atan2(__dy, __dx) - Math.PI/2;
            var __x2:Number = _radius * Math.cos(__radian);
            var __y2:Number = _radius * Math.sin(__radian);
            graphics.moveTo(_centerX, _centerY);
            graphics.lineTo($mouseX, $mouseY);
            graphics.lineTo(_centerX+__x2, _centerY+__y2);
            graphics.lineTo(_centerX, _centerY);
            graphics.lineTo(_centerX-__x2, _centerY-__y2);
            graphics.lineTo($mouseX, $mouseY);
        }

        private function mouseMoveHandler(evt:MouseEvent):void
        {
            draw();
            drawTriangle(mouseX, mouseY);
        }
    }
}