如何在Python中计算两条线的交点?

bol*_*t19 41 python geometry line intersect

我有两条线在一点交叉.我知道这两行的终点.如何计算Python中的交叉点?

# Given these endpoints
#line 1
A = [X, Y]
B = [X, Y]

#line 2
C = [X, Y]
D = [X, Y]

# Compute this:
point_of_intersection = [X, Y]
Run Code Online (Sandbox Code Playgroud)

roo*_*ook 58

不能袖手旁观

所以我们有线性系统:

A 1*x + B 1*y = C 1
A 2*x + B 2*y = C 2

让我们用Cramer的规则来做,因此可以在决定因素中找到解决方案:

x = D x/D
y = D y/D.

其中D是系统的主要决定因素:

A 1 B 1
A 2 B 2

D xD y可以从matricies找到:

C 1 B 1
C 2 B 2

A 1 C 1
A 2 C 2

(注意,因为C列因此取代了xy的系数列)

所以现在python,为了我们的清晰,不要搞砸了让我们在math和python之间进行映射.我们将使用阵列L存储我们coefs ,,Ç线方程和漂亮intestead x,y我们会有[0],[1]但无论如何.因此,我在上面写的内容将在代码中进一步具有以下形式:

D

L1 [0] L1 [1]
L2 [0] L2 [1]

对于D x

L1 [2] L1 [1]
L2 [2] L2 [1]

为了D y

L1 [0] L1 [2]
L2 [0] L2 [2]

现在去编码:

line- 通过提供的两个点产生线方程的系数A,B,C,
intersection- 找到coefs提供的两条线的交点(如果有的话).

from __future__ import division 

def line(p1, p2):
    A = (p1[1] - p2[1])
    B = (p2[0] - p1[0])
    C = (p1[0]*p2[1] - p2[0]*p1[1])
    return A, B, -C

def intersection(L1, L2):
    D  = L1[0] * L2[1] - L1[1] * L2[0]
    Dx = L1[2] * L2[1] - L1[1] * L2[2]
    Dy = L1[0] * L2[2] - L1[2] * L2[0]
    if D != 0:
        x = Dx / D
        y = Dy / D
        return x,y
    else:
        return False
Run Code Online (Sandbox Code Playgroud)

用法示例:

L1 = line([0,1], [2,3])
L2 = line([2,3], [0,4])

R = intersection(L1, L2)
if R:
    print "Intersection detected:", R
else:
    print "No single intersection point detected"
Run Code Online (Sandbox Code Playgroud)

  • @firelynx 我认为您将 **line** 一词与 **line segment** 混淆了。OP 要求线相交(故意或由于不理解差异)。在检查线的交叉点时,必须考虑到线是无限的,即从两个方向的中点(由定义它的两个点的给定坐标定义)开始的射线。在线段相交的情况下,仅检查给定点之间的直线部分是否相交,而忽略其无限延续。 (3认同)
  • 顺便说一句,重合线怎么样?使用上面的算法,它会为两条重合线返回“true”,这显然不能返回单个交点(因为从数学上讲,这种情况有无限多个交点)。我认为算法需要在单独的情况下处理这个问题,因为简单的相交和重合线是两种截然不同的情况。 (3认同)
  • 这个解决方案报告了线路COULD相交的交叉点,因为它们具有永久长度. (2认同)

Pau*_*per 47

与其他建议不同,这很简短,不使用外部库numpy.(并不是说使用其他库是坏的...它不是很好,特别是对于这么简单的问题.)

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:
       raise Exception('lines do not intersect')

    d = (det(*line1), det(*line2))
    x = det(d, xdiff) / div
    y = det(d, ydiff) / div
    return x, y

print line_intersection((A, B), (C, D))
Run Code Online (Sandbox Code Playgroud)

而且仅供参考,我会使用元组而不是列表来表示你的观点.例如

