Template Function nebula::drivers::angle_is_between

Function Documentation

template<typename T>
inline bool nebula::drivers::angle_is_between(T start_angle, T end_angle, T angle, bool start_inclusive = true, bool end_inclusive = true)

Tests if angle is in the angular sector defined by start_angle and end_angle.

Assumes that all three angles are normalized to the interval [0; max_angle) before comparison. This function is unit-independent (max_angle can be 360 for degrees, 2 * M_PI for radians, 36000 for centi-degrees, etc.) as long as the above assumption is fulfilled.

If start_angle equals end_angle, the sector is considered to cover the full circle. In this case, an angle is always considered to be in the sector, except when it coincides with the start_angle/end_angle and both boundaries are exclusive. There is no way to define a zero-width sector.

When start_angle and end_angle differ, angle is considered to be in the sector if

  • start_angle <= angle <= end_angle, or

  • end_angle < start_angle and (angle <= end_angle or start_angle <= angle)

Examples:

 {c++}
assert(angle_is_between(90, 270, 180) == true);
assert(angle_is_between(270, 90, 0) == true);  // wrap-around case
assert(angle_is_between(0, 0, 0) == true);
assert(angle_is_between(0, 0, 0, false, false) == false);