Optimized calcAngle function.