derivation of cosine law for triangle