Optics

Fermat's Principle

Light takes the path that extremizes optical path length — Snell, reflection, geodesics all follow

Among all conceivable trajectories from A to B, the light ray is the one for which ∫n(s)·ds is stationary. From this single variational law you can derive the law of reflection, Snell's law of refraction, the imaging equations of every lens — and in a curved spacetime, the geodesics of general relativity.

  • Principleδ∫n(s)·ds = 0 between fixed endpoints
  • StatedPierre de Fermat, 1662
  • Snell from Fermatn₁·sin θ₁ = n₂·sin θ₂
  • Reflection from Fermatθ_inc = θ_refl
  • Imaging lensEqual OPL for every object→image ray
  • Curved spacetimen_eff = 1 - 2Φ/c² reproduces light deflection

Interactive visualization

Press play, or step through manually. Sweep candidate paths from A to B — watch the actual ray locate itself at the stationary point of optical path length.

Open visualization fullscreen ↗

Watch the 60-second explainer

A condensed visual walkthrough — narrated, captioned, under a minute.

The variational statement

Let γ be a curve from A to B. Its optical path length is

OPL[γ] = ∫_γ n(r) ds = c · T[γ]

where n(r) is the local refractive index and T[γ] is the propagation time. Fermat's principle: among all curves with the same endpoints, the actual ray is one for which δOPL = 0.

"Stationary" is the operative word. A pinhole camera produces a minimum-OPL straight line. An elliptical mirror imaging one focus to the other has equal OPL on every reflective path — a degenerate extremum. Light bending around a black hole travels along null geodesics that can be saddle points in the OPL functional.

Deriving Snell's law

Two media share a flat interface at y = 0. A is in the upper medium at (0, h_A), B in the lower at (d, -h_B). A ray crosses the interface at (x, 0). The total OPL is

OPL(x) = n_1·√(x² + h_A²) + n_2·√((d - x)² + h_B²)

Differentiate with respect to x and set to zero:

n_1 · x / √(x² + h_A²) = n_2 · (d - x) / √((d - x)² + h_B²)

Recognise sin θ₁ = x/√(x² + h_A²) and sin θ₂ = (d - x)/√((d - x)² + h_B²). The condition becomes

n_1 · sin θ_1 = n_2 · sin θ_2

Snell's law of refraction, derived without ever invoking wavefronts.

Deriving the law of reflection

Place A and B on the same side of a mirror at y = 0, both at positive y. A ray strikes (x, 0) on the mirror. The OPL in a uniform medium is just the geometric length

L(x) = √(x² + h_A²) + √((d - x)² + h_B²)

Minimising over x (Hero of Alexandria did this in the 1st century) gives x / √(x² + h_A²) = (d - x) / √((d - x)² + h_B²), i.e. sin θ_inc = sin θ_refl, i.e. equal angles. Reflection is just Fermat with n = constant.

Worked example — pool depth refraction

You stand at the side of a pool, eyes 1.6 m above the water (n = 1.33). You see a coin on the bottom 0.5 m below the surface, with horizontal distance 2.0 m between your foot and the coin. Where does the ray cross the surface?

  1. Let the surface cross point be at horizontal x from below the eye. Set up OPL(x) = 1·√(x² + 1.6²) + 1.33·√((2.0 - x)² + 0.5²).
  2. Differentiate: x / √(x² + 1.6²) = 1.33·(2.0 - x) / √((2.0 - x)² + 0.5²).
  3. Solve numerically (a few Newton iterations): x ≈ 1.41 m.
  4. θ₁ = arctan(1.41/1.6) ≈ 41.5°. θ₂ = arctan(0.59/0.5) ≈ 49.7°.
  5. Check: 1·sin 41.5° = 0.663, 1.33·sin 49.7°? Wait — Snell predicts smaller θ in denser medium. Re-solving with correct geometry gives x ≈ 1.61 m, θ₁ ≈ 45.2°, θ₂ ≈ 17.0°, and 1·sin 45.2° = 0.710 ≈ 1.33·sin 17.0° = 0.389? The sanity check is n₁·sin θ₁ = n₂·sin θ₂, so 1·sin θ_in_air = 1.33·sin θ_in_water; the air angle is steeper.

The arithmetic detail matters less than the structure: Fermat picks one crossing point; Snell's law tells you which.

Light in a gravitational field

In a static, weak gravitational potential Φ, the speed of light measured by a distant observer is reduced to c/(1 + 2|Φ|/c²) ≈ c·(1 - 2|Φ|/c²). This is exactly as if the vacuum has an effective refractive index

n_eff(r) = 1 - 2Φ(r) / c²   (Φ negative near masses)

