Find the angle between two 3D-vectors

6.1k Views Asked by At

Image

In this picture (I'm using a $3D$ space, I draw it in $2D$ just to simplify), I have to find if the circumference $c$ intersecate the subtraction between $v2$ and $v1$, by checking the length of that small segment called $d$. To find it, the easier way is to find the $\angle DBC$ and then use the cosine formula.

The problem is that I don't know how to find that angle, because I think the $2D$ formula $a = \text{acos}(\text{norm}(v)* \text{norm}(v'))$ is missing a dimension

2

There are 2 best solutions below

6
On BEST ANSWER

Use inner product of vectors. The relation is $v.v' = |v| |v'| cos(\theta)$ with $norm(v)=|v|$.

Take $v=(a,b,c)$ and $v'=(a',b',c')$. The inner product is $v.v' = aa' + bb'+cc'$. The norm is $\sqrt{a^2 + b^2 + c^2}$.

So the angle becomes

$\theta = \cos^{-1} [\frac{v.v'}{|v||v'|}] = \cos^{-1} [\frac{aa' + bb'+cc'}{\sqrt{a^2 + b^2 + c^2} \sqrt{a'^2 + b'^2 + c'^2}}]$

Find components of each vector and substitute.

0
On

This is not an answer to the stated question, but more like an extended comment; specifically, that there is an even easier way to find out $d$, the minimum distance between the line segment $\vec{BC}$ and point $\vec{D}$.

The question implies that OP is interested in whether a spherical shell, let's say of radius $r$, centered at $\vec{D}$, intersects the line segment between $\vec{B}$ and $\vec{C}$.

  1. If $\lvert\vec{D} - \vec{B}\rvert \le r$, and $\lvert\vec{D} - \vec{A}\rvert \le r$, then both points $\vec{B}$ and $\vec{C}$ are within the spherical shell. Because the shell is convex, the line segment between $\vec{B}$ and $\vec{C}$ is also completely inside the spherical shell (and therefore there is no intersection per se).

    In practice, the checks are better written as $\lvert \vec{D} - \vec{B} \rvert^2 \le r^2$ and $\lvert\vec{D} - \vec{A}\rvert^2 \le r^2$, i.e. $$(x_D - x_B)^2 + (y_D - y_B)^2 + (z_D - z_B)^2 \le r^2$$ and $$(x_D - x_C)^2 + (y_D - y_C)^2 + (z_D - z_C)^2 \le r^2$$

    Otherwise,

  2. If $\lvert\vec{D} - \vec{B}\rvert \le r$, then point $\vec{B}$ is within the spherical shell. Because $\vec{C}$ isn't, the line segment between $\vec{B}$ and $\vec{C}$ must pass through the spherical shell.

    Otherwise,

  3. If $\lvert\vec{D} - \vec{C}\rvert \le r$, then point $\vec{C}$ is within the spherical shell. Because $\vec{B}$ isn't, the line segment between $\vec{B}$ and $\vec{C}$ must pass through the spherical shell.

    Otherwise,

  4. Calculate the distance squared $d^2$ between point $\vec{D}$ and the line that passes through points $\vec{B}$ and $\vec{C}$ (using e.g. Point-Line distance from Wolfram MathWorld) :

    $$d^2 = \frac{\lvert ( \vec{C} - \vec{B} ) \times ( \vec{B} - \vec{D} ) \rvert^2 }{\lvert \vec{C} - \vec{B} \rvert^2 }$$

    i.e.

    $$d^2 = \frac{[ (y_C - y_B) (z_B - z_D) - (z_C - z_B) (y_B - y_D) ]^2 + [ (z_C - z_B) (x_B - x_D) - (x_C - x_B) (z_B - z_D) ]^2 + [ (x_C - x_B) (y_B - y_D) - (y_C - y_B) (x_B - x_D) ]^2 }{ (x_C - x_B)^2 + (y_C - y_B)^2 + (z_C - z_B)^2 }$$

    Now, if $d^2 \gt r^2$, the entire line passing through $\vec{B}$ and $\vec{C}$ is outside the spherical shell, and there cannot be any intersection.

    Otherwise,

  5. Calculate the relative position $t$ of the point closest to $\vec{D}$ on the line passing through $\vec{B}$ and $\vec{C}$:

    $$t = \frac{ (\vec{D}-\vec{B}) \cdot (\vec{C} - \vec{B})}{\lvert\vec{C} - \vec{B}\rvert^2 }$$

    If $0 \le t \le 1$, then the closest point to $\vec{D}$ on the line is between $\vec{B}$ and $\vec{C}$, and it is either inside ($d^2 \lt r^2$) or on ($d^2 = r^2$) the spherical shell.

    This test can also be written as $$0 \le (x_D-x_B)(x_C-x_B) + (y_D-y_B)(y_C-y_B) + (z_D-z_B)(z_C-z_B) \le (x_C-x_B)^2 + (y_C-y_B)^2 + (z_C-z_B)^2$$

    Otherwise, there is no intersection. (The line that passes through $\vec{B}$ and $\vec{C}$ does intersect the $r$-radius spherical shell centered at $\vec{D}$, but the intersections do not occur in the segment between $\vec{B}$ and $\vec{C}$.)

If I counted right, the maximum cost of the above tests, total, is 25 multiplications, one division, and 46 additions or subtractions (but much fewer multiplications and additions or subtractions if you use temporary variables so you don't do the same operations repeatedly). On a computer, that is roughly comparable to the work done to evaluate a single trigonometric function; so if you use that as the metric for "easy" (I do), this is definitely "easier".