两个平行线段相交

时间:2010-05-11 02:49:13

标签: geometry

我知道有很多算法可以验证两个线段是否相交。

我所说的线段是由2个端点构成的长度线。

但是一旦他们遇到并行状态,他们只会告诉用户一个大的“否”和
假装没有重叠,共享终点或终点串通。

我知道我可以计算出2个线段之间的距离 如果距离为0,请检查位于其他线段中的端点 这意味着我必须使用很多if else和&& ||条件。

这并不困难,但我的问题是

“是否有一种技巧(或数学)方法来计算这种特殊的并行情况?”

我希望这张照片澄清我的问题 http://judark.myweb.hinet.net/parallel.JPG

5 个答案:

答案 0 :(得分:4)

是的,给定两条线的公式,测试它们的斜率是否相等。如果是,那么这些线是平行的,永远不会相交。

如果每条线上都有分数,则可以使用slope formula

如果两者都垂直于x轴,它们都将具有无限斜率,但它们将是平行的。每行上的所有点都具有相等的x坐标。

要处理线段,计算交点,然后确定两个线段的交点是否存在。

答案 1 :(得分:0)

我假设您感兴趣的情况是两个线段平行的位置(通过检查斜率确定,如Whirlwind所说),并且您正在尝试确定这两个线段是否重叠。

我认为最简单的方法是,如果一个网段的任一端点位于另一个网段中,那么最简单的方法就不是担心线条之间的距离了:

if (segment_contains_point(segment_A, segment_B.start) || 
    segment_contains_point(segment_A, segment_B.end)) {
        // lines overlap
}

答案 2 :(得分:0)

假设您有两条由公式a.x + b.y + c = 0d.x + e.y + f = 0描述的线条。 a = 0 and d = 0b/a = e/d时,这两行是平行的。或许不要进行划分,只需确保b.d = a.e

答案 3 :(得分:0)

我发现了这个(我修改了一下以适应) 它将返回intercetion x,y else如果没有发现intercetion它将返回-1,-1

    Public Function intercetion(ByVal ax As Integer, ByVal ay As Integer, ByVal bx As Integer, ByVal by As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal dx As Integer, ByVal dy As Integer) As Point
    '//  Determines the intersection point of the line segment defined by points A and B
    '//  with the line segment defined by points C and D.
    '//
    '//  Returns YES if the intersection point was found, and stores that point in X,Y.
    '//  Returns NO if there is no determinable intersection point, in which case X,Y will
    '//  be unmodified.

    Dim distAB, theCos, theSin, newX, ABpos As Double

    '//  Fail if either line segment is zero-length.
    If ax = bx And ay = by Or cx = dx And cy = dy Then Return New Point(-1, -1)

    '//  Fail if the segments share an end-point.
    If ax = cx And ay = cy Or bx = cx And by = cy Or ax = dx And ay = dy Or bx = dx And by = dy Then Return New Point(-1, -1)

    '//  (1) Translate the system so that point A is on the origin.
    bx -= ax
    by -= ay
    cx -= ax
    cy -= ay
    dx -= ax
    dy -= ay

    '//  Discover the length of segment A-B.
    distAB = Math.Sqrt(bx * bx + by * by)

    '//  (2) Rotate the system so that point B is on the positive X axis.
    theCos = bx / distAB
    theSin = by / distAB
    newX = cx * theCos + cy * theSin
    cy = cy * theCos - cx * theSin
    cx = newX
    newX = dx * theCos + dy * theSin
    dy = dy * theCos - dx * theSin
    dx = newX

    '//  Fail if segment C-D doesn't cross line A-B.
    If cy < 0 And dy < 0 Or cy >= 0 And dy >= 0 Then Return New Point(-1, -1)

    '//  (3) Discover the position of the intersection point along line A-B.
    ABpos = dx + (cx - dx) * dy / (dy - cy)

    '//  Fail if segment C-D crosses line A-B outside of segment A-B.
    If ABpos < 0 Or ABpos > distAB Then Return New Point(-1, -1)

    '//  (4) Apply the discovered position to line A-B in the original coordinate system.
    '*X=Ax+ABpos*theCos
    '*Y=Ay+ABpos*theSin

    '//  Success.
    Return New Point(ax + ABpos * theCos, ay + ABpos * theSin)
End Function

Origin

答案 4 :(得分:0)

我遇到了同样的问题:我想出的最简单的方法是检查线条是否重叠: 假设段是共线的(平行且与x轴具有相同的交点)。 从较长的段(A,B)中取一个点A作为起始点。现在找到距离点A的最小距离的其他三个点(平方距离更好,甚至曼哈顿长度也可以工作)测量B方向的距离。如果距离A的最近点是B,则线不相交。如果它属于他们所做的其他部分。 也许你必须检查特殊情况,如零长度线或相同的线,但这应该很容易。