I wanted to find the area of the Voronoi diagram, but I had a hard time finding the article that asked for the area, so I would like to write it down as a memo.
I will put the code immediately.
def voronoi_area(points):
v = Voronoi(points)
vol = np.zeros(v.npoints)
for i, reg_num in enumerate(v.point_region):
indices = v.regions[reg_num]
if -1 in indices:
vol[i] = np.inf
else:
vol[i] = ConvexHull(v.vertices[indices]).volume
return vol
sample_points = [[-2, 6], [ 3, -8], [ 5, 9], [ 4, 5], [-7, 2], [ 3, 4]]
voronoi_area(sample_points)
# >>> [inf, inf, inf, 205.92126984, inf, 52.62380952]
Since there is a part where the area of the Voronoi diagram becomes infinite, I try to output it as np.inf. If you want to prevent it from becoming infinite, you should mirror it. I think it is necessary when finding the area of the Voronoi diagram of athletes, so I will list it below.
def voronoi_volumes(points, x_max, y_max):
points_len = np.shape(points)[0]
points1 = points.copy()
points1[:,1] = - points[:,1]
points2 = points.copy()
points2[:,1] = 2 * y_max - points[:,1]
points3 = points.copy()
points3[:,0] = - points[:,0]
points4 = points.copy()
points4[:,0] = 2 * x_max - points[:,0]
points = np.concatenate((points, points1, points2, points3, points4), axis=0)
v = Voronoi(points)
vol = np.zeros(v.npoints)
for i, reg_num in enumerate(v.point_region):
indices = v.regions[reg_num]
vol[i] = ConvexHull(v.vertices[indices]).volume
return vol[:points_len]
Enter the maximum value of the X axis in x_max and the maximum value of the Y axis in y_max.
I think that Voronoi diagrams are often used when analyzing sports, so I hope you will use them.
Recommended Posts