In optics, Fermat's principle or the principle of least time is the principle that the path taken between two points by a ray of light is the path that can be traversed in the least time. This principle is sometimes taken as the definition of a ray of light. However, this version of the principle is not general; a more modern statement of the principle is that rays of light traverse the path of stationary, not minimal, time.Fermat's principle can be used to describe the properties of light rays reflected off mirrors, refracted through different media, or undergoing total internal reflection. It can be deduced from Huygens' principle, and can be used to derive Snell's law of refraction and the law of reflection.
for more info see this link http://en.wikipedia.org/wiki/Fermat%27s_principle