简体   繁体   English

如果我有细胞的坐标,如何找到 voronoi 细胞的种子?

[英]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:具体来说:

  1. point_region converts the point (seed) index to the associated index in the regions array point_region将点(种子)索引转换为区域数组中的关联索引
  2. regions contains a list of indices of the Voronoi vertices for a given cell regions包含给定单元格的 Voronoi 顶点的索引列表
  3. 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.

相关问题 我怎样才能找到不同值的坐标? - How can i find the coordinates of distinct values? 如何在Python中迭代地写入Excel单元格? - How can I write into Excel cells iteratively in Python? 如何将一行 (df) 中的 2 个单元格的内容转换为 Python 上同一行中的多个单元格? Pandas相关 - How can I convert the contents from 2 cells in a row (df) to say several more cells in the same row on Python? Pandas related 如何将 Python 中的代码和 Markdown 单元格导入 Jupyter Notebook? - How can I import code and markdown cells from Python to Jupyter notebook? 如何正确计算图像中每个单元格的相邻单元格的数量? - How can I correctly count the number of the neighboring cells for each cell in the image? 如何在不循环所有单元格的情况下将 openpyxl 单元格的范围转换为 pandas 数据框? - How do I convert range of openpyxl cells to pandas dataframe without looping though all cells? 如何允许使用 xlsxwriter 编辑受保护的单元格 - How do I allow protected cells to be edited using xlsxwriter 我如何从二进制图像中分离单元格 - How could I separate cells from binary image 如何在 Pandas 列中的特定单元格中添加连字符 - How do I add hyphens to specific cells in a Pandas column 我有一个多项式方程和 Y,我怎样才能找到 X? - I have a polynomial equation and Y, How can I find X?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM