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.
Asked
Active
Viewed 1.3k times
7
-
Do you mean arctanget? – epascarello Feb 24 '17 at 14:03
3 Answers
17
Use Math.atan() function and then multiply it by Math.toDegrees()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));
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
-
-
1@DaMahdi03 Yes, I know. Please notice the [tag:processing.js] tag in the question. – Kevin Workman Oct 15 '18 at 02:29
-
Maybe the person writing the question didn't know too much about JavaScript? – Da Mahdi03 Oct 15 '18 at 02:32
-
2@DaMahdi03 Sorry, I'm not sure what you're trying to tell me. The question was tagged with [tag:processing.js], so I answered with Processing.js. Seems pretty reasonable to me. – Kevin Workman Oct 15 '18 at 02:36