# find the coordinates of $n$-D point from $(n-1)$ angles and distance

I’m writing a program that can render $n$-D shapes, and, through the way I’m doing it, I have come to need to calculate the Cartesian coordinates of a point from $(n-1)$ angles and the distance of the point to the origin.

How can I calculate the coordinates $x_0, x_1, x_2, x_3, \ldots, x_n$ from the distance $r$ and the angles $\alpha_1, \alpha_2, \alpha_3, \ldots, \alpha_n$ wherein $\alpha_m$ is the angle measured from the axis $x_0$ towards the axis $x_m$?

Note: I have read Calculate 3D Vector out of two angles and vector length, but I’m having trouble generalizing it to $n$ dimensions. Above just an answer, an explanation would really be welcome.

• It's not in the english Wikipedia article for some reason, but the german one has it: de.wikipedia.org/wiki/… I hope it's clear enough. – Frank Sep 10 '17 at 18:38
• @Frank, I don't understand why, say, $x_{10}$ has more $\sin$ calculations than, say, $x_3$. I figured it'd have the same amount of calculations but with different angles. If you could post that as an answer and give an explanation, it'd be appreciated. – Zambonifofex Sep 10 '17 at 19:39

## 1 Answer

Assuming $\alpha_m$ is the polar angle measured in the plane spanned by $0$-th and $m$-th basis vectors, it’s a fairly trivial exercise. We have $x_0 = r_m\cos\alpha_m$ and $x_m = r_m\sin\alpha_m$ for some $r_m$ (the length of a projection of our vector $(x_0,\ldots,x_n)$ to that plane), then also we have $$r^2 = x_0^2 + \ldots + x_n^2 = x_0^2\left(1 + (x_1/x_0)^2 + \ldots + (x_m/x_0)^2\right) = x_0^2\left(1 + \tan^2\alpha_1 + \ldots + \tan^2\alpha_n\right),$$ assuming $x_0\ne0$. Then we have found $$x_0 = \frac r{\sqrt{1 + \tan^2\alpha_1 + \ldots + \tan^2\alpha_n}}.$$ And then we also have found all the $x_m = x_0\tan\alpha_m$.

The case $x_0 = 0$ holds iff $\tan\alpha_m$ are undefined, and then there is an ambiguity unless $r$ is also zero (then all the coordinates are obviously zero).

• Thanks for your answer, it seems to be exactly what I was looking for. Floating point will handle the case wherein $\tan \alpha_m$ is undefined for me, but thanks for mentioning it anyway. But I don't understand what you mean when you say that there is an ambiguity when $x_0 = 0$, nor do I understand why $x_m = x_0 \tan \alpha_m$. – Zambonifofex Sep 10 '17 at 22:31
• $x_m = r_m\sin\alpha_m = (x_0/\cos\alpha_m)\sin\alpha_m$, so here’s a tangent. When $x_0 = 0, r\ne0$, all $\alpha_m$ are $\pm\pi/2$, and we can’t recover $x_m$ magnitudes—only their signs. – arseniiv Sep 10 '17 at 23:00
• I was having trouble visualizing the $x_0 = 0$ case, but after actually drawing it, I can better understand the problem with it. Thanks for bothering to answer; it's really appreciated. – Zambonifofex Sep 11 '17 at 1:08