I have an algorithm that calculates the amount an object has rotated.
It calculates between -180 and 180 degrees to deal with both clockwise and anticlockwise. However when the rotation is just about 180 degrees, due to rounding it ends up detecting a mix of -180 and 180 degree rotations when comparing different parts of the shape, depending on how rounding occurs this throws the actual rotation out by quite a bit. The answer to this would be to use the absolute value however this ends up breaks rotation direction.
Here is the code
double newAngle = objOneAngle - objTwoAngle;
if (newAngle< -180)
newAngle += 360;
else if (newAngle > 180)
newAngle -= 360;
Any thoughts on the best way to enhance the above code to deal with 180 degree rotations?
EDIT: The most obvious method that comes to mind is to split up the positive and negative values, and then choose the best set, based on size or some other metric, but there must be a simpler more elegant solution?