Difference between revisions of "Angular distance"

From vegard.wiki
Jump to navigation Jump to search
(new page)
 
(add general formula for vectors)
 
Line 1: Line 1:
Shortest angular distance between two angles, in radians (always positive):
+
In general, the smallest angle (angular distance) between two vectors is given by:
 +
 
 +
<math>
 +
a = \cos^{-1} \frac{A \cdot B}{\left | A \right | \cdot \left | B \right |}
 +
</math>
 +
 
 +
(if <math>A</math> and <math>B</math> are unit/normalized, then this can be simplified further).
 +
 
 +
Given two angles <math>a_0</math> and <math>a_1</math>, you ''could'' substitute <math>A = \left [ \cos a_0, \sin a_0 \right ]</math> and <math>B = \left [ \cos a_1, \sin a_1 \right ]</math> above, or you could use the following code:
  
 
<source lang="C++">
 
<source lang="C++">
 
// https://gamedev.stackexchange.com/a/4472
 
// https://gamedev.stackexchange.com/a/4472
 +
// a0 and a1 are in radians (always positive)
 
float delta_angle(float a0, float a1)
 
float delta_angle(float a0, float a1)
 
{
 
{

Latest revision as of 12:21, 21 April 2020

In general, the smallest angle (angular distance) between two vectors is given by:

(if and are unit/normalized, then this can be simplified further).

Given two angles and , you could substitute and above, or you could use the following code:

// https://gamedev.stackexchange.com/a/4472
// a0 and a1 are in radians (always positive)
float delta_angle(float a0, float a1)
{
    return M_PI - fabs(fabs(a1 - a0) - M_PI);
}