2

I have a 2D matrix with boolean values, which is updated highly frequently. I want to choose a 2D index {x, y} within the matrix, and find the nearest element that is "true" in the table, without going through all the elements (the matrix is massive).

For example, if I have the matrix:

0000100
0100000
0000100
0100001

and I choose a coordinate {x1, y1} such as {4, 3}, I want returned the location of the closest "true" value, which in this case is {5, 3}. The distance between the elements is measured using the standard Pythagorean equation:

distance = sqrt(distX * distX + distY * distY) where distX = x1 - x and distY = y1 - y.

I can go through all the elements in the matrix and keep a list of "true" values and select the one with the shortest distance result, but it's extremely inefficient. What algorithm can I use to reduce search time?

Details: The matrix size is 1920x1080, and around 25 queries will be made every frame. The entire matrix is updated every frame. I am trying to maintain a reasonable framerate, more than 7fps is enough.

MathuSum Mut
  • 2,765
  • 3
  • 27
  • 59

2 Answers2

1

If matrix is always being updated, then there is no need to build some auxillary structure like distance transform, Voronoy diagram etc.

You can just execute search like BFS (bread-first search) propagating from query point. The only difference from usual BFS is euclidean metrics. So you can generate (u, v) pairs ordered by (u^2+v^2) and check symmetric points shifted by (+-u,+-v),(+-v,+-u) combinations (four points when u or v is zero, eight points otherwise)

MBo
  • 77,366
  • 5
  • 53
  • 86
  • I am not seeing any other good approach too. But I don't think this will be fast enough if the number of queries are large and matrix is very large. – Kaidul Nov 17 '16 at 16:06
  • Euclidian distance is making this hard. Does it make sense to use Manhattan distance (distance = abs(distX) + abs(distY))? – wigy Nov 18 '16 at 16:47
0

You could use a tree data structure like a quad-tree (see https://en.wikipedia.org/wiki/Quadtree) to store all locations with value "true". In this way it should be possible to quickly iterate over all "true" values in the neighborhood of a given location. Furthermore, the tree can be updated in logarithmic time, if the value of a location changes.

otmar
  • 386
  • 1
  • 9