>>>General sort of question:
>>>
>>>I'm drawing a compass and need to determine the 'shortest' direction in which to rotate the compass (clockwise or anti-clockwise) between old and new bearings (in degrees).
>>>In most cases it is simple but when the old and new values span the 0 degree point those solutions don't work.....
>>>
>>>Anyone got a foolproof algorithm (language doesn't matter - just the logic)
>>
>>A quick suggestion (VFP code):
>>
SIGN(ABS(a1 - a2) - 180)
>>
>>-1 = counter-clockwise
>>1 = clockwise
>>0 = either
>>Update: I just realized it doesn't work if angles in the 3rd and 4th quadrant. Sorry, Sat night here...
>
>Thanks for the try :-}
FUNCTION AzimuthChange
LPARAMETERS ;
tnAzimuth1 ;
, tnAzimuth2
* If we're only concerned about the *change* in Azimuth,
* we can rotate our frame of reference so tnAzimuth1 is always 0 (due north)
* so we can avoid problems with Azimuth changes that span 0
LOCAL ;
lnFrameRotation
* Calculate how much we have to rotate the frame of reference so that m.tnAzimuth1 is at 0 degrees:
m.lnFrameRotation = MOD( 360 - m.tnAzimuth1, 360 ) && MOD() handles case where m.tnAzimuth1 = 0
* Add that amount to the second Azimuth:
m.tnAzimuth2 = MOD( m.tnAzimuth2 + m.lnFrameRotation, 360 )
RETURN IIF( m.tnAzimuth2 > 180, m.tnAzimuth2 - 360, m.tnAzimuth2 )
Regards. Al
"Violence is the last refuge of the incompetent." -- Isaac Asimov
"Never let your sense of morals prevent you from doing what is right." -- Isaac Asimov
Neither a despot, nor a doormat, be
Every app wants to be a database app when it grows up