二维锥线段交点

发布于 2024-09-02 01:29:35 字数 529 浏览 5 评论 0原文

我想知道是否有任何方法可以确定圆锥体是否与(有限)线段相交。圆锥体实际上是一个位于 P(x,y) 处的圆,其视场为 θ 度,半径为 r:

插图

我正在尝试在 C# 中执行此操作,但我不知道如何执行此操作,所以现在这就是我正在做的事情:

  1. 检查线段是否与圆相交;
  2. 如果线段与圆相交,那么我使用我找到的函数检查线段中的每个点 此处

但我认为这不是最好的方法。有人有想法吗?

有关更多信息,我需要此函数来制作某种简单的视觉模拟器。

I would like to know if is there any way to determine if a cone is intersecting with a (finite) line segment. The cone is actually a circle located at P(x,y) with theta degree field of view and radius r:

Illustration

I'm trying to do it in C# but I don't have any idea how to that, so for now this is what I'm doing:

  1. Check if the line segment is intersecting with the circle;
  2. If the line segment is intersecting with the circle then I check every single point in the line segment using a function I found here.

But I don't think this is the best way to do it. Does anyone have an idea?

For additional info, I need this function to make some kind of simple vision simulator.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

〆凄凉。 2024-09-09 01:29:35

使用极坐标可能会有所帮助。在这里,不是将点表示为 (x,y),而是将其表示为 (r, 角度),其中 r 是距原点的距离,角度是与所选轴形成的角度(对应于角度 0)。

在您的情况下,如果将 P(x,y) 设置为原点,并将圆锥体的一条射线设置为 angle = 0 并找到线段端点的极坐标,例如 (r1, ang1) 和(r2, ang2) 则需要满足以下四个条件才能使线段完全位于圆锥体内(包括边界)。

r1 <= r
r2 <= r

ang1 <= theta
ang2 <= theta

其中 r 是圆锥体的半径,theta 是视角,您选择了轴,以便逆时针旋转给出相应的正角度。

在极坐标和 (x,y)(称为直角坐标)之间切换很容易,您可以在我上面提供的 wiki 链接上找到它。

为了确定线段的任何点是否与曲线相交,您可以使用此处给出的线的极坐标方程:链接

我们可以使用极坐标范式

R = p sec(ang - omega)

我们可以算出 p 和 omega给定线段的两个端点如下:

我们有

p = r1 * cos(ang1-omega) = r2*cos(ang2-omega)

使用 cos(xy) = cos(x)*cos(y) + sin(x)*sin(y) 我们

[r1*cos(ang1) - r2*cos(ang2)] * cos(omega) =  [r2*sin(ang2) - r1*sin(ang1)] * sin(omega)

得到您可以计算 tan(omega) = sin(omega)/cos(omega) 并使用 arctan(tan 的反函数)来获取 omega 的值。一旦知道 omega 是什么,您就可以求解 p。

现在我们需要知道这条线上是否存在一些(R,ang)组合,使得

R <= r
0 <= ang <= theta
min{ang1, ang2} <= ang <= max{ang1, ang2}

(注意r是圆锥半径,theta是视角,ang1是P1的角度,ang2是P2的角度)。

该方程可以重写为

Rcos(ang-omega) = p

现在 cos(ang-omega) 就单调性而言是一个表现良好的函数,您只需在区间 [min{ang1, ang2}, max{ang1, ang2}] 中考虑它。

您应该能够首先进行一些手动操作以简化代码。

剩下的就交给你了。

Working with Polar Co-ordinates might help. Here, instead of representing a point as (x,y) you represent it as (r, angle), where r is distance from origin and angle is the angle made with a chosen axis (which corresponds to angle 0).

In your case, if you set P(x,y) to be origin and one of the rays of the cone as angle = 0 and find polar co-ordinates of the end points of the line segment, say (r1, ang1) and (r2, ang2) then you need the following four conditions to be true for the line segment to be completely within (including boundary) of the cone.

r1 <= r
r2 <= r

ang1 <= theta
ang2 <= theta

