Solution (source code)

= Solution

Assume $\pi\in C$ and
$$
(x-\pi)^T(z-\pi)\leq0
$$
for every $z\in C$. Expanding the squared <Euclidean distance> gives
$$
\begin{aligned}
\|x-z\|_2^2
&=\|(x-\pi)-(z-\pi)\|_2^2\\
&=\|x-\pi\|_2^2+\|z-\pi\|_2^2
  -2(x-\pi)^T(z-\pi)\\
&\geq\|x-\pi\|_2^2.
\end{aligned}
$$
\b[Hence $\pi$ minimizes the distance from $x$ over $C$, so $\pi=\pi_C(x)$. This proves the sufficient direction of the <variational characterization of convex projection>.]