线与线段相交
如何检测直线(从点 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果这是一个二维任务(直线和线段位于同一平面,并且它们由二维坐标指定),那就很容易。
构造一个垂直于 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.
您可以简单地检查两条线(您的线和线段线)是否相交并评估交点。
第 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)
设
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)
anda
its direction the line equation isD = a.x+b
where
b = y - a.x
let
p1(x1,y1)
andp2(x2,y2)
a segment which equation isD' = a'.x+b'
for anyx
in[x1;x2]
where
a' = (y2-y1)/(x2-x1)
andb' = y2 - a'.x2 = y1 - a'.x1
you've got an intersection if there is an
x
between[x1;x2]
for whichD = D'
thus if
X = (b'-b)/(a-a')
belong to[x1;x2]
then
Y = a.X+b = a'.X+b
gives you the intersection pointP(X,Y)
for instance :
let
p(255,255)
,a = 1
=>
b = 0
let
p1(60,179)
andp2(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 atP(X,Y)
要检测交点,只需将 p1 和 p2 放入直线方程中即可。如果值的符号不同,则它们在线的两侧。
要找到交点,请使用您计算的先前值的权重计算 p1 和 p2 的加权平均值,并将其交换为 p1 和 p2(尽管是绝对值)。
因此该线与该线段相交,因为符号不同。要找到交点,请获取计算值交换绝对值的加权平均值:
要验证您的答案,请将 pi 放入直线方程中:
因此它在线上。而且它肯定在线段上,因为它是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).
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:
to verify your answer, put pi in the line equation:
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.