python - 如何获取线系统的所有交叉点列表
问题描述
我有一个方程组 x=1,y=1,x+y=1,x+y=2。我怎样才能得到由上述方程形成的多边形的顶点。
解决方案
使用SymPy:
import itertools as IT
import sympy as sym
x, y = sym.symbols('x,y')
eqns = [x-1, y-1, x+y-1, x+y-2]
for pair in IT.combinations(eqns, 2):
soln = sym.solve(pair)
print('eqns: {} --> soln: {}'.format(pair, soln))
产量
eqns: (x - 1, y - 1) --> soln: {x: 1, y: 1}
eqns: (x - 1, x + y - 1) --> soln: {x: 1, y: 0}
eqns: (x - 1, x + y - 2) --> soln: {x: 1, y: 1}
eqns: (y - 1, x + y - 1) --> soln: {x: 0, y: 1}
eqns: (y - 1, x + y - 2) --> soln: {x: 1, y: 1}
eqns: (x + y - 1, x + y - 2) --> soln: []
如果您在定义多边形边缘的线上有点而不是方程式,那么您可以使用shapely来查找线的交点:
import itertools as IT
import shapely.geometry as SG
lines = [SG.LineString([(1,-10),(1,10)]),
SG.LineString([(-10, 1),(10, 1)]),
SG.LineString([(-10, 11),(10, -9)]),
SG.LineString([(-10, 12),(10, -8)])]
for line1, line2 in IT.combinations(lines, 2):
soln = line1.intersection(line2)
if isinstance(soln, SG.point.Point):
print('soln: {}'.format(list(soln.coords)))
产量
soln: [(1.0, 1.0)]
soln: [(1.0, 0.0)]
soln: [(1.0, 1.0)]
soln: [(0.0, 1.0)]
soln: [(1.0, 1.0)]