雖然這是一個古老的問題,但我在尋找相同的東西,並認爲答案可能對某人有所幫助。可以使用scipy
模塊中的Delaunay
。
from scipy.spatial import Delaunay
from collections import defaultdict
import itertools
points=[[0.0, 0.0], [0.0, 1.0], [0.2, 0.5], [0.3, 0.6], [0.4, 0.5], [0.6, 0.3], [0.6, 0.5], [1.0, 0.0], [1.0, 1.0]]
tri = Delaunay(points)
neiList=defaultdict(set)
for p in tri.vertices:
for i,j in itertools.combinations(p,2):
neiList[i].add(j)
neiList[j].add(i)
for key in sorted(neiList.iterkeys()):
print("%d:%s" % (key,','.join([str(i) for i in neiList[key]])))
0:1,2,5,7
1:0,8,2,3
2:0,1,3,4,5
3:8,1,2,4,6
4:2,3,5,6
5:0,2,4,6,7
6:8,3,4,5,7
7:8,0,5,6
8:1,3,6,7
# This is for visualization
from scipy.spatial import Voronoi, voronoi_plot_2d
import matplotlib.pyplot as plt
vor = Voronoi(points)
voronoi_plot_2d(vor)
for i,p in enumerate(x):
plt.text(p[0], p[1], '#%d' % i, ha='center')
plt.show()
有用。值得強調的是,這種voronoi圖的可視化在界限上可能會產生誤導。例如。節點#0與#1和#7相鄰,但情節沒有顯示。 – Maptopixel 2017-11-08 12:55:15