Made this because I needed it for my 360 platformer. Basically all you have to do for this is just to check if the nearest side should be a y side or an x side then find the distance to that. It's nothing special.
All code by me. :)