我有这段代码可以绘制多条线(图1)。如何找到这些线的交点?

import matplotlib.pylab as pyl
import numpy as np
import math

x = [200, 300, 300, 200,200]
y = [150, 150, 100, 100,140]
x1 = [100, 400]
y1 = [50, 250]

pyl.plot(x, y, 'r')
pyl.plot(x1, y1, 'c')

pyl.xlim(0, 480)
pyl.ylim(0, 320)
pyl.grid(True)
pyl.show()


python - 如何在matplotlib图中找到多条线的交点-LMLPHP

最佳答案

我喜欢Shapely这类问题

from shapely.geometry import LineString, Polygon

x = [200, 300, 300, 200, 200]
y = [150, 150, 100, 100, 140]

x1, x2 = [100, 400]
y1, y2 = [50, 250]

poly = Polygon([(x, y) for x, y in zip(x, y)])
line = LineString([(x1, y1), (x2, y2)])

cross = poly.intersection(line)
[px1, px2], [py1, py2] = cross.coords.xy
p1 = (px1, py1) # (200, 116,66)
p2 = (px2, py2) # (250, 150)

关于python - 如何在matplotlib图中找到多条线的交点,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57728338/

10-12 18:50