I'm dealing with a (a) set of points P in Euclidean space (let's assume the plane to keep things simple), (b) a bounding rectangle [a,b] x [c,d] containing all the points in P, along with (c) a vector (m,n) of positive integers encoding subdivisions along each dimension. The basic question is
How does one efficiently build an m x n grid of distances to P?
In particular, I'd like to generate a cubical approximation to the distance function from P in the obvious m x n grid decomposition of the rectangle [a,b] x [c,d]: chop the X-direction into m pieces and the Y direction into n pieces, and for each little rectangle R which results from the partition, compute a positive integer distance d(R) to P defined as follows.
Consider a weighted graph G whose vertex set V corresponds to little rectangles in our grid with an edge of weight $1$ between neighbors (even diagonal neighbors). Call a vertex "red" if the corresponding rectangle contains a point of P. What I want to compute is the function d on V taking positive integer values which associates to each vertex the shortest distance in G to a red vertex. So, if a little rectangle actually contains a point from P, then its associated vertex gets assigned "0". If it does not contain a point of P but is adjacent (even diagonally) to a rectangle which does, then it gets "1" and so on...
The naive approach of considering the distance of each point from each little rectangle and keeping track of the minimum incurs a cost of |P|mn which appears prohibitive for large grids. So here's the second approach that I considered: set each d(R) to some large MAX to each R in the grid which does NOT contain a point from P. Then, set d(R) = 0 for each R that does contain a point of P and throw all its neighbors with strictly larger d-values into a Queue. Then, iterate this until the queue is empty:
- pop a grid rectangle R from the queue,
- if d(R) > 1 + d(R') where R' is the neighbor whose processing enqueued R, then set d(R) = 1 + d(R') and enqueue all neighbors of R' with d-values exceeding 1+d(R).
I'd appreciate any ideas on how one solves this problem more efficiently than this "second approach".