问题描述
我正在使用 scipy.spatial.Voronoi 函数.我使用点的随机二维分布(参见下面的 MCVE).
I'm generating a simple 2D Voronoi tessellation, using the scipy.spatial.Voronoi function. I use a random 2D distribution of points (see MCVE below).
我需要一种方法来遍历每个定义的区域(由 scipy.spatial.Voronoi
定义)并获得与其关联的点的坐标(即:所述区域包围的点).
I need a way to go through each defined region (defined by scipy.spatial.Voronoi
) and get the coordinates of the point associated to it (ie: the point that said region encloses).
问题是为 N
点定义了 N+1
个区域(多边形),我不确定这意味着什么.
The issue is that there are N+1
regions (polygons) defined for the N
points, and I'm not sure what this means.
这是一个 MCVE,当它到达最后一个区域时会失败:
Here's a MCVE that will fail when it gets to the last region:
from scipy.spatial import Voronoi
import numpy as np
# Generate random data.
N = 10
x = [np.random.random() for i in xrange(N)]
y = [np.random.random() for i in xrange(N)]
points = zip(x, y)
# Obtain Voronoi regions.
vor = Voronoi(points)
# Loop through each defined region/polygon
for i, reg in enumerate(vor.regions):
print 'Region:', i
print 'Indices of vertices of Voronoi region:', reg
print 'Associated point:', points[i], '\n'
我不明白的另一件事是为什么存储了空的 vor.regions
?根据文档:
Another thing I don't understand is why are there empty vor.regions
stored? According to the docs:
regions:形成每个 Voronoi 区域的 Voronoi 顶点的索引.-1 表示在 Voronoi 图之外的顶点.
空白区域是什么意思?
添加
我尝试了 point_region
属性,但显然我不明白它是如何工作的.它返回 points
列表范围之外的索引.例如:在上面的 MCVE 中,对于 10 个点的列表,它总是会显示一个索引 10
,这显然超出了范围.
I tried the point_region
attribute but apparently I don't understand how it works. It returns indexes outside of the range of the points
list. For example: in the MCVE above it will always show an index 10
for a list of 10 points, which is clearly out of range.
推荐答案
对于您的第一个问题:
问题是为 N 个点定义了 N+1 个区域(多边形),我不确定这意味着什么.
这是因为您的 vor.regions 将始终有一个空数组.类似的东西
This is because your vor.regions will always have an empty array. Something like
[[],[0, 0],[0, 1],[1, 1]]
这与你的第二个问题有关:
This is related to your second question:
我不明白的另一件事是为什么存储了空的 vor.regions?根据文档:区域:形成每个 Voronoi 区域的 Voronoi 顶点的索引.-1 表示 Voronoi 图之外的顶点.空白区域是什么意思?
默认情况下,Voronoi() 使用 QHull 并启用选项 'Qbb Qc Qz Qx' (qhull.org/html/qvoronoi.htm).这会插入一个无穷大点",用于提高循环输入的精度.因此,作为假"点,它没有区域.如果您想摆脱这种情况,请尝试删除 Qz 选项:
By default Voronoi() uses QHull with options 'Qbb Qc Qz Qx' enabled (qhull.org/html/qvoronoi.htm). This inserts a "point-at-infinity" which is used to improve precision on circular inputs. Therefore, being a "fake" point, it has no region. If you want to get rid of this, try removing the Qz option:
vor = Voronoi(points, qhull_options='Qbb Qc Qx')
这篇关于获取与 Voronoi 区域相关的点 (scipy.spatial.Voronoi)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!