求两个圆的交点。
参考:https://blog.csdn.net/Akiyama_sou/article/details/119806236
效果:
代码:
package { import flash.display.MovieClip; import flash.display.Sprite; import flash.events.Event; import flash.events.MouseEvent; import flash.geom.Point; import flash.text.TextField; public class Main extends MovieClip { public var p0: MovieClip; public var p1: MovieClip; public var p2: MovieClip; public var p3: MovieClip; public var tf: TextField; public function Main() { // constructor code stage?initStage(null):addEventListener(Event.ADDED_TO_STAGE, initStage); } private function initStage(e:Event):void { removeEventListener(Event.ADDED_TO_STAGE, initStage); drag(p0); drag(p1); drag(p2); drag(p3); addEventListener(Event.ENTER_FRAME, enterFrameHandler); } private function enterFrameHandler(e:Event):void { graphics.clear(); graphics.lineStyle(1, 0x0000ff, 1.0); graphics.drawCircle(p0.x, p0.y, distance2(p0, p1)); graphics.lineStyle(1, 0x00ff00, 1.0); graphics.drawCircle(p2.x, p2.y, distance2(p2, p3)); var r0: Number = distance2(p0, p1); var r1: Number = distance2(p2, p3); var d: Number = distance2(p0, p2); var a: Number = (d * d + r0 * r0 - r1 * r1) / (2 * d); if (Math.abs(a) > r0) { // 没有交点 tf.text = "没有交点"; } else { var h: Number = Math.sqrt(r0 * r0 - a * a); tf.text = h == 0 ? "一个交点": "两个交点"; // 几乎不会刚好是0 var n1: Point = new Point((p2.x - p0.x) / d, (p2.y - p0.y) / d); var n2: Point = new Point( -n1.y, n1.x); var p01: Point = new Point(p0.x + a * n1.x + h * n2.x, p0.y + a * n1.y + h * n2.y); var p02: Point = new Point(p0.x + a * n1.x - h * n2.x, p0.y + a * n1.y - h * n2.y); graphics.lineStyle(1.0, 0x000000, 1.0); graphics.drawCircle(p01.x, p01.y, 3); graphics.drawCircle(p02.x, p02.y, 3); } } private function distance2(p0: Object, p1: Object): Number { return distance(p0.x - p1.x, p0.y - p1.y); } private function distance(x: Number, y: Number):Number { return Math.sqrt(x * x + y * y); } //----------------------------- private var curSp:Sprite; private var draged:Boolean = false; private function drag(sp:Sprite):void { sp.buttonMode = true; sp.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler); } private function mouseDownHandler(e:MouseEvent):void { curSp = e.currentTarget as Sprite; curSp.startDrag(); addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler); stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler); stage.addEventListener(Event.MOUSE_LEAVE, mouseUpHandler); } private function mouseUpHandler(e:MouseEvent):void { curSp.stopDrag(); curSp = null; removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler); stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler); stage.removeEventListener(Event.MOUSE_LEAVE, mouseUpHandler); } private function mouseMoveHandler(e:MouseEvent):void { // draged = true; } } }
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。