Transforming linear combination of the cosine and sine function In the proof of Transforming $a\cos\left(\, x\,\right)+b\sin\left(\, x\right)$ to $r\cos\left(\,\phi - x\,\right)$
\begin{align}
a\cos\left(\, x\,\right) + b\sin\left(\, x\,\right)
&=\,\sqrt{\,a^{2} + b^{2}\,}\,
\left[\,\frac{a}{\,\sqrt{\, a^{2} + b^{2}\,}\,}\,\cos\left(\, x\,\right)
+\frac{b}{\,\sqrt{\, a^{2} + b^{2}\,}\,}\,\sin\left(\, x\,\right)\,\right]\,
\\[2mm]&=\,\sqrt{\,a^{2} + b^{2}\,}\,\left[\,
\cos\left(\,\phi\,\right)\cos\left(\, x\,\right) + \sin\left(\,\phi\,\right)\sin\left(\,x\,\right)
\,\right]
\\[2mm]&=\,\sqrt{\, a^{2} + b^{2}\,}\,\cos\left(\,\phi - x\,\right)
\end{align}
why did we factor out $\,\sqrt{\, a^{2} + b^{2}\,}\,$ where did this idea came from ?. It seems out of the blue.

The idea behind the proof comes from noting that 
$$\left(\dfrac{a}{\sqrt{a^2+b^2}}\right)^2+\left(\dfrac{b}{\sqrt{a^2+b^2}}\right)^2=1.\tag1$$
This is analogous to the Pythagorean identity 
$$\cos^2\phi+\sin^2\phi=1.\tag2$$
So for all $a$ and $b$ we can get an expression that satisfies $(2)$ by just factoring out $\sqrt{a^2+b^2}$, but in what way will this be useful? From $(1)$ and $(2)$ we can say that there exists an angle $\phi$ such that 
$$\cos\phi=\dfrac{a}{\sqrt{a^2+b^2}}\qquad\sin\phi=\dfrac{b}{\sqrt{a^2+b^2}},$$ or $$\cos\phi=\dfrac{b}{\sqrt{a^2+b^2}}\qquad\sin\phi=\dfrac{a}{\sqrt{a^2+b^2}}.$$
All of this will permit us to transform $a\cos x+b\sin x$ into an expression of the form 
$$\text{constant}\cdot(\sin\phi\cos x\pm\cos\phi\sin x),$$
or $$\text{constant}\cdot(\cos\phi\cos x\pm\sin\phi\sin x).$$
Those two are the result of the angle-addition formulas, which will enable us to write it in the form $r\cos(\phi\pm x)$ or $r\sin(\phi'\pm x)$.
In conclusion, the proof boils down to determining how to transform the coefficients of $\cos$ and $\sin$ such that they are exactly equal to the $\cos$ and $\sin$ of an angle. The rest follows from the angle-addition formulas.
