Fermat's Principle
From biophysics wiki
Fermat's principle or the principle of least time, describes the way beam of light takes through different media. The light chooses the path that takes the least time from one point to another. Consider a wave travelling through two different media $1,2$ with the corresponding speed $v_1$ and $v_2$ ($v_1>v_2$).

The time the light ray needs to travel from A to B is \begin{align} T&=\int_A^B dt = \int_a \frac{ds}{v_1} + \int_b \frac{ds}{v2}\\ &=\frac{\sqrt{y_a^2+x^2}}{v_1}+\frac{\sqrt{y_b^2+(L-x)^2}}{v_1} \end{align} How does the time $T$ change if we vary $x$? \begin{align} \frac{dT}{dx}=\frac{2x}{2 \sqrt{y_a^2+x^2}} \frac{1}{v_1}-\frac{2(L-x)}{2 \sqrt{y_b^2+(L-x)^2}} \frac{1}{v_2} \end{align}