Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / Return difference between wrapped angles

Author
Message
Phosphoer
16
Years of Service
User Offline
Joined: 8th Dec 2007
Location: Seattle
Posted: 13th Jul 2009 09:01
Hi guys, I'm trying to devise a function to return the difference between two angles. It's not ( I think ) as simple as it sounds.

Consider this example:
AngleA = 350;
AngleB = 10;

A simple subtraction would yield:
AngleA - AngleB = 340

However, the distance from AngleA to AngleB is not 340 degrees, it is 20.

How would I make a function which always picks the shorter distance?

Thanks for your help

Phosphoer
16
Years of Service
User Offline
Joined: 8th Dec 2007
Location: Seattle
Posted: 13th Jul 2009 09:31
Nevermind, I have found the solution with.....GOOGLE!

*Feels silly*

Here's a link to the solution I found for those interested.
http://blog.lexique-du-net.com/index.php?post/Calculate-the-real-difference-between-two-angles-keeping-the-sign

dark coder
21
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 13th Jul 2009 11:34
That method is potentially very slow because it uses while loops which I guess is a poor man's way of wrapping the values. If your angles are known to be within 0..360 or -180..180 then you can just use conditions, else I'd advise you use of modf() to wrap them beforehand.

Phosphoer
16
Years of Service
User Offline
Joined: 8th Dec 2007
Location: Seattle
Posted: 13th Jul 2009 20:31
I noticed that too :p My angles are always wrapped from 0 to 360 so I used if statements instead.

I have not heard of modf(), is that the c++ standard equivalent of dbWrapValue() ?

Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 13th Jul 2009 21:03
modf() is a function used to get the modulus of a floating point value. It essentially is used the same way the integer mod operator (%) is used except for floating point. It returns the remainder of dividing one number by another. In the context of this discussion I think the suggestion is also implied to use floating point for expressing angles. It may not be necessary for your program but you may need to use fraction of degrees. Certainly if you use the standard math/trig notation of C++ you'll need to use floating point since they require that you use radians.

Lilith, Night Butterfly
I'm not a programmer but I play one in the office
Phosphoer
16
Years of Service
User Offline
Joined: 8th Dec 2007
Location: Seattle
Posted: 13th Jul 2009 23:40
Ah I see. I actually already express my angles as floats, I just don't quite understand how I'd use modf() to wrap a value from 0 to 360.

Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 13th Jul 2009 23:47
Figure that if you mod the angle with 360 you get the remainder which must fall between 0 and 359.9999999 (or something like that.) It filters out having the needle spin around the dial (so to speak) several times and forces it to stop when it first reaches its resting point.

I'd have to double check the effect that modding has on a negative number.

Lilith, Night Butterfly
I'm not a programmer but I play one in the office
Iuzidal
15
Years of Service
User Offline
Joined: 25th Jun 2009
Location:
Posted: 15th Jul 2009 17:59 Edited at: 15th Jul 2009 18:01
Found this the other day as I was answering the same question:
http://codebase.dbp-site.com/code/find-difference-between-two-angles-25

It's DBP but easy enough to convert.



Some other nice tidbits on that site; the console was a nice addition I used.
Phosphoer
16
Years of Service
User Offline
Joined: 8th Dec 2007
Location: Seattle
Posted: 15th Jul 2009 20:49
Oh cool ^^. Thanks all for your help

Login to post a reply

Server time is: 2024-10-01 08:17:34
Your offset time is: 2024-10-01 08:17:34