JavaScript - 从两个角度寻找转弯方向

JavaScript - Find turn direction from two angles

我有两个归一化角度(在 0 到 360 度之间),我想找到从点 a 到点 b[=32 的最短转弯方向=]. (顺时针或逆时针)。 ab 可以在圆上的任何位置,因此该函数应该可以双向工作: if a 较小,如果 a 大于 b.

我写了以下函数,除了最短距离超过 0 度标记时,它工作正常:

function clockwise(a, b){

    return a < b;     

}

函数return true如果是顺时针,false如果方向是逆时针。

我怎样才能让它在跨越 0 度角的距离上工作?我正在寻找具体在 JavaScript 中的实现,因为我无法翻译我找到的任何数学解释。提前致谢!

相对于a点,

  • 是顺时针,如果b存在就是在接下来的180
  • 否则逆时针
function clockwise(a, b){
    let theta1 = b-a;
    return theta1 >= 0 && theta1 <= 180; // return true if clockwise
}

可能有更优雅的解决方案,但我认为这给出了预期的结果?
(如果 a=60, b=230 差异是 170clockwise=true 是正确的..?!)

function clockwise(a, b) {
  let clockwise, diff
  if (b > a) {
    diff = b - a
    clockwise = diff >= 0 && diff <= 180
  } else {
    diff = a - b
    clockwise = diff >= 180
  }
  return clockwise
}

console.log(clockwise(60,230))  // true
console.log(clockwise(220,150))  // false
console.log(clockwise(40,300))  // false
console.log(clockwise(120,214))  // true