A = (X, Y)
Run Code Online (Sandbox Code Playgroud)

  • 这个解决方案产生(1.0,2.0)交叉`line_intersection(((0.5,0.5),(1.5,0.5)),((1,0),(1,2)))`,应该是(1,0.5) ). (4认同)
  • 我必须同意@xtofl-这行不通。我得到错误的肯定和否定。 (2认同)
  • Î 也将避免在此处使用异常。一个简单的 `False` 就可以了,它不像处理异常那样昂贵。 (2认同)

小智 20

这是使用Shapely库的解决方案。Shapely 通常用于 GIS 工作,但其构建用于计算几何。我将您的输入从列表更改为元组。

问题

# Given these endpoints
#line 1
A = (X, Y)
B = (X, Y)

#line 2
C = (X, Y)
D = (X, Y)

# Compute this:
point_of_intersection = (X, Y)
Run Code Online (Sandbox Code Playgroud)

解决方案

import shapely
from shapely.geometry import LineString, Point

line1 = LineString([A, B])
line2 = LineString([C, D])

int_pt = line1.intersection(line2)
point_of_intersection = int_pt.x, int_pt.y

print(point_of_intersection)
Run Code Online (Sandbox Code Playgroud)

  • 重要的是要注意,此解决方案仅在定义的端点之间存在交集时才有效,因为 shapely 仅查找线段的交集,而不是相应的无限线的交集。 (5认同)

Kik*_*ell 7

我在网上没有找到直观的解释,所以现在我解决了,这是我的解决方案。这是针对无限线(我需要的),而不是线段。

您可能还记得一些术语:

直线定义为 y = mx + b OR y = 斜率 * x + y 截距

斜率 = 上升超过运行 = dy / dx = 高度 / 距离

Y 截距是线与 Y 轴相交的位置,其中 X = 0

鉴于这些定义,以下是一些函数:

def slope(P1, P2):
    # dy/dx
    # (y2 - y1) / (x2 - x1)
    return(P2[1] - P1[1]) / (P2[0] - P1[0])

def y_intercept(P1, slope):
    # y = mx + b
    # b = y - mx
    # b = P1[1] - slope * P1[0]
    return P1[1] - slope * P1[0]

def line_intersect(m1, b1, m2, b2):
    if m1 == m2:
        print ("These lines are parallel!!!")
        return None
    # y = mx + b
    # Set both lines equal to find the intersection point in the x direction
    # m1 * x + b1 = m2 * x + b2
    # m1 * x - m2 * x = b2 - b1
    # x * (m1 - m2) = b2 - b1
    # x = (b2 - b1) / (m1 - m2)
    x = (b2 - b1) / (m1 - m2)
    # Now solve for y -- use either line, because they are equal here
    # y = mx + b
    y = m1 * x + b1
    return x,y
Run Code Online (Sandbox Code Playgroud)

这是两条(无限)线之间的简单测试:

A1 = [1,1]
A2 = [3,3]
B1 = [1,3]
B2 = [3,1]
slope_A = slope(A1, A2)
slope_B = slope(B1, B2)
y_int_A = y_intercept(A1, slope_A)
y_int_B = y_intercept(B1, slope_B)
print(line_intersect(slope_A, y_int_A, slope_B, y_int_B))
Run Code Online (Sandbox Code Playgroud)

输出:

(2.0, 2.0)
Run Code Online (Sandbox Code Playgroud)


Gab*_*Eng 6

使用以下公式:https : //en.wikipedia.org/wiki/Line%E2%80%93line_intersection

 def findIntersection(x1,y1,x2,y2,x3,y3,x4,y4):
        px= ( (x1*y2-y1*x2)*(x3-x4)-(x1-x2)*(x3*y4-y3*x4) ) / ( (x1-x2)*(y3-y4)-(y1-y2)*(x3-x4) ) 
        py= ( (x1*y2-y1*x2)*(y3-y4)-(y1-y2)*(x3*y4-y3*x4) ) / ( (x1-x2)*(y3-y4)-(y1-y2)*(x3-x4) )
        return [px, py]
Run Code Online (Sandbox Code Playgroud)


Pau*_*hen 6

如果您的线条是多个点,则可以使用此版本。

在此处输入图片说明

import numpy as np
import matplotlib.pyplot as plt
"""
Sukhbinder
5 April 2017
Based on:    
"""

