The following code will find the shortest rotation (in radians) (from pi to -pi) that I need to apply to from
to leave me with to
.
Scalar rotationBetween(Scalar from, Scalar to)
{
Scalar fromMod = std::signbit(from) ?
pi * 2 + std::fmod(from, pi * 2) : std::fmod(from, pi * 2);
Scalar toMod = std::signbit(to) ?
pi * 2 + std::fmod(to, pi * 2) : std::fmod(to, pi * 2);
Scalar rotation = toMod - fromMod;
if(rotation > pi)
return rotation - 2 * pi;
else if(rotation < -pi)
return 2 * pi + rotation;
else
return rotation;
}
Scalar
is a c++ float
.
Can I simplify this? I feel like this is far too much logic to do something so simple.
std::min(std::max(from, to) - std::min(from,to), std::min(from,to) + 2*pi - std::max(from,to));
? \$\endgroup\$