code: public static double angularDifference (double a1, double a2) {
          double ma1 = mirrorAngle(a1), ma2 = mirrorAngle(a2);
          double diff = a1 - a2, mdiff = ma2 - ma1;
          return (Math.abs(diff) < Math.abs(mdiff)) ? diff : mdiff;
      }
docs: Returns the (shortest) difference between two angles, assuming that both angles are in
  [-pi, +pi].