[PYTHON] How to find the area of the Voronoi diagram

Introduction

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.

code

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.

At the end

I think that Voronoi diagrams are often used when analyzing sports, so I hope you will use them.

Recommended Posts

How to find the area of the Voronoi diagram
How to find the optimal number of clusters in k-means
How to find the scaling factor of a biorthogonal wavelet
Find the area of the union of overlapping rectangles
How to check the version of Django
How to find the memory address of a Pandas dataframe value
How to calculate the volatility of a brand
Combinatorial optimization to find the hand of "Millijan"
How to find out the number of CPUs without using the sar command
How to find the correlation for categorical variables
[Circuit x Python] How to find the transfer function of a circuit using Lcapy
How to find the coefficient of the trendline that passes through the vertices in Python
How to know the port number of the xinetd service
How to get the number of digits in Python
How to visualize the decision tree model of scikit-learn
[Blender] How to dynamically set the selection of EnumProperty
[Python] Summary of how to specify the color of the figure
How to hit the document of Magic Function (Line Magic)
How to access the global variable of the imported module
[Selenium] How to specify the relative path of chromedriver?
How to use the generator
How to find Mahalanobis distance
How to use the decorator
How to increase the axis
How to start the program
I tried to find the entropy of the image with python
How to increase the processing speed of vertex position acquisition
[Ubuntu] How to delete the entire contents of a directory
I tried to find the average of the sequence with TensorFlow
How to test the attributes added by add_request_method of pyramid
Inherit the standard library to find the average value of Queue
How to calculate the amount of calculation learned from ABC134-D
(Note) How to pass the path of your own module
How to summarize the results of FreeSurfer ~ aparc, aseg, wmparc ~
How to run the Export function of GCP Datastore automatically
How to increase the number of machine learning dataset images
How to see the contents of the Jupyter notebook ipynb file
How to connect the contents of a list into a string
How to find the average amount of information (entropy) of the original probability distribution from a sample
Find the definition of the value of errno
How to use the zip function
How to use the optparse module
Summary of how to use pandas.DataFrame.loc
Summary of how to use pyenv-virtualenv
How to read the SNLI dataset
How to get the Python version
[Python] How to import the library
How to overwrite the output to the console
Summary of how to use csvkit
How to use the ConfigParser module
Supplement to the explanation of vscode
How to handle multiple versions of CUDA in the same environment
How to determine the existence of a selenium element in Python
How to change the log level of Azure SDK for Python
How to implement Java code in the background of RedHat (LinuxONE)
How to know the internal structure of an object in Python
How to change the color of just the button pressed in Tkinter
How to get the ID of Type2Tag NXP NTAG213 with nfcpy
[EC2] How to install chrome and the contents of each command
How to check the memory size of a variable in Python
Make the theme of Pythonista 3 like Monokai (how to make your own theme)