Q:

Derivation of a proof related to the centroid of a set of points

I encountered the following proof while I was researching about the centroid for a set of points.
I was not able to understand how the proof is derived. Namely, how do you obtain $\sum_i{|a_i-c|^2} + 2(c-x) \cdot\ \sum_i{(a_i-c)} +n|c-x|^2$ from $\sum_i{|a_i-c+c-x|^2}$ where $a_i$ is a point in a set of points, $c$ is the centroid of the set, and $x$ is any point in space (assuming 2-dimensional). 
Image of the proof
Thank you!

A:

$|u+v|^2=|u|^2+2u\cdot v+|v|^2$ and $u\cdot v=v\cdot u$ and $\sum_i2(u_i\cdot v)= 2v\cdot \sum_iu_i.$  Therefore $$\sum_{i=1}^n|a_i-x|^2=\sum_{i=1}^n|(a_i-c)+(c-x)|^2=$$ $$=\sum_{i=1}^n(\;|a_i-c|^2+2(a_i-c)\cdot (c-x)+|c-x|^2\; )=$$ $$=\sum_{i=1}^n(\;|a_i-c|^2+2(a_i-c)\cdot (c-x)\;)+\sum_{i=1}^n|c-x|^2=$$ $$=\sum_{i=1}^n(\;|a_i-c|^2+2(a_i-c)\cdot (c-x)\;)+n|c-x|^2=$$ $$=\sum_{i=1}^n|a_i-x|^2+2\sum_{i=1}^n(a_i-c)\cdot (c-x)+n|c-x|^2=$$ $$=\sum_{i=1}^n|a_i-c|^2+2(c-x)\cdot \sum_{i=1}^n(a_i-c)+n|c-x|^2.$$

