玩家应该向哪个方向旋转?

Which direction should the player rotate?

我有两个角度,currentAngle 和 goalAngle。两个角度都在 [0, 360] 之间。我需要一行代码来告诉我以哪种方式旋转,从 currentAngle 开始,以最便宜的方式达到 goalAngle,其中旋转 +1 或 -1 度需要 1 美元。

我的代码如下所示:

if( ??? ) { rotateClockwise(); }
else { rotateCounterclockwise(); }

<script type="text/javascript">
function rotateClockwise()
{
 alert('Clockwise');
}
function rotateCounterclockwise()
{
 alert('Counterclockwise');
}

function calc(currentAngle,goalAngle)
{
 if(isNaN(currentAngle)||isNaN(goalAngle)){alert('invalid angles');return false;}
 
 var diff=currentAngle-goalAngle;
 while (diff < -180) diff += 360;
 while (diff > 180) diff -= 360;
 if(diff<0){
  rotateClockwise();
 }else{
  rotateCounterclockwise();
 }
}
</script>
<input type="number" id="c" value="45" step="1">
<input type="number" id="g" value="75" step="1">
<input type="button" onclick="calc(parseFloat(document.getElementById('c').value),parseFloat(document.getElementById('g').value))" value="Calc">

        // Given the player's rotation, calculate which direction the player should rotate, if any.
    if(Math.abs(goalAngle - super.getRotY()) > ROTATION_ACCURACY){
        if(Math.abs(goalAngle - super.getRotY()) < 180){
            if(goalAngle - super.getRotY() < 0){ super.increaseRotation(0, -super.getTurnSpeed(), 0); }
            else{ super.increaseRotation(0, super.getTurnSpeed(), 0); } }
        else{ 
            if(goalAngle - super.getRotY() < 0){ super.increaseRotation(0, super.getTurnSpeed(), 0); }
            else{ super.increaseRotation(0, -super.getTurnSpeed(), 0); } } }