Need to devise a formula to compare 2 angles

paddycobbett
paddycobbett used Ask the Experts™
on
Hi all,

I need a mathematical formula to determine whether given a starting angle and an ending angle, I need to determine whether it is "shorter" to go in a clock-wise direction or anti-clockwise.


For example:

starting angle: 50 degrees
ending angle: 60 degrees

It would be clockwise, as it's only 10 degrees in clockwise fashion where as it would 350 degrees if it would to go anti-clockwise to reach the 60 degree mark.

Is there some mathematical formula that can be devised which would return a result indicating a clockwise and anti-clockwise direction? Perhaps something which returns a positive or negative corresponding to each??

For a bit of context I'm a computer programmer and need to implement the logic, but my maths is failing me on this one!

Many thanks!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
you need to do this:

angle_difference = (angle1 - angle2) mod 360;

Perhaps if you can tell which language are you using I can help more. For example on C/Java languages families you sound put:

angle_difference = (angle1 - angle2) % 360;

angle1 is the bigger one and angle2 is the smaller one. If you want the anti-clockwize, just substract 360 and change the sign.

Regards.



dhsindyRetired considering supplemental income.

Commented:
There are several angle conventions.  You need to specify the system you are using.  Especially where 0 is and which direction is a positive angle.
As I know, 0 is from the middle of the circle to the right, and angles increments anti-clock-wise.

So the formula I said is not exactly: when I said clock-wise I meant anti-click-wise and vice-versa.
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

Author

Commented:
I'm not sure if I've explained properly or if I fully understand the answers. Let me give another example, say given a 12 hour clock. Given a starting time and an ending time on the 12 hour clock (i.e 2 hour hands), would it be quicker for the starting hand to reach the ending hand if it were to go clockwise or anti-clockwise, i.e forwards or backwards in time.

For example say the starting hand was at 3 o'clock, and the ending hand was at 6 o'clock, then it would go clockwise.. however if the ending hand was at 2 o'clock then it would go counter clock-wise.

I don't need to know how many hours it takes (i.e how many degrees), just need to know which direction it goes in. The formula should not reference the angles via "smaller angle" and "larger angle", since it does not know which is larger, "starting angle" and "ending angle". But suspect that the result would be positive or negative indicating the direction?? I'm not sure, but am saying I should be able to infer from the result the direction.

Any ideas? .. or perhaps I just needed clarification from the above responses.

Many thanks for responding!
ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
(end-start)%12 > 6  = clockwise
ozo
Most Valuable Expert 2014
Top Expert 2015

Commented:
(end-start)%12 < 6  = clockwise
(end-start)%12 > 6  = counterclockwise

Author

Commented:
You may be onto the right track but it's not correct. Using the exact examples from my previous post, using a start time of 3 o'clock:

start time=3
end time=6

(6-3)%12 = 3 (which is <6 and so clockwise)


.. and using end time=2

(2-3)%12 = -1 (which is <6 so still clockwise) This should be anti-clockwise!

Any ideas?

Author

Commented:
K I think I got it, using ozo's answer. The prob with %12 is it keeps the sign.

result = (end-start)%12

if (result < 0) result=result+12

and THEN:

result < 6  = clockwise
result > 6  = counterclockwise

ozo, I thought from my school days that % (modulus) would remove the sign, so %12 should always return 0 > x < 12, (i.e -1%12=11) however my programming language keeps the sign, should it "correctly" do so in mathematics? And if not is the above amendment to your formula correct?

Most Valuable Expert 2014
Top Expert 2015
Commented:
modulus 12 should be positive, but some programming languages implement % that can be negative,
one way to deal with that can be to change
(end-start)%12 to
(end-start+12)%12

Author

Commented:
Thanks! That's actually what I had thought.. with my amendment the software is working as expected. I am using Coldfusion, which keeps the negative sign, simply removing the sign gives the wrong result.. ya gotta add 12 (or 360 if talking in degrees)... Many thanks! Will award credit.. but just to confirm, should modulus always returns a positive number?

Author

Commented:
thanks again!
I'm happy to know your question is solved.

I feel I have exposed the idea first, but got 0 points. For future questions please consider to split points where more than one experts are helping you.

Regards.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial