Solution (source code)

= Solution

<Fermat principle> states that the <optical path length> $\int n\,ds$, equivalently the travel time in the nondispersive stationary isotropic ray model, is stationary for the actual <light ray> between fixed endpoints. It need not be a minimum in every optical system.

For two homogeneous media separated by $y=0$, place the fixed endpoints at $(0,h_1)$ and $(D,-h_2)$, with positive heights. If the ray crosses at $(x,0)$, its optical path is
$$
\mathcal L(x)=n_1\sqrt{x^2+h_1^2}+n_2\sqrt{(D-x)^2+h_2^2}.
$$
Stationarity with respect to the crossing point gives
$$
\frac{d\mathcal L}{dx}=n_1\frac{x}{\sqrt{x^2+h_1^2}}-n_2\frac{D-x}{\sqrt{(D-x)^2+h_2^2}}=0.
$$
The two fractions are $\sin\theta_1$ and $\sin\theta_2$, with angles measured from the interface normal. Thus
$$
\boxed{n_1\sin\theta_1=n_2\sin\theta_2},
$$
which is <Snell's law>. Dividing $\mathcal L$ by the vacuum light <speed> gives the corresponding travel time; minimizing geometric distance without refractive-index weighting would incorrectly predict a straight ray through unlike media.