Look up inverse kinematics if you don't know what it is, but this should be self-explanatory anyway...
I used some trigonometry to find a simple way to quickly get the angles without needing to iterate many times, but it only works for 2 segments of the same length.