where r is the radius of the cone and theta is the angle of view and you chose the axis so that rotating counter-clockwise gives a corresponding positive angle.

Switching between polar and (x,y) (called rectangular) coordinates is easy, and you can find it on the wiki link I gave above.

In order to determine if any point of the line segment intersects the curve you can use the polar equation of a line give here: Link

We can use the Polar normal form

R = p sec(ang - omega)

We can figure out p and omega given the two end-points of the line segment as follows:

We have

p = r1 * cos(ang1-omega) = r2*cos(ang2-omega)

Using cos(x-y) = cos(x)*cos(y) + sin(x)*sin(y) we get

[r1*cos(ang1) - r2*cos(ang2)] * cos(omega) =  [r2*sin(ang2) - r1*sin(ang1)] * sin(omega)

Thus you can calculate tan(omega) = sin(omega)/cos(omega) and use arctan (the inverse function of tan) to get the value of omega. Once you know what omega is, you can solve for p.

Now we need to know if there is some (R, ang) combination on this line such that

R <= r
0 <= ang <= theta
min{ang1, ang2} <= ang <= max{ang1, ang2}

(Note r was radius of cone, and theta was the angle of view, ang1 was angle of P1 and ang2 was angle of P2).

The equation can be rewritten as

Rcos(ang-omega) = p

Now cos(ang-omega) is a very nicely behaved function in terms of monotonicity and you only need to consider it in the interval [min{ang1, ang2}, max{ang1, ang2}].

You should be able to do some manual manipulations first in order to simplify your code.

I will leave the rest to you.

半城柳色半声笛 2024-09-09 01:29:35

我会搜索线/凸多边形相交算法,您的视野由三角形和圆的一部分组成,可以通过凸多边形近似到任何精度。您的第一步可能仍然有助于排除远离视野的线条。

I'd Google around for line/convex polygon intersection algorithms, your field of view is composed of a triangle and a part of a circle which can be approximated to any degree of accuracy by a convex polygon. Your first step might still be useful to rule out lines which go nowhere near the field of view.

乖乖公主 2024-09-09 01:29:35

如果像上面那样保持二维,交点可以计算如下:

线段的起点是S1,终点是S2。
代码的左边缘是沿着边缘C1的向量,右边缘是C2,代码的原点是O。

取从(O到S1)和C1形成的向量的叉积的Z分量的符号。

取从 (O 到 S2) 和 C1 的向量叉积的 Z 分量的符号。如果符号不同,则起点和终点位于 C1 的相对侧,因此它们必须相交。如果不是,则用 C2 而不是 C1 进行相同的比较。

如果双方的符号不同,则没有边相交。

在 3D 中,情况有点复杂。我用谷歌搜索并多次发现圆锥球体相交。对于线来说,会非常相似,我只需要考虑一下:)

快速谷歌搜索“锥线相交”得到了各种各样的点击。基本思想是从圆锥体的原点和直线的起点和终点形成一个平面。一旦你有了这个,你就可以得到该平面和圆锥体法线方向之间的角度。如果该角度小于圆锥体上的展开角度,则存在交点。

If you keep it 2D like above, intersection can be calculated as follows:

Starting point of line segment is S1, ending is S2.
Left edge of code is a vector along the edge C1, right edge is C2, Origin of code is O.

Take the sign of the Z component of the cross product of the vector formed from (O to S1) and C1.

Take the sign of the Z component of the cross product of the vector fromed from (O to S2) and C1. If the signs differ, your starting and ending points are on opposite sides of C1 and therefore they must intersect. If not, do the same comparisons with C2 instead of C1.

If neither side's signs differ, no edge intersection.

In 3D, it's a bit more complicated. I've googled and found cone sphere intersection any number of times. Doing it for lines, would be very similiar, I just need to think about it for a bit :)

A quick google of 'cone line intersection' got all sorts of hits. The basic idea is to form a plane from the cone's origin and the starting and ending point of the line. Once you have that, you can take the angle between that plane and the cone's direction normal. If that angle is less than the angle of the spread on the cone, you have an intersection.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文