Usually we want the opposite: put the hand there. Which joint angles do that? This is inverse kinematics.
Key idea
For a two-link arm and target $(x, y)$, let $r^2 = x^2 + y^2$. The law of cosines gives the elbow: $\cos\theta_2 = \dfrac{r^2 - L_1^2 - L_2^2}{2 L_1 L_2}$. If that is outside $[-1, 1]$, the target is out of reach. Otherwise there are usually two answers, elbow up and elbow down. Then $\theta_1 = \operatorname{atan2}(y, x) - \operatorname{atan2}(L_2\sin\theta_2,\ L_1 + L_2\cos\theta_2)$.
Why it works
The two links and the line from the shoulder to the target make a triangle with sides $L_1$, $L_2$ and $r$. The law of cosines finds the angle between the links. A triangle can be flipped over its long side, which is why there are two answers.
Worked example: Find the elbow
$L_1 = 10$, $L_2 = 7$, target $(12, 5)$.
- $r^2 = 144 + 25 = 169$, so $r = 13$.
- $\cos\theta_2 = \dfrac{169 - 100 - 49}{140} = \dfrac{20}{140} \approx 0.143$.
- $\theta_2 \approx \pm 81.8^\circ$.
Answer: About 81.8Β° (elbow down) or β81.8Β° (elbow up).
Worked example: Out of reach
Same arm, target $(15, 9)$.
- $r = \sqrt{225 + 81} \approx 17.5$, more than $L_1 + L_2 = 17$.
Answer: No solution: the target is out of reach.
Common mistake
Forgetting to check that the target is in reach, or missing the second solution.