我是编码的新手。我试图做一些Voronoi分析,所以我需要存储每个多边形区域及其邻居。我寻找每个多边形顶点,如果它在其他多边形中,那么我可以将它们定义为邻居并计算其面积或只是传递给下一个。我执行以下代码。
import numpy as np
from scipy.spatial import Voronoi, voronoi_plot_2d
import matplotlib.pyplot as plt
inst=np.zeros((360,2))
inst1=-0.5+np.random.random((300,2))
inst1[:,:2]*=3.9
inst1[:,:2]+=4
inst2=4+0.5*np.random.random((30,2))
inst3=5+0.2*np.random.random((30,2))
for i in range(len(inst1)):
inst[i]=inst1[i]
for i in range(len(inst2)):
inst[len(inst1)+i]=inst2[i]
inst[len(inst1)+len(inst2)+i]=inst3[i]
vor=Voronoi(inst[:,:2])
reg=vor.regions
poly_v0=[]
poly_vf=[]
for region in reg:
if not -1 in region:
poly_v0=[vor.vertices[i] for i in region]
poly_vf.append(poly_v0)
vor_surf=[[]]
punto=[]
for i in range(len(poly_vf)):
a=np.zeros((len(poly_vf[i]),2))
for j in range(len(a[:,0])):
a[j]=poly_vf[i][j]
vor_surf.append(0.5*np.abs(np.dot(a[:,0],np.roll(a[:,1],1))-np.dot(a[:,1],
np.roll(a[:,0],1))))
for vertices in poly_vf:
for punto in vertices:
for n in range(len(poly_vf)-1):
z=punto in poly_vf[n]
if punto in poly_vf[n].any():
b=np.zeros((len(poly_vf[n]),2))
for k in range(len(b[:,0])):
b[k]=poly_vf[n][k]
vor_surf[i].append(0.5*np.abs(np.dot(b[:,0],np.roll(b[:,1],1))-np.dot(b[:,1],np.roll(b[:,0],1))))
voronoi_plot_2d(vor, show_vertices=False)
plt.plot(inst[:,0], inst[:,1], 'o', ms=3)
plt.show()
Traceback (most recent call last):
File "C:\Users\User\Desktop\Python\Voronoi-voisin.py", line 45, in <module>
z=punto in poly_vf[n+1]
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()