You're not pulling sine and cosine out of the air, you're using a substitution. We're saying that x will be:

$\displaystyle x = r\sin{\theta}$

Which automatically means that:

$\displaystyle \frac{dx}{d\theta} = r\cos{\theta}$

And by the properties of differentials we can conclude that:

$\displaystyle dx = r\cos{\theta} ~d\theta$

The rest is the result of the substitution.