gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 22:50:13 25 4
gpt4 key购买 nike

我正在尝试自定义绘制 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 是什么意思?

最佳答案

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

qvoronoi outputs

Voronoi vertices

[...]

FN: list the Voronoi vertices for each Voronoi region. The first line is the number of Voronoi regions. Each remaining line starts with the number of Voronoi vertices. Negative indices (e.g., -1) indicate vertices outside of the Voronoi diagram.

<小时/><小时/>

why do the regions overlap?

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

any advice for plotting the infinite regions?

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

关于python - Scipy voronoi 算法中的 -1 意味着什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39476094/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com