7

I'm new to Javascript and I'm trying to use inverse tangent to find the angle in degrees between a line and the x axis on an elevated y. I don't see any command for it so I really need some help.

PLP123
  • 167
  • 2
  • 2
  • 10

3 Answers3

17

Use Math.atan() function and then Math.toDegrees() multiply it by 180/Math.PI to convert radians to degrees Found the answer it here

Later edit:

Here is an example of angle calculation between a line defined by 2 points (A and B) and the X axis. The elevation of the second line (parallel with the X axis) is irrelevant since the angle stays the same.

 /*
 * Calculates the angle between AB and the X axis
 * A and B are points (ax,ay) and (bx,by)
 */
function getAngleDeg(ax,ay,bx,by) {
  var angleRad = Math.atan((ay-by)/(ax-bx));
  var angleDeg = angleRad * 180 / Math.PI;
  
  return(angleDeg);
}

console.log(getAngleDeg(0,1,0,0));
Community
  • 1
  • 1
Ionut
  • 1,635
  • 4
  • 23
  • 45
0

Try using Math.atan (outputs angle in radians) and some trigonometry.

0

Questions like these are best answered by the reference. I see a bunch of trigonometric functions there, including:

  • acos()
  • asin()
  • atan()
  • atan2()
  • cos()
  • degrees()
  • radians()
  • sin()
  • tan()
Kevin Workman
  • 40,517
  • 9
  • 64
  • 103