-1在Scipy的voronoi算法中意味着什么?

时间:2016-09-13 17:46:22

标签: python graphics scipy computational-geometry voronoi

我试图在2D

中自定义绘制随机点的Voronoi区域
import matplotlib.pyplot as plt
%matplotlib inline

from scipy.spatial import Voronoi
pt = np.random.random((10,2))
x = sp.spatial.Voronoi(pt)

# trial an error to figure out the type structure of [x]

plt.plot(x.vertices[:,0], x.vertices[:,1], '.', markersize=5)

# how to iterate through the x.regions object?
for poly in x.regions:
    z = np.array([ x.vertices[k] for k in poly])
    print z
    if z.shape[0] > 0:
        plt.plot( z[:,0], z[:,1])

plt.xlim([0,2])
plt.ylim([0,2])

为什么这些地区重叠?有关绘制无限区域的建议吗?

enter image description here

数据点只是随机数:

x.vertices

array([[ 0.59851675,  0.15271572],
       [ 0.24473753,  0.70398382],
       [ 0.10135325,  0.34601724],
       [ 0.42672008,  0.26129443],
       [ 0.54966835,  1.64315275],
       [ 0.24770706,  0.70543002],
       [ 0.39509645,  0.64211128],
       [ 0.63353948,  0.86992423],
       [ 0.57476256,  1.4533911 ],
       [ 0.76421296,  0.6054079 ],
       [ 0.9564816 ,  0.79492684],
       [ 0.94432943,  0.62496293]])

区域按编号列出

x.regions

[[],
 [2, -1, 1],
 [3, 0, -1, 2],
 [5, 1, -1, 4],
 [6, 3, 2, 1, 5],
 [11, 9, 7, 8, 10],
 [8, 4, 5, 6, 7],
 [9, 0, 3, 6, 7],
 [10, -1, 4, 8],
 [11, -1, 0, 9],
 [11, -1, 10]]

从中我们可以重新构建情节。我的问题是-1是什么意思?

1 个答案:

答案 0 :(得分:3)

scipy.spatial.Voronoi使用下面的Qhull库。根据我的经验,Qhull包含几个可用性错误。你点击了one of them

  

qvoronoi输出

     

Voronoi顶点

     

[...]

     

FN:列出每个Voronoi区域的Voronoi顶点。第一行   是Voronoi地区的数量。剩下的每一行都以   Voronoi顶点的数量。 负指数(例如-1)表示   Voronoi图之外的顶点。

  

为什么这些地区重叠?

因此,来自-1的第一个Voronoi区域[2, -1, 1]中的x.regions代表无限顶点未表示在 x.vertices)。然而,当您使用该虚假索引访问x.vertices时,您将获得最后一个顶点。对于-1中的每个x.regions都会发生这种情况(请注意,这些-1表示不同的顶点 - 无穷大)。结果,您得到虚假的Voronoi边连接到x.vertices的最后一个顶点。

  

有关绘制无限区域的建议吗?

为什么不简单地使用scipy.spatial.voronoi_plot_2d()

相关问题