越线检测程序不起作用

Line crossing detection program does not work

我有这种类型的代码:

def line_intersection(line1, line2):
    xdiff = (line1[0][0] - line1[1][0], line2[0][0] - line2[1][0])
    ydiff = (line1[0][1] - line1[1][1], line2[0][1] - line2[1][1])
​
    def det(a, b):
        return a[0] * b[1] - a[1] * b[0]
​
    div = det(xdiff, ydiff)
    if div == 0:
        return False
    else:
        return True

但是当我尝试使用这些参数运行这个函数时

line1 = [(100, 200), (390, 286)]
line2 = [(120, 256),
 (166.05304970014424, 275.47091711543254)]
​intrsct = line_intersection(line1, line2)

我得到 True,但是这些线没有相互交叉。哪里错了?

看起来你正在使用这个 site 的方程式。这将 return False 如果线是平行的。在你的情况下,它们不是,因此你得到 True.

您的代码将确定这两条线是否会交叉,假设它们在两个方向上无限延伸。确定两条线段是否会交叉是无用的。那是更多的工作。参见 https://geeksforgeeks.org/check-if-two-given-line-segments-intersect