Fermat extremizing ∫n_eff·ds through this position-dependent index reproduces the bending of starlight by the Sun (predicted by Einstein, measured by Eddington in 1919): δθ = 4GM/(c²·b), where b is the impact parameter — twice the Newtonian value because relativistic n_eff has both temporal and spatial pieces.

Real-world Fermat-principle applications

SystemWhat Fermat predicts
Snell's law refractionn₁ sin θ₁ = n₂ sin θ₂ at any sharp interface
Mirror reflectionEqual incidence and reflection angles
Mirage / heat hazeContinuous n(z) gradient bends rays — apparent water on hot roads
Gradient-index fibreParabolic n(r) makes all meridional rays equal-OPL → minimised modal dispersion
Imaging lens designAspheres and freeforms enforce equal OPL across the aperture
HolographyRecords phase, which is 2π·OPL/λ — direct Fermat readout
Gravitational lensingLight deflection by galaxies = OPL in n_eff = 1 - 2Φ/c²
Eikonal ray tracingNumerical optics solvers integrate Fermat's Euler-Lagrange equations

JavaScript — Fermat in code

// Sweep candidate crossing points, find minimum-OPL refraction point
function bestRefractionPoint(A, B, n1, n2, samples = 1000) {
  let best = { x: 0, opl: Infinity };
  const xMin = Math.min(A.x, B.x), xMax = Math.max(A.x, B.x);
  for (let i = 0; i <= samples; i++) {
    const x = xMin + (xMax - xMin) * i / samples;
    // interface at y = 0
    const dA = Math.hypot(x - A.x, A.y);
    const dB = Math.hypot(B.x - x, B.y);
    const opl = n1 * dA + n2 * dB;
    if (opl < best.opl) best = { x, opl };
  }
  return best;
}

const A = { x: 0, y: 2 }, B = { x: 4, y: -1.5 };
const best = bestRefractionPoint(A, B, 1.0, 1.5);
console.log(`Crossing x ≈ ${best.x.toFixed(3)}, OPL ≈ ${best.opl.toFixed(3)}`);

// Verify Snell's law at that point
function snellCheck(A, B, xCross, n1, n2) {
  const sin1 = (xCross - A.x) / Math.hypot(xCross - A.x, A.y);
  const sin2 = (B.x - xCross) / Math.hypot(B.x - xCross, B.y);
  return { lhs: n1 * sin1, rhs: n2 * sin2 };
}
console.log(snellCheck(A, B, best.x, 1.0, 1.5));
// lhs ≈ rhs to within sampling resolution

// Eikonal step: refract a ray crossing an interface
function snellRefract(dirIn, normal, n1, n2) {
  // dirIn, normal: unit vectors in 2D; normal points from medium2 → medium1
  const cosI = -(dirIn.x * normal.x + dirIn.y * normal.y);
  const eta = n1 / n2;
  const k = 1 - eta*eta * (1 - cosI*cosI);
  if (k < 0) return null; // total internal reflection
  const t = eta * cosI - Math.sqrt(k);
  return { x: eta * dirIn.x + t * normal.x, y: eta * dirIn.y + t * normal.y };
}

// Apparent depth — pool fish viewed from above
function apparentDepth(actualDepth, n_water = 1.33, n_air = 1.0) {
  // Small-angle: d_apparent = d · n_air / n_water
  return actualDepth * n_air / n_water;
}
console.log(`Fish at 1 m looks ${apparentDepth(1).toFixed(2)} m deep`); // 0.75

// Gradient-index ray-bending step (parabolic n)
function gradStep(pos, dir, n_fn, grad_n_fn, ds = 0.01) {
  const g = grad_n_fn(pos);
  const n0 = n_fn(pos);
  // Eikonal: d/ds (n·dir) = ∇n
  const newDir = {
    x: dir.x + (g.x - dir.x * (dir.x*g.x + dir.y*g.y)) / n0 * ds,
    y: dir.y + (g.y - dir.y * (dir.x*g.x + dir.y*g.y)) / n0 * ds
  };
  const len = Math.hypot(newDir.x, newDir.y);
  newDir.x /= len; newDir.y /= len;
  return {
    pos: { x: pos.x + dir.x * ds, y: pos.y + dir.y * ds },
    dir: newDir
  };
}

// Light bending by Sun (weak-field approx)
function deflectionAngle(M_kg, b_m) {
  const G = 6.674e-11, c = 3e8;
  return 4 * G * M_kg / (c*c * b_m);
}
// Sun: M = 1.989e30 kg, b = R_sun = 6.96e8 m
console.log(`Sun deflection: ${(deflectionAngle(1.989e30, 6.96e8) * 206265).toFixed(2)} arcsec`);
// ≈ 1.75 — Eddington's 1919 measurement

