Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

272
Visualizações
JavaScript - Find turn direction from two angles

I have two normalized angles (between 0 and 360 degrees) and i want to find the shortest turn direction from point a to point b. (clockwise or counterclockwise). a or b can be anywhere on the circle, so the function should work in both way: if a is smaller and if a is larger than b.

I am wrote the following function, which works fine except when the shortest distance crosses the 0 degree mark:

function clockwise(a, b){

    return a < b;     

}

The function return true if it is clockwise and false if the direction is counterclockwise.

How can I get this to work for distances that cross the 0 degree angle? I am looking for an implementation specifically in JavaScript, since I wasn't able to translate any of the mathematical explanations I found. Thanks in advance!

enter image description here

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Relative to point a,

  • Is clockwise, If b is present is in the next 180
  • anticlockwise otherwise
function clockwise(a, b){
    let theta1 = b-a;
    return theta1 >= 0 && theta1 <= 180; // return true if clockwise
}
about 4 years ago · Juan Pablo Isaza Relatório

0

There's probably a more elegant solution, but I think this gives the expected results?
(if a=60, b=230 the difference is 170 and clockwise=true is correct..?!)

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

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda