问题描述
我想找到一种方法,检查是否两条线段相交或not.I正在使用Xlib的编程来实现这一点。
I want to find a way to check whether two line segments intersects or not.I am using Xlib programming to implement this.
我查了网上,但我只找到了方法,找到交集的两条线,但不是两条线段点。
I checked on internet but i only found the ways to find the intersection point of two lines but not of two line segments.
我怎样才能实现这个用X LIB编程?
How can i implement this using X lib programming?
推荐答案
您不需要的Xlib这一点。让分两部分
You don't need Xlib for this. Let the two segments be
-
A1 =(X1,Y1)
- >B1 =(X1 + DX1,Y1 + DY1)
和 -
A2 =(x2,y2)
- >B2 =(X2 + DX2,Y2 + DY2)
。
A1 = (x1, y1)
->B1 = (x1 + dx1, y1 + dy1)
andA2 = (x2, y2)
->B2 = (x2 + dx2, y2 + dy2)
.
让
-
VP = DX1 * DY2 - DX2 * DY1
如果 VP == 0
段是平行的,没有交集。
If vp == 0
the segments are parallel and there is no intersection.
否则,让 V =(VX,VY)
是 A1
和 A2
-
VX = X2 - X1
-
VY = Y2 - Y1
vx = x2 - x1
vy = y2 - y1
计算
-
K1 =(VX * DY2 - VY * DX2)/ VP
-
K2 =(VX * DY1 - VY * DX1)/ VP
k1 = (vx * dy2 - vy * dx2) / vp
k2 = (vx * dy1 - vy * dx1) / vp
如果任一 K1
或 2
会超出 [0,1]
的间隔,该段没有相交(但基础线确实相交)。否则,交叉是在
If either k1
or k2
fall outside the [0, 1]
interval, the segments don't intersect (but the underlying lines do intersect). Otherwise, the intersection is at
(X1 + K1 * DX1,Y1 + K1 * DY1)
这顺便说一句,如果你想知道的对称性,将相同的点
which incidentally, if you wonder about symmetry, will be the same point as
(2 + K2 * DX2,Y2 + K2 * DY2)
这些公式基本上与上How你发现,其中两条线段相交?除了编码从那里不一定是微不足道的或者是匆忙新手还是有人(像我一直自己很多次)。
These formulas are basically similar to the answer on How do you detect where two line segments intersect? except coding from there would not necessarily be trivial for either a newbie or someone in a rush (like I have been myself many times).
这篇关于如何查找是否将两个线段(不是两条直线)相交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!