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

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


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 cKDTreepoints = [[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.


For very small number of points and cells it might be simpler to check every point against every cell with 'point in polygon' algorithm (perhaps there are numpy implementations of it)

In general case you can build trapezoidal map data structure over cells and quickly determine - what cell every points belongs to.

Arbitrary found example