[英]How can I find the seeds of the voronoi cells if I have the coordinates of the cells?
I want to calculate the distance between the vertex of the voronoi cell and the seed of that particular cell.我想计算 voronoi 单元的顶点与该特定单元的种子之间的距离。 However I have not been able to figure out how I can get the seed coordinate for that particular cell.但是,我无法弄清楚如何获得该特定单元格的种子坐标。 I have given my code here which generates an array of cell coordinates.我在这里给出了我的代码,它生成了一个单元格坐标数组。 Could anyone tell me how I can find the corresponding seed for the particular cell?谁能告诉我如何找到特定细胞的相应种子?
import numpy as np
import freud
import matplotlib
import matplotlib.pyplot as plt
import freud.box
import matplotlib.pyplot as plt
points = np.array([
[-2, 2, 0],
[2, 4, 0],
[-2, 3, 0],
[3, 5, 0]])
box = freud.box.Box(10,12, is2D= True )
voro = freud.locality.Voronoi()
cells= voro.compute((box, points)).polytopes
print(cells)
plt.figure()
ax = plt.gca()
ax.scatter(points[:, 0], points[:, 1], c= 'k')
voro.plot(ax=ax)
plt.show()
The output of this code is the plot below and the print(cells) prints the coordinates of the cells vertices as show below.此代码的 output 是下面的 plot 并且 print(cells) 打印单元格顶点的坐标,如下所示。
[array([[-6.46875 , -1.40625 , 0. ],
[-2.7 , -3.5 , 0. ],
[-1.3 , -3.5 , 0. ],
[ 2.26086957, -1.52173913, 0. ],
[ 0.25 , 2.5 , 0. ],
[-5.1 , 2.5 , 0. ],
[-5.25 , 2.25 , 0. ]]), array([[ 0.25 , 2.5 , 0. ],
[ 2.26086957, -1.52173913, 0. ],
[ 3.53125 , -1.40625 , 0. ],
[ 4.75 , 2.25 , 0. ],
[-1.16666667, 8.16666667, 0. ]]), array([[-5.1 , 2.5 , 0. ],
[ 0.25 , 2.5 , 0. ],
[-1.16666667, 8.16666667, 0. ],
[-1.3 , 8.5 , 0. ],
[-2.7 , 8.5 , 0. ]]), array([[ 4.75 , 2.25 , 0. ],
[ 4.9 , 2.5 , 0. ],
[ 7.3 , 8.5 , 0. ],
[ 3.53125 , 10.59375 , 0. ],
[ 2.26086957, 10.47826087, 0. ],
[-1.3 , 8.5 , 0. ],
[-1.16666667, 8.16666667, 0. ]])]
I want to calculate the vertex to seed distance for each of the cell as indicated by the lines in the figure.我想计算每个单元格的顶点到种子的距离,如图中的线条所示。
Here is an example using scipy.spatial.Voronoi:这是使用 scipy.spatial.Voronoi 的示例:
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from scipy.spatial import Voronoi, voronoi_plot_2d
points = np.array([
[-2, 2],
[2, 4],
[1, 3],
[-2, 3],
[3, 5],
[-1, 5],
[2, 1]]
)
vor = Voronoi(points)
print("Coordinates of point 2:")
print(vor.points[2])
print("Region for point 2:")
print(vor.point_region[2])
print("Vertices for region:")
print(vor.regions[vor.point_region[2]])
print("Coordinates of Voronoi vertices:")
print(vor.vertices[vor.regions[vor.point_region[2]]])
for vert in vor.vertices[vor.regions[vor.point_region[2]]]:
distance = np.linalg.norm(vor.points[2]-vert)
print("Distance between " + str(vor.points[2]) + " and " + str(vert) + " is " + str(distance))
fig = voronoi_plot_2d(vor)
plt.gca().set_aspect('equal')
plt.show()
Output running the code is: Output 运行代码为:
Coordinates of point 2:
[1. 3.]
Region for point 2:
4
Vertices for region:
[6, 4, 2, 0, 5]
Coordinates of Voronoi vertices:
[[-0.5 3.5 ]
[ 0.5 4.5 ]
[ 2.5 2.5 ]
[-0.07142857 1.21428571]
[-0.5 2.5 ]]
Distance between [1. 3.] and [-0.5 3.5] is 1.5811388300841898
Distance between [1. 3.] and [0.5 4.5] is 1.5811388300841898
Distance between [1. 3.] and [2.5 2.5] is 1.5811388300841898
Distance between [1. 3.] and [-0.07142857 1.21428571] is 2.0824828195876073
Distance between [1. 3.] and [-0.5 2.5] is 1.5811388300841898
The Voronoi diagram produced contains a few data structures to traverse to associate the seed point with the Voronoi vertices of the cell.生成的 Voronoi 图包含一些要遍历的数据结构,以将种子点与单元的 Voronoi 顶点相关联。 Specifically:具体来说:
point_region
converts the point (seed) index to the associated index in the regions array point_region
将点(种子)索引转换为区域数组中的关联索引regions
contains a list of indices of the Voronoi vertices for a given cell regions
包含给定单元格的 Voronoi 顶点的索引列表vertices
contains the coordinates of the vertices of the Voronoi cells vertices
包含 Voronoi 单元的顶点坐标In the code above, the Voronoi diagram shown below is computed and the distances between the seed point and vertices are computed for the Voronoi cell in the center of the diagram.在上面的代码中,计算了下面显示的 Voronoi 图,并为图中心的 Voronoi 单元计算了种子点和顶点之间的距离。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.