从 x,y 坐标计算角度
可能的重复:
圆线碰撞检测
我正在尝试在有限线段和圆弧段。我有一个碰撞测试,它进行线段与线段的比较,因此我打算用线段来近似这些弧段并运行我现有的测试。
我定义弧段的数据是三个点。其中两个点是位于圆圆周上的端点,第三个点是该圆的中心。
到目前为止,这就是我所得到的:
设 (a,b) 为圆的中心点,设 'r' 为圆的半径,(x1, y1), (x2, y2) 为端点位于圆的圆周上的弧段。
以下参数方程给出了圆弧的 x 和 y 位置。 “t”是参数变量。
x = a + r * cos(t) y = b + r * sin(t)
为了从圆弧创建线段,我想沿着圆弧行走一些固定的“t”比率,沿途创建线段,直到到达圆弧的末端。为此,我想我必须找到起始角度和结束角度。我将从起始角度开始沿着弧线行走,并在结束角度结束。因为我知道起点和终点,所以我想我可以使用这些方程来求解这些角度。以下是我的方程:
t = arccos((xa)/r)
或
t = acrcsin((yb)/r)
我遇到的问题是这些函数返回的值的范围 (http://en.wikipedia.org/wiki/Inverse_trigonometric_function< /a>) 是有限的,因此很有可能不会返回我要查找的角度,因为这些函数是多值的:arcsin(0) = 0,而且 arcsin(0) = π, arcsin(0 ) = 2π 等
如何获得我正在寻找的精确角度?或者,您能想出更好/不同的方式来实现我的目标吗?
Possible Duplicate:
Circle line collision detection
I'm trying to do collision testing between a finite line segment, and an arc segment. I have a collision test which does line segment vs. line segment, so I was going to approximate these arc segments with line segments and run my existing test.
The data I have defining the arc segment(s) are three points. Two of which are endpoints that lie on the circumference of a circle, and the third point is the center of that circle.
So far this is what I've got:
Let (a,b) be the center point of the circle, let 'r' be the radius of the circle, and (x1, y1), (x2, y2) be the endpoints of the arc segment which lies on the circumference of the circle.
The following parametric equations give the x, and y locations of an arc. 't' is the parametric variable.
x = a + r * cos(t)
y = b + r * sin(t)
To create the line segments from the arc, I wanted to walk the arc for some fixed ratio of 't' creating line segments along the way, until I've reached the end of the arc. To do this I figured I'd have to find the start and end angle. I'd start walking the arc from the start angle, and end at the end angle. Since I know the start and end points I figured I could use these equations to solve for these angles. The following are my equations for this:
t = arccos((x-a)/r)
or
t = acrcsin((y-b)/r)
The problem I'm having is that the range of values returned by these functions (http://en.wikipedia.org/wiki/Inverse_trigonometric_function) is limited, so there is a high probability that the angle I'm looking for will not be returned because these functions are multivalued: arcsin(0) = 0, but also arcsin(0) = π, arcsin(0) = 2π, etc
How do I get the exact angle(s) I'm looking for? Or, can you think of a better/different way of achieving my goal?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看一下
atan2
函数,它应该存在于任何您正在使用的编程语言或数学库。它需要两个参数,一个点的 x 和 y 坐标(对于您来说:(xa)/r 和 (yb)/r),并返回 -π 到 +π 范围内的角度。Take a look at the
atan2
function, which should exist in whatever programming language or math library you're using. It takes two arguments, the x and y coordinates of a point (for you: (x-a)/r and (y-b)/r) and returns the angle in the range -π to +π.至少在我看来,你的做法是错误的。一条直线有一个方程
y=mx+b
。圆的方程为 x2 + y2 = r2。您正在寻找圆的 x 和 y 等于直线的 x 和 y 的点。您可以通过用直线的mx+b
方程替换圆中的y
方程,然后使用二次方程求解来实现此目的。涉及的方程确实有点长,但是有相当多的网页(例如,http://www.sonoma.edu/users/w/wilsonst/papers/geometry/circles/default.html)拥有它们,此时将方程实现为简单的问题几个函数并插入特定圆/线的值。基于这些方程的解完全避免了使用 arg 正切带来的歧义。
At least IMO, you're going at this the wrong way. A line has an equation
y=mx+b
. A circle has an equation x2 + y2 = r2. You're looking for a point at which the x and y of the circle equals the x and y of the line. You can do that by substituting themx+b
equation for the line for they
equation in the circle, and then solve using the quadratic equation.The equations involved do get a bit long, but quite a few web pages (e.g., http://www.sonoma.edu/users/w/wilsonst/papers/geometry/circles/default.html) have them, at which point it's simple matter of implementing the equations as a couple of functions and plugging in the values for your particular circle/line. A solution based on these equations complete avoids the ambiguity from using an arg tangent.
你的伪代码看起来很像Python。如果您不介意使用 Python,我会推荐 Shapely 库。如果您只想要算法,请检查来源。
形状对象具有“简化”和“相交”方法。
Your pseudo-code looks a lot like Python. If you don't mind using Python I would recommend the Shapely Library. If you just want the algorithm, check the source.
Shapely objects have the 'simplify' and 'intersection' methods.