Solution (source code)

= Solution

Choose the mirror as the line $y=0$, the source as $(x_A,h_A)$ and the observer as $(x_B,h_B)$, with positive heights. Within the plane of incidence, let the reflection point be $(s,0)$. In a uniform medium the optical length is a constant index times
$$
\ell(s)=\sqrt{(s-x_A)^2+h_A^2}+\sqrt{(x_B-s)^2+h_B^2}.
$$
Stationarity with respect to the point on the mirror gives
$$
0=\ell'(s)=\frac{s-x_A}{\sqrt{(s-x_A)^2+h_A^2}}-\frac{x_B-s}{\sqrt{(x_B-s)^2+h_B^2}}.
$$
These two ratios are the sines of the angles to the normal, with the stationary point between the two projected endpoints. Therefore
$$
\boxed{\sin\theta_{\rm in}=\sin\theta_{\rm out},\qquad\theta_{\rm in}=\theta_{\rm out}.}
$$
The angles lie in $[0,\pi/2)$, where sine is injective. Varying a second tangential coordinate in three dimensions also enforces that the source, normal and reflected ray lie in one plane. This derives the <law of reflection> from <Fermat principle>.