Derivation of the formula starting from the distance formula