Diffraction patterns are formed when waves encounter an obstacle or opening that alters their path. For instance, when a beam of X-rays or electrons is directed at a crystal, the waves are scattered by the atoms within the crystal lattice. Depending on the arrangement and spacing of these atoms, the scattered waves interfere with one another, creating a pattern that can be detected and analyzed.