Finding the angle of a line in 3D space projected onto a 2D plane

774 Views Asked by At

I'm actually a programmer here. I have a math problem, but I don't major in math so I may not explain this clearly.

I have a line in a 3D space with points A and B (e.g. (3,4,8) to (6,9,2)). I (or my eyes) am at point C looking towards a direction that allows me to see point A, as well as part of the line or the whole line.

If whatever I'm seeing is being projected onto a 2D plane, how do I calculate the angle between the line and the top of the 2D plane?

1

There are 1 best solutions below

3
On

You do not have sufficient information.

What you need to know is

  • The 3D direction vector $\vec{w}$ perpendicular to the viewing plane: from $C$ into the 3D direction that passes through the center of the 2D viewing plane. This is the direction you are viewing at.

  • The 3D direction vectors $\vec{u}$ and $\vec{v}$ corresponding to "right" and "up", both perpendicular to $\vec{w}$. Often only one of them is specified, because the other can be obtained from the other two via vector cross product: $\vec{v} = \vec{w} \times \vec{u}$ and $\vec{u} = \vec{v} \times \vec{w}$.

Since their lengths are irrelevant, they are usually unit vectors. The following assumes so, that $\lVert\vec{u}\rVert = \lVert\vec{v}\rVert = \lVert\vec{w}\rVert = 1$.

You have a direction vector in 3D, $\vec{d} = B - A = (3, 5, -6)$. Project this to the visual plane using $$\begin{aligned} \vec{p} &= \vec{d} - \frac{\vec{d} \cdot \vec{w}}{\left\lVert \vec{w} \right\rVert} \vec{w} \\ ~ &= \vec{d} - (\vec{d} \cdot \vec{w}) \vec{w} \\ \end{aligned}$$ Then, the dot products between the projected direction vector with the "right" and "up" vectors give you the direction cosines, $$\begin{aligned} x_\Delta &= \vec{p} \cdot \vec{u} \\ y_\Delta &= \vec{p} \cdot \vec{v} \\ \theta &= \operatorname{atan2}\left(y_\Delta, x_\Delta\right) \\ \end{aligned}$$ where $\theta$ is the direction in 2D: $0°$ and $\pm 360°$ right, $+90°$ and $-270°$ up, $\pm 180°$ left, and $+270°$ and $-90°$ down, and $\operatorname{atan2}(y, x)$ is the two-parameter form of $\arctan(y/x)$, except covering all angles by taking both signs into account. Most programming languages provide this two-parameter form.

Because $\vec{u}$ and $\vec{v}$ are perpendicular to $\vec{w}$, $x_\Delta$ and $y_\Delta$ are unchanged by projecting the direction vector to the viewing plane. Thus, we can simplify the calculation to the following:

Let $\vec{u} = ( x_u , y_u , z_u )$ be the 3D vector that is directly right in the 2D view, and $\vec{v} = ( x_v , y_v , z_v )$ be the 3D vector that is directly up in the 2D view, and $\vec{d} = ( x_d , y_d , z_d ) = B - A$ the 3D direction vector. Then, $$\begin{aligned} x_\Delta &= \vec{d} \cdot \vec{u} \\ y_\Delta &= \vec{d} \cdot \vec{v} \\ \theta &= \operatorname{atan2}\left( y_\Delta , x_\Delta \right) \\ \end{aligned}$$ or, in Cartesian coordinates, $$\theta = \operatorname{atan2}\left( x_d x_v + y_d y_v + z_d z_v , \; x_d x_u + y_d y_u + z_d z_u \right)$$