Light crossing from air into water bends. It isn't taking the shortest path; it's taking the fastest one, since it travels slower in water. Minimizing the travel time with a derivative reproduces Snell's law of refraction.