计算 Voronoi 多边形的最远顶点与生成它的点之间的距离
Calculating the distance between the furthest vertex of a Voronoi polygon and the point from which it was generated
我需要计算 Voronoi 多边形的最远顶点与生成它的点之间的距离。我需要计算所有 Voronoi 多边形的距离。
有没有办法在 Python 中自动执行此操作?
为了生成多边形,我使用了 Scipi。你对我有什么提示或提示吗?
类似这样的方法可行:
import numpy as np
import matplotlib.pyplot as plt
from scipy.spatial import Voronoi, voronoi_plot_2d, distance
points = np.random.uniform(0, 100, size=(10, 2))
vor = Voronoi(points)
voronoi_plot_2d(vor)
dists = []
for i,point in enumerate(points):
# get nearby vertices
ridges = np.where(vor.ridge_points == i)[0]
vertex_set = set(np.array(vor.ridge_vertices)[ridges, :].ravel())
region = [x for x in vor.regions if set(x) == vertex_set][0]
region = [x for x in region if x != -1] # remove outliers
polygon = vor.vertices[region]
if len(polygon) < 1:
continue
# calc distance of every vertex to the initial point
distances = distance.cdist([point], polygon).T
max_dist_idx = np.argmax(distances)
max_dist = distances[max_dist_idx]
dists.append(max_dist)
# just for visuals
xvals = [point[0], polygon[max_dist_idx][0]]
yvals = [point[1], polygon[max_dist_idx][1]]
plt.plot(xvals,yvals,'r-')
# do stuff with that info
plt.show()
将多边形与封闭点匹配的方法发布在这里:
我需要计算 Voronoi 多边形的最远顶点与生成它的点之间的距离。我需要计算所有 Voronoi 多边形的距离。
有没有办法在 Python 中自动执行此操作?
为了生成多边形,我使用了 Scipi。你对我有什么提示或提示吗?
类似这样的方法可行:
import numpy as np
import matplotlib.pyplot as plt
from scipy.spatial import Voronoi, voronoi_plot_2d, distance
points = np.random.uniform(0, 100, size=(10, 2))
vor = Voronoi(points)
voronoi_plot_2d(vor)
dists = []
for i,point in enumerate(points):
# get nearby vertices
ridges = np.where(vor.ridge_points == i)[0]
vertex_set = set(np.array(vor.ridge_vertices)[ridges, :].ravel())
region = [x for x in vor.regions if set(x) == vertex_set][0]
region = [x for x in region if x != -1] # remove outliers
polygon = vor.vertices[region]
if len(polygon) < 1:
continue
# calc distance of every vertex to the initial point
distances = distance.cdist([point], polygon).T
max_dist_idx = np.argmax(distances)
max_dist = distances[max_dist_idx]
dists.append(max_dist)
# just for visuals
xvals = [point[0], polygon[max_dist_idx][0]]
yvals = [point[1], polygon[max_dist_idx][1]]
plt.plot(xvals,yvals,'r-')
# do stuff with that info
plt.show()
将多边形与封闭点匹配的方法发布在这里: