【问题标题】:How can I find the seeds of the voronoi cells if I have the coordinates of the cells?如果我有细胞的坐标,如何找到 voronoi 细胞的种子?
【发布时间】:2021-06-02 13:18:33
【问题描述】:

我想计算 voronoi 单元的顶点与该特定单元的种子之间的距离。但是,我无法弄清楚如何获得该特定单元格的种子坐标。我在这里给出了我的代码,它生成了一个单元格坐标数组。谁能告诉我如何找到特定细胞的相应种子?

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()

此代码的输出如下图所示,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.        ]])]


我想计算每个单元格的顶点到种子的距离,如图中的线条所示。

【问题讨论】:

  • 虽然您展示了您的代码,但您尚未提供 MRE 所需的示例数据、预期输出和当前输出。比如print(cells)的结果是什么
  • @itprorh66 好的,知道了。立即检查修改。
  • 如何手动计算细胞的种子?
  • 我不知道,这是我的问题。
  • 那么这是错误的论坛要问。该论坛是针对与编程相关的具体技术问题提出的,您正在寻找一个解决算法相关问题的论坛。

标签: python-3.x plot voronoi


【解决方案1】:

这是一个使用 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()

运行代码的输出是:

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

生成的 Voronoi 图包含一些要遍历的数据结构,以将种子点与单元格的 Voronoi 顶点相关联。具体来说:

  1. point_region 将点(种子)索引转换为区域数组中的关联索引
  2. regions 包含给定单元格的 Voronoi 顶点的索引列表
  3. vertices 包含 Voronoi 单元的顶点坐标

在上面的代码中,计算了如下所示的 Voronoi 图,并为图中心的 Voronoi 单元计算了种子点和顶点之间的距离。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-07
    • 1970-01-01
    • 2020-02-09
    • 2021-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-21
    相关资源
    最近更新 更多