我有两个角度,currentAngle和goalAngle。两个角度都在[0,360]之间。我需要一行代码,告诉我从currentAngle开始旋转的方式,以最便宜的方式达到goalAngle,旋转+1或-1度的成本为1美元。
这是我的代码的样子:
if( ??? ) { rotateClockwise(); }
else { rotateCounterclockwise(); }
答案 0 :(得分:0)
<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">
答案 1 :(得分:0)
// 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); } } }