Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: check (and count how many) where points sit within voronoi cells

I think my questions has something in common with this question or others, but anyway, mine is not specifically about them.

I would like, after having found the voronoi tessallination for certain points, be able to check where other given points sit within the tessellination. In particular:

Given say 50 extra-points, I want to be able to count how many of these extra points each voronoi cell contains.

My MWE

from scipy.spatial import ConvexHull, Voronoi, voronoi_plot_2d
import matplotlib.pyplot as plt

points = [[0,0], [1,4], [2,3], [4,1], [1,1], [2,2], [5,3]]
#voronoi
vor = Voronoi(points)
voronoi_plot_2d(vor)
plt.show()

voronoi

Now I am given extra points

extraPoints = [[0.5,0.2], [3, 0], [4,0],[5,0], [4,3]]
# In this case we have that the first point is in the bottom left, 
# the successive three are in the bottom right and the last one
# is in the top right cell.

I was thinking to use the fact that you can get vor.regions or vor.vertices, however I really couldn't come up with anything..

Is there parameter or a way to make this

like image 435
Euler_Salter Avatar asked Mar 09 '23 05:03

Euler_Salter


1 Answers

This stuff is interesting, I have solved it for you using the answer from here The docs for this function are here

from scipy.spatial import cKDTree
points = [[0,0], [1,4], [2,3], [4,1], [1,1], [2,2], [5,3]]
voronoi_kdtree = cKDTree(points)
extraPoints = [[0.5,0.2], [3, 0], [4,0],[5,0], [4,3]]
test_point_dist, test_point_regions = voronoi_kdtree.query(extraPoints)
test_point_regions
>>> array([0, 3, 3, 3, 6])

Here is how you interpret that array - Will call your 'extraPoints' test points. Your first test point (0.5,0.2) sits in the region around your 0th point being (0,0). Second, third and fourth point sit in the region around point 3 (0-index) being (4,1). The last of your test points is around (5,3).

I think there might be an issue in imagining that these regions are polygons and trying to apply that kind of algorithm, as some of them on the edges are regions that go off to infinity.

like image 193
cardamom Avatar answered Apr 27 '23 17:04

cardamom