Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Compass Headings
Message
From
20/11/2016 10:07:23
 
 
General information
Forum:
Games
Category:
Trivia
Miscellaneous
Thread ID:
01643702
Message ID:
01643728
Views:
26
>>>>>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)
>>>>
>>>>Possibility one : move1 = the difference between oldPos and newPos gives a distance and a direction
>>>>
>>>>The other possibility (move2) is to take the opposite direction, where the distance is 360 - abs(move1)
>>>>
>>>>Take the move with the minimum absolute value
>>>
>>>May be being thick but I don't see how that works. Can you demonstrate with these examples:
>>>
>>>Old New
>>>
>>>350 2 (Should be clockwise)
>>>10 190 (Doesn't matter)
>>>20 190 (Clockwise)
>>>10 350 (Anti-clockwise)
>>>90 70 (Anti-clockwise)
>>>5 190 (Anti-clockwise)
>>>2 350 (Anti-clockwise)
>>>
>>>I don't care about the length of the arc - just in which direction to move.
>>
>>Our definition of direction differs. To me 2 to 350 is clockwise
>
>No. Shortest distance between 2 and 350 degrees would be anti-clockwise (12 degrees) ?
Yeah - well - we have different clocks then


>>
>>	? GetRotation(20, 335)
>>	? GetRotation(335, 20)
>>	? GetRotation(350, 2)
>>	? GetRotation(20, 190)
>>	? GetRotation(2, 350)
>>
>>
>>
>>function GetRotation( from, to)
>>	
>>	from = mod(from, 360)
>>	to = mod(to, 360)
>>	
>>	local move1, move2, move, direction
>>	
>>	move1 = to - from
>>	move2 = (360 - abs(move1)) * (-sign(move1 ))
>>	
>>	move = iif(abs(move1) < abs(move2), move1, move2)
>>	direction = iif(sign(move) <=0, 'clockwise', 'counter-clockwise')
>>	
>>	? from, '->', to, ' degrees', abs(move), direction 
>>endfunc
>
>
>>*_______________________________________________________________________________
>>
> I'll try it. Al's solution works though.....
So does mine, I think. You'll have to change the direction to fit your definition I guess
Gregory
Previous
Reply
Map
View

Click here to load this message in the networking platform