How to handle the edge case of determining if we are moving clockwise at 0/360?

66 Views Asked by At

I am programming a dial in an application that can spin around continuously. The user can grab the dial and turn it, and increase/decrease a value. The problem is I am having trouble with the edge case of when the dial angle goes from 360 to 0 and vice versa if the user is moving it clockwise/counterclockwise to increase or decrease a value respectively.

Currently, I am taking the current angle and previous angle and comparing them by: tan(1/4 * angle). However, I'm not quite sure how to handle if the previous angle is say 350 and our current angle is now 10 moving counterclockwise. If I compare the two I would get a wrong result (i.e. clockwise)

1

There are 1 best solutions below

0
On BEST ANSWER

As long as you check often enough that the dial cannot be moved $180^\circ$ between readings, just subtract the old reading from the new one. If the result is less than $-180$ add $360$. If the result is greater than $180$ subtract $360$. You now have a result in $[-180,180]$ and can use the sign for clockwise/counterclockwise.