clsedistance calculator function