convert $(x-x')^2 + (y-y')^2 + (z-z')^2$ to cylindrical coordinates Context
This is an interim problem related to a Green's function solution for a boundary-value problem in the cylindrical coordinate system.
Question
How do I convert $(x-x')^2 + (y-y')^2 + (z-z')^2$ to cylindrical coordinate system?
My worked solution is given in the answer below.
 A: 
$$(x-x')^2 + (y-y')^2 + (z-z')^2
=r^2 + {r'}^2 -2\,r\,{r}' \,\cos\left(\theta - \theta'\right) + (z-z')^2$$

My attempt
I know that to tranform from Cartesian to cylindrical requires the following three equations
\begin{align}
       r &= \sqrt{x^2 + y^2} \\
  \theta &= \arctan{\left(\frac{y}{x}\right)} \\
       z &= z \quad.
\end{align}
Directly, the question reduces to how to covert the following to cylindrical system.
$$(x-x')^2 + (y-y')^2.$$
Its natural to begin with $r$ and $r'$.
\begin{align}
(x-x')^2 + (y-y')^2
&=
 x^2 - 2\,x\,x' +{x'}^2 +  y^2 - 2\,y\,{y'} +{y'}^2 
\\
&=
 x^2 +  y^2 +{x'}^2 +{y'}^2 - 2\,x\,x'  - 2\,y\,{y'}  
\\
&=
r^2 +{r'}^2 - 2\,x\,x'  - 2\,y\,{y'}  
\end{align}
Now the problem is to convert $ - 2\,x\,x'  - 2\,y\,{y'}   $ to cylindrical coordinate system.
The next part of the answer required trial and error, and some good fortune.
\begin{align*}
r\,r' \,\cos\left(\theta - \theta'\right) 
&= 
\sqrt{\left(x^2 + y^2\right)\,\left( {x '}^2 + {y'}^2\right)}\,\cos\left(\arctan{\frac{y}{x}} - \arctan{\frac{y'}{x'}}\right) 
\\
&= 
\sqrt{\left(x^2 + y^2\right)\,\left( {x '}^2 + {y'}^2\right)}\,\cos\left(\arctan{\frac{\frac{y}{x} - \frac{y'}{x'}}{1+ \frac{y}{x} + \frac{y'}{x'}}} \right) 
\\
&= 
\sqrt{\left(x^2 + y^2\right)\,\left( {x '}^2 + {y'}^2\right)}\,\cos\left(\arctan{\frac{\frac{y}{x} - \frac{y'}{x'}}{1+ \frac{y\,y'}{x\,x'}  }} \right) 
\\
&= 
\sqrt{\left(x^2 + y^2\right)\,\left( {x '}^2 + {y'}^2\right)}\,\cos\left(\arctan{\frac{ y\, x'  -  y'\,x  }{x\,x'+  y\,y'   }} \right) 
\\
&= 
\sqrt{\left(x^2 + y^2\right)\,\left( {x '}^2 + {y'}^2\right)}\,\frac{1}{\sqrt{1+\left(\frac{ y\, x'  -  y'\,x  }{x\,x'+  y\,y'   }\right)^2}} 
\\
&= 
\sqrt{\left(x^2 + y^2\right)\,\left( {x '}^2 + {y'}^2\right)}\,\frac{x\,x'+  y\,y' }{\sqrt{(x\,x'+  y\,y' )^2+\left(  y\, x'  -  y'\,x   \right)^2}} 
\\
&= 
\sqrt{\left(x^2 + y^2\right)\,\left( {x '}^2 + {y'}^2\right)}\,\frac{x\,x'+  y\,y' }{\sqrt{\left(x\,x'\right)^2+      2\,x\,x' \,  y\,y' + \left( y\,y' \right)^2+\left(  y\, x' \right)^2   -2\, y\, x'\,y'\,x     + \left( y'\,x    \right)^2}} 
\\
&= 
\sqrt{\left(x^2 + y^2\right)\,\left( {x '}^2 + {y'}^2\right)}\,\frac{x\,x'+  y\,y' }{\sqrt{\left(x\,x'\right)^2+        \left( y\,y' \right)^2+\left(  y\, x' \right)^2         + \left( y'\,x    \right)^2}} 
\\
&= 
x\,x'+  y\,y' 
\end{align*}
At this point the nut is cracked, and I rebuild the solution as follows:
\begin{align}
 - 2\,x\,x'  - 2\,y\,{y'} 
&= -2\,r\,r' \,\cos\left(\theta - \theta'\right) 
\\
r^2 + {r'}^2 - 2\,x\,x'  - 2\,y\,{y'} 
&=r^2 + {r'}^2 -2\,r\,r' \,\cos\left(\theta - \theta'\right) 
\\
(x-x')^2 + (y-y')^2
&=r^2 + {r'}^2 -2\,r\,r' \,\cos\left(\theta - \theta'\right) 
\\
(x-x')^2 + (y-y')^2 + (z-z')^2
&=r^2 + {r'}^2 -2\,r\,r' \,\cos\left(\theta - \theta'\right) + (z-z')^2
\end{align}
