for (all cells in voronoi diagram)
for (all edges in current cell)
if (matching edge found in hash table)
// the current cell is adjacent to the cell that added
// the matching edge segment to the hash table
else
// push current edge segment onto hash table and mark with
// current cell index
endif
endfor
endfor
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图,您可以在单元之间查找共享的边段。如果发现两个单元共享Voronoi边段,则表示它们相邻。建立整个数据集的邻接信息的一种有效方法是通过扫描Voronoi单元列表来建立边缘哈希表。在
有许多很好的现有软件包可用于计算点集的Voronoi图/Delaunay三角剖分。由于这是一个计算开销大且对数字敏感的操作,我建议使用现有的库。Triangle和{a2}包被广泛使用。在
希望这有帮助。在
一个可能的算法是可用的here 它使用线性规划方法。在
PuLP可以生成MPS或LP文件并调用GLPK、COIN、CPLEX、和{a5}来解决线性问题。在
PuLP是一个用Python编写的LP建模器,可以用来在Python中建模这个线性程序,然后使用GLPK进行求解。在
虽然这是一个老问题,但我一直在寻找同样的答案,并认为这个答案对某些人还是有帮助的。可以从
scipy
模块使用Delaunay
。在相关问题 更多 >
编程相关推荐