Normalize an angle to the range [0, 360).
static double normalizeRotation(double rotation) { double normalized = rotation % 360; return normalized < 0 ? normalized + 360 : normalized; }