I have a N x M field and I'd like to create a matrix with the Manhattan distances to a particular location. Additionally, the field wraps around on all ends (torus).
Can I generate this in Numpy fast (without doing a slow flood fill loop)?
For example the function would return
f(x=2, y=3, size_x=8, size_y=8) ->
array([[5, 4, 3, 2, 3, 4, 5, 6],
[4, 3, 2, 1, 2, 3, 4, 5],
[3, 2, 1, 0, 1, 2, 3, 4],
[4, 3, 2, 1, 2, 3, 4, 5],
[5, 4, 3, 2, 3, 4, 5, 6],
[6, 5, 4, 3, 4, 5, 6, 7],
[7, 6, 5, 4, 5, 6, 7, 8],
[6, 5, 4, 3, 4, 5, 6, 7]])
Here position (2,3) has distance zero and all other positions the Manhattan/taxi-cab distance to (2,3) while being able to wrap around at the edges.