Algorithm to calculating shortest distance between two spots on the flat