flash - 从一个点到圆上的相反切线画一条线? AS3 中的锥形/楔形

标签 flash actionscript-3 math geometry

这应该是一些简单的几何图形:如何计算在下面的代码中绘制线条的点,使其成为 2D 锥形或楔形?

import flash.geom.Point;

//draw circle
var mc=new Sprite()
mc.graphics.lineStyle(0,0)
mc.graphics.drawCircle(0,0,30)
mc.x=mc.y=Math.random()*300+100
addChild(mc)

//draw lines:
graphics.lineStyle(0,0)
var p=new Point(Math.random()*500,Math.random()*400)
graphics.moveTo(p.x, p.y)
graphics.lineTo(mc.x,mc.y) // << should be point on edge of circle
graphics.moveTo(p.x, p.y)
graphics.lineTo(mc.x,mc.y) // << should be point on opposite edge of circle

更新:
谢谢大家,我应该提到我的目标不是绘制楔形,而是从随机点到现有圆的边缘绘制一条线。

如果你更喜欢代数而不是 Action ,也许你可以看看这个图形并为我发布一个公式?
tangents

最佳答案

您的问题是关于泰勒斯定理(见 http://en.wikipedia.org/wiki/Thales%27_theorem)。

以下是稍微修改的定理以适用于 AS3。

导入 flash.geom.Point;

// The radius of the circle
var r1:Number = 30;
// The center point of the circle
var cp:Number = Math.random() * 300+100;
var c:Point = new Point(cp, cp);

// draw circle
var mc=new Sprite();
mc.graphics.lineStyle(0,0);
mc.graphics.drawCircle(0,0,r1);
mc.x = mc.y = cp;
addChild(mc);

// The point
var p = new Point(Math.random() * 500, Math.random() * 400);

// Calculate points for intesecting circle
var c2:Point = Point.interpolate(c, p, 0.5);
var r2:Number = Point.distance(c2, c);
var d:Number = Point.distance(c, c2);

// Remove comment below to see intersecting circle
//graphics.beginFill(0xFF0000, 0.25);
//graphics.drawCircle(c2.x, c2.y, r2);

var a:Number = (r1*r1 - r2*r2 + d*d) / (2*d);
var p2x:Number = c.x + a * ( c2.x - c.x ) / d;
var p2y:Number = c.y + a * ( c2.y - c.y ) / d;
var h:Number = Math.sqrt(r1*r1 - a*a);

var d1x:Number = p2x + h * ( c2.y - c.y ) / d;
var d1y:Number = p2y - h * ( c2.x - c.x ) / d;
var d2x:Number = p2x - h * ( c2.y - c.y ) / d;
var d2y:Number = p2y + h * ( c2.x - c.x ) / d;

// Draw lines
graphics.lineStyle(1, 0xFF00FF, 0.5);
graphics.moveTo(p.x, p.y);
graphics.lineTo(d1x, d1y);
graphics.moveTo(p.x, p.y);
graphics.lineTo(d2x, d2y);

最终产品:

enter image description here

绘制第二个圆(您实际上不需要在第二个圆中绘制,您只需要它的中心点和半径)

enter image description here

查看以下 SWF 以查看实际效果(刷新以查看不同的随机圆圈):

http://megaswf.com/serve/1097652

关于flash - 从一个点到圆上的相反切线画一条线? AS3 中的锥形/楔形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5683471/

相关文章:

apache-flex - Action Script3/Pixel Bender 复合模糊效果?

flash - 编译到Flash 8时如何让Haxe播放声音文件?

python - 检查角度是否落入圆形置信区间的优雅(最有效)方法

python - 添加随机加权点

math - Three.js 平面的奇怪现象

java - 在基于浏览器的游戏中创建 UDP 和 TCP 连接?

flash - 错误 #2032 : Stream Error

apache-flex - 您可以从 Adob​​e Air 访问 Windows 注册表吗?

actionscript-3 - 加载后无法显示很大的图像

actionscript-3 - 可视化大型 as3 项目的好方法是什么?