GML Distance to closest Cell with a certain value


So in a grid with 0's and 1's, for every 1 I want to find the closest cell containing a 0, storing that distance (in cells) in an additional grid. This only needs to be executed once to establish all the distances, so performance should not be an issue, however how could I find the distance to a cell with a certain value? Even if I checked all the cells, I would not know which is closest... Thanks!


You could take the A* pathing approach to search the grid around the 1's. As soon as a 0 is found, its distance would be ( abs(1.cellX - 0.cellX ) + abs(1.cellY - 0.cellY) ). This equation would make "adjacent diagonals" further away than those cells that are directly above/below or to the side though.

Save that value as the '0 "distanceToBeat" and then you only need to check within distanceToBeat to see if there are any closer 0's.