Algorithm or formula for the shortest direction of movement between two degrees in a circle?

Given two degrees in a circle of 360 degrees. Lets call them Source and Destination.

For example, a source can be 120 degrees and a destination 30 degrees.

Is there an elegant solution to the question of which direction of movement from source to destination is shorter, i.e. is it shorter clockwise (increase in degrees) or counterclockwise (decrease in degrees)?

For example, with the powers given above, then the solution would be: Go counterclockwise. On the other hand, with a source of both 350 and Destination as 20, then the solution would be: Go clockwise.

+5
source share
5 answers

Calculate the difference, then normalize it to +/- 180. Positive numbers indicate the direction of movement in the direction of increasing the angle (clockwise in your case).

+4
source
if ((dest - source + 360) % 360 < 180)
  // clockwise
else
  // anti-clockwise

By the way, your agreement that clockwise == "increasing degrees" is the opposite of the Trigonometry 101 agreement that the rest of the world is using, and therefore confusing (was for me, anyway).

+10
source

, . 0 - 360.

function shortestDistDegrees(start, stop) {      
  const modDiff = (stop - start) % 360;
  let shortestDistance = 180 - Math.abs(Math.abs(modDiff) - 180);
  return (modDiff + 360) % 360 < 180 ? shortestDistance *= 1 : shortestDistance *= -1;
}

shortestDistDegrees(50, -20)   // Output: -70
shortestDistDegrees(-30, -370) // Output: 20
shortestDistDegrees(160, -710) // Output: -150
+1

, :

rotSpeed = 0.25;                      //arbitrary speed of rotation

angleDiff      = 180-abs(abs(source-dest)-180);            //find difference and wrap
angleDiffPlus  = 180-abs(abs((source+rotSpeed)-dest)-180); //calculate effect of adding
angleDiffMinus = 180-abs(abs((source-rotSpeed)-dest)-180); //           ... subtracting

if(angleDiffPlus < angleDiff){        //if adding to ∠source reduces difference
    source += rotSpeed;               //add to ∠source
}else if(angleDiffMinus < angleDiff){ //if SUBTRACTING from ∠source reduces difference
    source -= rotSpeed;               //subtract from ∠source
}else{                                //if difference smaller than rotation speed
    source = dest;                    //set ∠source to ∠destination
}

Under the "wrapping" of the corner, we can calculate the difference. Then we can check the current difference compared to the forecasts to see which direction will actually reduce the difference.

0
source

The general answer is here: "Modulo arithmetics". You can read about it, it's worth it.

-3
source

All Articles