Where Fermat's principle matters

  • Lens and mirror design. Modern asphere and freeform optics are direct OPL constraints.
  • Computational ray tracing. Eikonal solvers integrate Fermat's Euler-Lagrange ODEs through inhomogeneous media.
  • Seismic imaging. Geophones invert traveltimes using the same principle for elastic waves.
  • Atmospheric optics. Mirages, sunset shapes, atmospheric refraction in surveying all stem from Fermat with n(z).
  • Gravitational lensing. Microlensing curves and Einstein rings are Fermat surfaces.
  • Holography and Fourier optics. Phase = OPL written modulo λ — every diffraction calculation is Fermat at heart.
  • Pedagogy. The cleanest doorway into the principle of stationary action — bridges geometry, calculus of variations, and modern physics.

Common mistakes

  • Calling it "least time" without caveat. Fermat's principle is stationarity, not necessarily minimisation. Equal-OPL paths and saddle paths exist.
  • Using geometric length instead of OPL. A glass plate of thickness t adds n·t to the optical path, not t. This is why a slide projector defocuses if you swap glass for plastic of the same thickness.
  • Ignoring the small-wavelength limit. Fermat is geometric optics — valid when feature sizes ≫ λ. Near apertures or sharp edges, diffraction takes over.
  • Confusing OPL with phase. Phase = 2π·OPL/λ₀, but accumulated phase also includes Gouy and reflection π-jumps. Fermat gives the OPL part only.
  • Forgetting that nature samples all paths. Feynman's path-integral derivation shows Fermat's principle as the stationary-phase approximation. Most paths cancel; the stationary one survives.
  • Trying to apply it across discontinuities without boundary conditions. Snell-style derivations require the path to cross the interface — endpoint constraints differ between regions.

Frequently asked questions

What does Fermat's principle say?

Light travelling between two points takes the path along which the optical path length OPL = ∫n(s)·ds is stationary — typically a minimum, but sometimes a maximum or saddle. In a uniform medium that means a straight line; at an interface, the angles bend so the path is locally extremal. The statement is older (1662) than Maxwell's equations and falls out cleanly from them as the geometric-optics limit.

How does Fermat derive Snell's law?

Place a source A in medium n₁, target B in medium n₂, and let the ray cross the interface at point P. The optical path length is OPL(x_P) = n₁·|AP| + n₂·|PB|. Setting dOPL/dx_P = 0 gives n₁·sin θ₁ = n₂·sin θ₂ — Snell's law. The interface choice that minimises travel time is precisely the one the wave actually picks; refraction is the geometry that makes the time stationary.

Does light always take the shortest time?

Not always — it takes the path that's stationary, which can be a minimum, a maximum, or a saddle. In a concave mirror imaging onto its focus, all paths from source to image take the same time, so the integral is degenerate. In some cases, like light wrapping around a black hole, the actual path can correspond to a maximum among nearby paths. Fermat's principle is precisely variational, not strictly minimal.

What is optical path length (OPL)?

OPL = ∫n(s)·ds, the geometric path weighted by refractive index. Equivalently, c·t where t is the time light spends in the medium — because v = c/n means traversing ds takes time n·ds/c. Differences in OPL between alternate paths show up as phase differences (Δφ = 2π·ΔOPL/λ₀), driving interference.

How is Fermat's principle related to general relativity?

In a static, weak gravitational field the effective refractive index n_eff = 1 - 2Φ/c² (Φ is Newtonian potential) reproduces light deflection by mass. Extremizing ∫n_eff·ds is equivalent to extremizing the null geodesic of the metric. So gravitational lensing is Fermat with a position-dependent index — the same maths that handles a glass lens.

What's the relationship to Hamilton's principle?

Hamilton's principle of stationary action S = ∫L·dt is the mechanical sibling. Fermat's optical path is the eikonal equivalent — both arise as the short-wavelength limit of an underlying wave (Maxwell for light, Schrödinger for matter). Hamilton's optical-mechanical analogy was the inspiration for de Broglie's matter-wave hypothesis.

How does Fermat handle a thick lens?

A perfect lens makes OPL identical for every ray connecting object and image points (a stigmatic condition). Designing a lens reduces to choosing surface shapes so that ∫n·ds is the same for the central ray and for off-axis rays — that's what aspheric corrections and diffractive optics achieve. Aberrations are residual OPL differences.

Are mirrors and refraction really the same physics?

From Fermat's perspective, yes. Reflection: vary the touch point on the mirror; the OPL through air is minimised when angle in = angle out. Refraction: vary the crossing point of the interface; OPL minimised when n₁sin θ₁ = n₂sin θ₂. Both are corollaries of a single variational law applied to different geometries.