线与线段相交

发布于 2024-09-29 11:03:29 字数 348 浏览 0 评论 0原文

如何检测直线(从点 p 开始的 d 和 -d 方向)和线段(点 p1 和 p2 之间)是否在 2D 中相交?如果他们这样做,我怎样才能得到他们的交点。

有很多如何检测两条线段是否相交的示例,但这应该是更简单的情况。

我发现了这个,但我不明白什么是侧面操作员: http://www.loria.fr/~ lazard//ARC-Visi3D/Pant-project/files/Line_Segment_Line.html

How can I detect whether a line (direction d and -d from point p) and a line segment (between points p1 and p2) intersects in 2D? If they do, how can I get their intersection point.

There are lots of example how to detect whether two line segments intersects but this should be even simpler case.

I found this but I do not understand what is a side-operator:
http://www.loria.fr/~lazard//ARC-Visi3D/Pant-project/files/Line_Segment_Line.html

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

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

发布评论

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

评论(4

原野 2024-10-06 11:03:29

如果这是一个二维任务(直线和线段位于同一平面,并且它们由二维坐标指定),那就很容易。

构造一个垂直于 d(直线方向)的向量,称为 n。

计算点积 n.(p1-p) 和 n.(p2-p)。如果它们具有相同的符号,则没有交集。如果它们的符号相反,则存在交叉点。稍微思考一下,您就可以弄清楚如何用 p、p1-p 和 p2-p 来计算交点的位置。

If this is a 2D task (the line and the segment lie in the same plane and they are specified by 2-dimensional coordinates), it's easy.

Construct a vector that is normal to d (the direction of the line) called n.

Compute dot products n.(p1-p) and n.(p2-p). If they have the same sign, there's no intersection. If they have opposite signs, there is an intersection. With a little bit of thought, you can figure out how to compute the location of the intersection in terms of p, p1-p and p2-p.

浅唱々樱花落 2024-10-06 11:03:29

您可以简单地检查两条线(您的线和线段线)是否相交并评估交点。

第 1 行:(x,y)(t) = p + t*d;
第 2 行: (x,y)(t) = p1 + k*(p2 - p1)

在交点处:
p + t*d = p1 + k*(p2 - p1) - 两个方程(每个 x 和每个 y)

从该方程中,您可以简单地找到 k 和 t 参数。如果 0 < k< 1 交点位于 (p1, p2)

如果您知道 k 或 t,您可以简单地从 (x,y)(t) = p + t*d 计算交点或
(x,y)(t) = p1 + k*(p2 - p1)

You can simply check if two lines (your line, and a line segment line) intersects and evaluate the intersection point.

line 1: (x,y)(t) = p + t*d;
line 2: (x,y)(t) = p1 + k*(p2 - p1)

At the intersection point:
p + t*d = p1 + k*(p2 - p1) - two equations (per x and per y)

From that equations you can simply find k and t parameters. If 0 < k < 1 the intersection point is in (p1, p2)

If you know k or t you can simply compute the intersection point from (x,y)(t) = p + t*d or
(x,y)(t) = p1 + k*(p2 - p1)

南冥有猫 2024-10-06 11:03:29

p(x,y)a其方向,直线方程为D = a.x+b
其中 b = y - ax

p1(x1,y1)p2(x2,y2) 为方程为 D 的线段' = a'.x+b' 对于 [x1;x2] 中的任何 x
其中 a' = (y2-y1)/(x2-x1)b' = y2 - a'.x2 = y1 - a'.x1

你有如果 [x1;x2] 之间存在 x,且 D = D' ,则为交集
因此,如果 X = (b'-b)/(aa') 属于 [x1;x2]
然后 Y = a.X+b = a'.X+b 给出交点 P(X,Y)

例如:

p(255,255 ),a = 1
=> b = 0

p1(60,179)p2(168,54)

=>; a' = -125/108

=>; b' = 24596/99

=> X = (24596/99 - 0)/(1+125/108) = 115,1587983

=> Y = (24596/99 - 0)/(1+125/108) = 115,1587983

X 介于 60 和 168 之间,因此在 处有一个交点>P(X,Y)

let p(x,y) and a its direction the line equation is D = a.x+b
where b = y - a.x

let p1(x1,y1) and p2(x2,y2) a segment which equation is D' = a'.x+b' for any x in [x1;x2]
where a' = (y2-y1)/(x2-x1) and b' = y2 - a'.x2 = y1 - a'.x1

you've got an intersection if there is an x between [x1;x2] for which D = D'
thus if X = (b'-b)/(a-a') belong to [x1;x2]
then Y = a.X+b = a'.X+b gives you the intersection point P(X,Y)

for instance :

let p(255,255), a = 1
=> b = 0

let p1(60,179) and p2(168,54)

=> a' = -125/108

=> b' = 24596/99

=> X = (24596/99 - 0)/(1+125/108) = 115,1587983

=> Y = (24596/99 - 0)/(1+125/108) = 115,1587983

X is between 60 and 168 so there is an intersection point at P(X,Y)

谁与争疯 2024-10-06 11:03:29

要检测交点,只需将 p1 和 p2 放入直线方程中即可。如果值的符号不同,则它们在线的两侧。

要找到交点,请使用您计算的先前值的权重计算 p1 和 p2 的加权平均值,并将其交换为 p1 和 p2(尽管是绝对值)。

example: p=(3,2) d=(7,6) p1=(9,10) p2=(12,8)
line equation is ax+by+c=0
for a and b you can put (6,-7) so 6x-7y+c=0
to find c, put p in the line equation 6*3-7*2+c=0 so c=-4
so the line equation is 6x-7y-4=0
p1: 6*9-7*10-4=-20
p2: 6*12-7*8-4=12

因此该线与该线段相交,因为符号不同。要找到交点,请获取计算值交换绝对值的加权平均值:

pi=(p1*12+p2*20)/(12+20) = (10.875,8.75)

要验证您的答案,请将 pi 放入直线方程中:

6*10.875-7*8.75-4=0 ok!

因此它在线上。而且它肯定在线段上,因为它是p1和p2的线性组合。有关更多信息,请参阅线性代数书籍。

我尽可能简单地解释了。

To detect the intersection, simply put p1 and p2 in the line equation. if the sign of values was not the same, they are on both sides of the line.

to find the intersection point, calculate the weighted average of p1 and p2 with the weight of previous values you calculated, swapped for p1 and p2 (absolute values albeit).

example: p=(3,2) d=(7,6) p1=(9,10) p2=(12,8)
line equation is ax+by+c=0
for a and b you can put (6,-7) so 6x-7y+c=0
to find c, put p in the line equation 6*3-7*2+c=0 so c=-4
so the line equation is 6x-7y-4=0
p1: 6*9-7*10-4=-20
p2: 6*12-7*8-4=12

so the line intersects that line segment because signs are not the same. to find the intersection point, get the weighted average with swapped absolute of calculated values:

pi=(p1*12+p2*20)/(12+20) = (10.875,8.75)

to verify your answer, put pi in the line equation:

6*10.875-7*8.75-4=0 ok!

so it is on the line. and also it is definitely on the line segment because it is a linear combination of p1 and p2. For more information refer to linear algebra books.

I explained as simply as I could.

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