Rays are traced to the point of intersection with a surface that defines the boundary between different media. At this interface, rays will refract according to Snell's famous law, here written in the vector form:

n' (N x k') = n (N x k)

where N is the unit normal vector of the surface at the point of intersection, and k is the ray direction cosine vector. Primed quantites are after refraction, and unprimed quantities are prior to refraction.

For reflection, the index of refraction is unimportant, and Snell's law simplifies to

N x k' = -N x k

This expression may be derived by setting n' = -n, and this convention is often used by ray tracing programs to eliminate the distinction between refraction and reflection.

Some optical surfaces, such as diffractive gratings, also bend rays. The general form of the diffraction expression, which includes refractive or reflective effects, is

n' (N x k') = n (N x k) + M (l/p) q

where M is the diffraction order, l is the wavelength, p is the local grating period (length per 2p period), q is a unit vector tangent to the surface and parallel to the local grating lines. Note if M is zero, or p is infinite, the general grating expression simplifies to Snell's law.

For a more comprehensive discussion of this topic, see reference 2.