所以我正在研究一个视线脚本,其中多边形充当建筑物并根据一条线是否与多边形相交返回一个布尔值。
它背后的逻辑是有效的,但是当我尝试从名为polycoords的列表中集成更多多边形并使用 For 循环以便它可以在 Graph 上实例化它时,它会作为两个单独的图而不是一个单独的图出现。
import numpy as np
import matplotlib.pyplot as plt
import shapely.geometry
from shapely.geometry import LineString
from shapely.geometry import Point, Polygon
import descartes
origin = [-1.0, 0.0] # Set a point to view from
quality = 7 # Number of grid points squared
polycoords = [[[-1, 1], [-1, 0.5], [0, 0.5], [0, 1]],[[1, -1], [1, -0.5], [0, -0.5], [-0, -1]]] # Coordinates of the Polygon
fullresults = []
newbool = []
def los (origin, quality, polycoords):
for buildingpoints in range(len(polycoords)):
x = np.linspace(-1,1,quality)
y = np.linspace(-1,1,quality)
X,Y = np.meshgrid(x,y)
clip_poly = shapely.geometry.Polygon(polycoords[buildingpoints])
fig = plt.figure()
ax = fig.add_subplot(111)
polygonbuilding = ax.add_patch(descartes.PolygonPatch(clip_poly, fc='pink', alpha=0.3))
positions = np.vstack([Y.ravel(), X.ravel()])
for i in range(len(positions)):
for j in range(len(positions[i])):
plt.scatter(*positions[::-1])
x1 = positions[0][j]
y1 = positions[1][j]
line = LineString([origin, (x1, y1)])
if line.intersects(clip_poly) == True:
ax.plot(*np.array(line).T, color='red', linewidth=1, solid_capstyle='round')
else:
ax.plot(*np.array(line).T, color='green', linewidth=1, solid_capstyle='round')
boolintersect = line.intersects(clip_poly)
listresults = origin, [x1,y1],boolintersect
fullresults.append(listresults)
boollist = [x[2] for x in fullresults]
newbool.append(sum(boollist))
return(fullresults, newbool)
def analysis (losresults):
percenteq = round((100-(newbool[-1]/len(fullresults))*100))
print (percenteq,'% of the space is PUBLICALLY visible from point', fullresults[0][0])
print (100-percenteq,'% of the space is PRIVATE/ISNT visible from point', fullresults[0][0])
los(origin, quality, polycoords)
analysis (los)
plt.show()
当我只需要一个同时存在两个多边形时,结果会显示两个单独的图表。我相信这与我关于 For 循环的代码结构有关,但我仍然很新,不太确定如何解决这个问题。