我希望找到一条线与多边形相交的点。我使用这个thread中提供的凹多边形轮廓计算方法得到了该多边形。
但是
import alphashape
from shapely.geometry import LineString
import matplotlib.pyplot as plt
from descartes import PolygonPatch
points = [(17, 158),(15, 135),(38, 183),(43, 19),(93, 88),(96, 140),(149, 163),(128, 248),(216, 265),(248, 210),(223, 167),(256, 151),(331, 214),(340, 187),(316, 53),(298, 35),(182, 0),(121, 42)]
points = np.array(points)
alpha = 0.99 * alphashape.optimizealpha(points)
hull = alphashape.alphashape(points, alpha)
hull_pts = hull.exterior.coords.xy
path = PolygonPatch(hull, fill=False, color='green')
print(path.contains_point([128,248]))
fig, ax = plt.subplots()
ax.scatter(hull_pts[0], hull_pts[1], color='red')
ax.scatter(points[:,0], points[:,1], color='red')
p = np.array([[350, 100],[0, 100]])
ax.plot(p[:, 0], p[:, 1], color='blue')
ax.add_patch(path)
到目前为止,我尝试使用以下方式定义一条线:
l = LineString(p)
inters = l.intersection(hull)
但是
inters.xy
会返回一个NotImplemented错误,所以我不确定如何获得线与多边形相交的点的坐标。
y=mx+n
的方程以及每个线段的方程。将这些方程组合起来。平行线没有解。对于有解的线段,您需要检查解是否落在形成该线段的两个点之间。 - user2261062