The reason for the different vibrating lengths is the following:
Depressing a string to fret an note raises string tension and thus pitch. Means that all strings would sound sharp if fretted exactly at the middle of the vibrating length. To compensate for this the length between the 12th ftret and the bridge saddle is increased by varying degree.
As the relative pitch/frequency raise increases with string stiffness, heavier gauge/core strings need more compensation (added length) than thinner ones. This results in the typical slanted pattern.