如何计算两条直线的交点?

我有两条线在同一点相交。我知道这两条线的终点。如何在 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]
196729 次浏览

与其他建议不同的是,这个建议很短,而且不使用像 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))

顺便说一下,我会用元组代替列表来表达你的观点。

A = (X, Y)

编辑: 最初是一个打印错误。这是2014年9月 修好了感谢@zidik。

这只是下面公式的 Python 音译,其中的行是(答1答2)和(B1B2) ,交点是 P。(如果分母为零,则两条直线没有唯一的交点。)

不能袖手旁观,

所以我们有线性系统:

A1 * x + B1 * y = C1
A2 * x + B2 * y = C < sub > 2

我们用克莱默定律,这样就可以在行列式中找到解:

X = DX/D
Y = D/D

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

A1
A2

以及 D < sub > x D < sub > y 可以从矩阵中找到:

C1B1C1C1C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0C0
C2B < sub > 2

还有

A1 C1
A2 C < sub > 2

(注意,由于 C列因此取代了 X的 coe.column)

现在,为了让我们更清楚地了解 Python,为了不把事情搞砸,我们来做一下数学和 python 之间的映射。我们将使用数组 L来存储我们的首席 ABC的线性方程组和可爱的 xy我们将有 [0][1],但无论如何。因此,我上面所写的代码将进一步采用以下形式:

D

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

D < sub > x

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

D < sub > y

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

现在开始编码:

根据提供的两点生成线性方程式 ABC,
intersection-查找由 coef 提供的两条线的交点(如果有的话)。

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

用法例子:

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"

我没有在网上找到一个直观的解释,所以现在我解决了这个问题,这是我的解决方案。这是用于无限行(我需要的) ,而不是段。

一些你可能记得的术语:

直线定义为 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

下面是两行(无限行)之间的一个简单测试:

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))

产出:

(2.0, 2.0)

使用的公式来自: 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]

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

enter image description here

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()

下面是使用 很好库的解决方案。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)

解决方案

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)

我发现的最简洁的解决方案使用 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)

使用 Scikit 空间库,你可以通过以下方式轻松实现:

import matplotlib.pyplot as plt


from skspatial.objects import Line


# Define the two lines.
line_1 = Line.from_points([3, -2], [5, 4])
line_2 = Line.from_points([-1, 0], [3, 2])


# Compute the intersection point
intersection_point = line_1.intersect_line(line_2)


# Plot
_, ax = plt.subplots()
line_1.plot_2d(ax, t_1=-2, t_2=3, c="k")
line_2.plot_2d(ax, t_1=-2, t_2=3, c="k")
intersection_point.plot_2d(ax, c="r", s=100)
grid = ax.grid()

enter image description here

已经有一个 回答使用了来自 维基百科的公式,但是它没有任何检查点来检查线段是否真的相交,所以这里

def line_intersection(a, b, c, d):
t = ((a[0] - c[0]) * (c[1] - d[1]) - (a[1] - c[1]) * (c[0] - d[0])) / ((a[0] - b[0]) * (c[1] - d[1]) - (a[1] - b[1]) * (c[0] - d[0]))
u = ((a[0] - c[0]) * (a[1] - b[1]) - (a[1] - c[1]) * (a[0] - b[0])) / ((a[0] - b[0]) * (c[1] - d[1]) - (a[1] - b[1]) * (c[0] - d[0]))


# check if line actually intersect
if (0 <= t and t <= 1 and 0 <= u and u <= 1):
return [a[0] + t * (b[0] - a[0]), a[1] + t * (b[1] - a[1])]
else:
return False
    

#usage
print(line_intersection([0,0], [10, 10], [0, 10], [10,0]))


#result [5.0, 5.0]