2
$\begingroup$

How to calculate signed angle in range $[-\pi..+\pi]$ between two vectors in 3D?

$acos\left(\frac{\boldsymbol{a}\cdot\boldsymbol{b}}{a b}\right)$

gives always positive answer in the range of

$[0..2\pi)$

And

$asin\left(\frac{|\boldsymbol{a}\times\boldsymbol{b}|}{a b}\right)$

gives the answer in the range of

$[-\pi..\pi]$

I can calculate both and select appropriate answer, but is there united formula?

UPDATE

Sorry, I agree I need additional vector $\boldsymbol{n}$ to denote the direction of view. And the range will be $[-2\pi, +2\pi]$ then:

enter image description here

  • 0
    What about decreasing the result with $2\pi$ if the first formula gave a $>\pi$ result?2017-02-12
  • 0
    Both formulas loose vectors order and, consequently, the angle sign. I need angle in range $[-\pi,\pi]$2017-02-12
  • 0
    You can reconstruct the angle sign by calculating the sign of $a x b$. It is already a partial result of your second formula. Furthermore, asin and acos start to be increasingly un-precise as they are calculated at +/-1. If precision is important even in such edge cases, then a quite common solution is to use between the formulas which doesn't have to be calculated for abs values higher than 0.5 .2017-02-12
  • 0
    Your last example is wrong, it should be $angle_{\boldsymbol{n}}(\boldsymbol{b},\boldsymbol{a})=+181°$: vector $\boldsymbol{b}$ must rotate $181°$ counterclockwise to reach $\boldsymbol{a}$.2017-02-12
  • 0
    It makes your solution dependant on $n$. If you calculate $n$ always on the spot by $a\times{b}$, then it will work, although you need an additional vectorial multipliation. Speed critical applications tend to avoid using more complex function, they reduce the whole problem to linear vector-operations.2017-02-13

1 Answers 1

4

Two simple formulas hold for the angle $\theta$ by which $\boldsymbol{a}$ must rotate counterclockwise (as seen from unit vector $\boldsymbol{n}$) to reach $\boldsymbol{b}$: $$ \cos\theta={\boldsymbol{a}\cdot\boldsymbol{b}\over ab},\quad \sin\theta={(\boldsymbol{n}\times\boldsymbol{a})\cdot\boldsymbol{b}\over ab}. $$ Having both $\cos\theta$ and $\sin\theta$ it should not be difficult for you to devise the way to find $\theta$ in the desired range. If you want $0\le\theta<2\pi$ you can set for instance: $$ \cases{ \theta=\arccos{\boldsymbol{a}\cdot\boldsymbol{b}\over ab}, & if $\sin\theta\ge0$;\\ \theta=2\pi-\arccos{\boldsymbol{a}\cdot\boldsymbol{b}\over ab}, & if $\sin\theta<0$.\\ } $$