def _rect_inter_inner(x1,x2):
    n1=x1.shape[0]-1
    n2=x2.shape[0]-1
    X1=np.c_[x1[:-1],x1[1:]]
    X2=np.c_[x2[:-1],x2[1:]]    
    S1=np.tile(X1.min(axis=1),(n2,1)).T
    S2=np.tile(X2.max(axis=1),(n1,1))
    S3=np.tile(X1.max(axis=1),(n2,1)).T
    S4=np.tile(X2.min(axis=1),(n1,1))
    return S1,S2,S3,S4

def _rectangle_intersection_(x1,y1,x2,y2):
    S1,S2,S3,S4=_rect_inter_inner(x1,x2)
    S5,S6,S7,S8=_rect_inter_inner(y1,y2)

    C1=np.less_equal(S1,S2)
    C2=np.greater_equal(S3,S4)
    C3=np.less_equal(S5,S6)
    C4=np.greater_equal(S7,S8)

    ii,jj=np.nonzero(C1 & C2 & C3 & C4)
    return ii,jj

def intersection(x1,y1,x2,y2):
    """
INTERSECTIONS Intersections of curves.
   Computes the (x,y) locations where two curves intersect.  The curves
   can be broken with NaNs or have vertical segments.
usage:
x,y=intersection(x1,y1,x2,y2)
    Example:
    a, b = 1, 2
    phi = np.linspace(3, 10, 100)
    x1 = a*phi - b*np.sin(phi)
    y1 = a - b*np.cos(phi)
    x2=phi    
    y2=np.sin(phi)+2
    x,y=intersection(x1,y1,x2,y2)
    plt.plot(x1,y1,c='r')
    plt.plot(x2,y2,c='g')
    plt.plot(x,y,'*k')
    plt.show()
    """
    ii,jj=_rectangle_intersection_(x1,y1,x2,y2)
    n=len(ii)

    dxy1=np.diff(np.c_[x1,y1],axis=0)
    dxy2=np.diff(np.c_[x2,y2],axis=0)

    T=np.zeros((4,n))
    AA=np.zeros((4,4,n))
    AA[0:2,2,:]=-1
    AA[2:4,3,:]=-1
    AA[0::2,0,:]=dxy1[ii,:].T
    AA[1::2,1,:]=dxy2[jj,:].T

    BB=np.zeros((4,n))
    BB[0,:]=-x1[ii].ravel()
    BB[1,:]=-x2[jj].ravel()
    BB[2,:]=-y1[ii].ravel()
    BB[3,:]=-y2[jj].ravel()

    for i in range(n):
        try:
            T[:,i]=np.linalg.solve(AA[:,:,i],BB[:,i])
        except:
            T[:,i]=np.NaN


    in_range= (T[0,:] >=0) & (T[1,:] >=0) & (T[0,:] <=1) & (T[1,:] <=1)

    xy0=T[2:,in_range]
    xy0=xy0.T
    return xy0[:,0],xy0[:,1]


if __name__ == '__main__':

    # a piece of a prolate cycloid, and am going to find
    a, b = 1, 2
    phi = np.linspace(3, 10, 100)
    x1 = a*phi - b*np.sin(phi)
    y1 = a - b*np.cos(phi)

    x2=phi
    y2=np.sin(phi)+2
    x,y=intersection(x1,y1,x2,y2)
    plt.plot(x1,y1,c='r')
    plt.plot(x2,y2,c='g')
    plt.plot(x,y,'*k')
    plt.show()
Run Code Online (Sandbox Code Playgroud)


jar*_*mag 6

我发现的最简洁的解决方案使用 Sympy:https ://www.geeksforgeeks.org/python-sympy-line-intersection-method/

# import sympy and Point, Line 
from sympy import Point, Line 
  
p1, p2, p3 = Point(0, 0), Point(1, 1), Point(7, 7) 
l1 = Line(p1, p2) 
  
# using intersection() method 
showIntersection = l1.intersection(p3) 
  
print(showIntersection) 
Run Code Online (Sandbox Code Playgroud)