Python 由点指定的两条无限直线的交点

Python 由点指定的两条无限直线的交点,python,python-3.x,geometry,linear-algebra,Python,Python 3.x,Geometry,Linear Algebra,这里已经有很多类似的问题被问到和回答了,但是在浏览它们之后,没有一个完全解决了我的问题。我正在寻找一个可靠的算法,以找到两个点指定的两条无限线的交点。就我而言,有两个并发症: 不能保证交点位于指定每条线的两个顶点之间(这意味着类似的解决方案不起作用),并且 直线可以在二维平面上任意定向,这意味着基于斜率和截距的解决方案并不总是有效(因为其中一条直线可以垂直定向,从而产生无限的斜率和截距) 我目前的方法说明了基于坡度和拦截的方法的缺点。这个问题可以通过实现一个旋转整个系统的步骤来部分地避免,这样就

这里已经有很多类似的问题被问到和回答了,但是在浏览它们之后,没有一个完全解决了我的问题。我正在寻找一个可靠的算法,以找到两个点指定的两条无限线的交点。就我而言,有两个并发症:

  • 不能保证交点位于指定每条线的两个顶点之间(这意味着类似的解决方案不起作用),并且
  • 直线可以在二维平面上任意定向,这意味着基于斜率和截距的解决方案并不总是有效(因为其中一条直线可以垂直定向,从而产生无限的斜率和截距)
  • 我目前的方法说明了基于坡度和拦截的方法的缺点。这个问题可以通过实现一个旋转整个系统的步骤来部分地避免,这样就没有直线是垂直的,但是这看起来不是很优雅。你知道更好的方法吗

    import numpy as np
    
    # The intersection point of the example below should be (0,0)
    
    # Vertices for the first line
    p1_start    = np.asarray([-5,   0])
    p1_end      = np.asarray([-3,   0])
    
    # Vertices for the second line
    p2_start    = np.asarray([0,    4])
    p2_end      = np.asarray([0,    2])
    
    # Calculate slope and intercept for the first line
    m_1         = (p1_end[1]-p1_start[1])/(p1_end[0]-p1_start[0])
    t_1         = p1_start[1] - m_1*p1_start[0]
    
    # The slope and intercept are zero
    print('First line')
    print('slope = '+str(m_1))
    print('intercept = '+str(t_1))
    
    # Calculate slope and intercept for the second line
    m_2         = (p2_end[1]-p2_start[1])/(p2_end[0]-p2_start[0])
    t_2         = p2_start[1] - m_2*p2_start[0]
    
    # The slope and intercept are infinite
    print('Second line')
    print('slope = '+str(m_2))
    print('intercept = '+str(t_2))
    
    # Find out where these points interset
    # Doesn't work if one of the slopes is infinite
    intersection_point_x = (t_2-t_1)/(m_1-m_2)
    intersection_point_y = intersection_point_x*m_1 + t_1
    print('Intersection point')
    print(intersection_point_x)
    print(intersection_point_y)
    
    在下面的解决方案中,我将his中报告的解决方案改编为一个简短的Python代码片段:

    import numpy as np
    
    # The intersection point of the example below should be (0,0)
    
    # Vertices for the first line
    p1_start    = np.asarray([-5,   0])
    p1_end      = np.asarray([-3,   0])
    
    # Vertices for the second line
    p2_start    = np.asarray([0,    4])
    p2_end      = np.asarray([0,    2])
    
    p       = p1_start
    r       = (p1_end-p1_start)
    
    q       = p2_start
    s       = (p2_end-p2_start)
    
    t       = np.cross(q - p,s)/(np.cross(r,s))
    
    # This is the intersection point
    i       = p + t*r
    

    为什么不使用sympy几何模块。 下面是代码

    import sympy as sy
    import sympy.geometry as gm
    sy.init_printing()
    line1=gm.Line(gm.Point(1,1),gm.Point(8,5)) #Line1
    line2=gm.Line(gm.Point(30,4),gm.Point(35,-4)) #Line2
     #These are two infinite lines defined by two points on the line
    intersection=line1.intersection(line2)
    print(intersection[0].evalf())
    

    你确定e.g.不会起作用吗?恐怕是的。此线程中的解决方案似乎仅适用于线段,即有限线。在我的例子中,点指定的直线是无限的,因此交点可能位于点跨越的线段之外。答案是“如果r×s≠ 0和0≤ T≤ 1和0≤ U≤ 1”–卸下0≤ T≤ 1和0≤ U≤ 1条件应该使该解适用于无限长的直线。啊,是的,这似乎是可行的。非常感谢。我已经更新了上面的问题。请不要用答案改变问题;如果您愿意,您可以在为答案保留的空间中发布您自己问题的答案。我将你的帖子退回到原来的问题。