Voronoi Diagram Edges: How to get edges in the form (point1, point2) from a scipy.spatial.Voronoi object?

て烟熏妆下的殇ゞ 提交于 2019-12-05 08:14:33

Take a look at the ridge_vertices attribute:

    ridge_vertices  (list of list of ints, shape (nridges, *))
        Indices of the Voronoi vertices forming each Voronoi ridge.

Each element in that list is a pair of integers. Each integer is an index into the vertices list. So each element defines a line to be draw in the Voronoi diagram. An index of -1 means a point that is "at infinity".

Here's script that draws the lines of the Voronoi diagram:

import numpy as np
import matplotlib.pyplot as plt
from scipy.spatial import Voronoi


points = np.array([[0, 0], [0, 1], [0, 2],
                   [1, 0], [1, 1], [1, 2],
                   [2, 0], [2, 1], [2, 2]])

vor = Voronoi(points)


fig = plt.figure()

# Mark the Voronoi vertices.
plt.plot(vor.vertices[:,0], vor.vertices[:, 1], 'ko', ms=8)

for vpair in vor.ridge_vertices:
    if vpair[0] >= 0 and vpair[1] >= 0:
        v0 = vor.vertices[vpair[0]]
        v1 = vor.vertices[vpair[1]]
        # Draw a line from v0 to v1.
        plt.plot([v0[0], v1[0]], [v0[1], v1[1]], 'k', linewidth=2)

plt.show()

It creates:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!