This is essentially https://en.wikipedia.org/wiki/Marching_squares with right angles, isn't it?