AP_Math: Extend vector2::angle(vector2) to distinguish parallel and antiparallel vectors

There are a number of use cases where distingusihing antiparallel from parallel vectors is important
This commit is contained in:
Michael du Breuil 2017-03-01 16:22:01 -07:00 committed by Francisco Ferreira
parent a62d728933
commit c5c94949bf
2 changed files with 5 additions and 1 deletions

View File

@ -131,9 +131,12 @@ float Vector2<T>::angle(const Vector2<T> &v2) const
return 0.0f;
}
float cosv = ((*this)*v2) / len;
if (fabsf(cosv) >= 1) {
if (cosv >= 1) {
return 0.0f;
}
if (cosv <= -1) {
return M_PI;
}
return acosf(cosv);
}

View File

@ -92,6 +92,7 @@ struct Vector2
T operator %(const Vector2<T> &v) const;
// computes the angle between this vector and another vector
// returns 0 if the vectors are parallel, and M_PI if they are antiparallel
float angle(const Vector2<T> &v2) const;
// computes the angle in radians between the origin and this vector