derivation of distance formula