Find Inverse Tangent?
Asked Answered
P

4

11

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.

Phosphaturia answered 24/2, 2017 at 14:1 Comment(1)
Do you mean arctanget?Chaumont
M
23

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));
Merman answered 24/2, 2017 at 14:5 Comment(4)
what do I put into the atan? is it all 3 x's and y'sPhosphaturia
If a ratio is being used, it is better to use Math.atan2(y, x). It gets the correct quadrant and avoids possible division by zero problems.Blackman
@AndrewMorton I don't see how atan2() would avoid the division by zero edge case this needs to be checked first to see if ax and bx are identical and then to see if the two points A and B are identicalMerman
@Merman The function does the check itself, and works correctly for all four quadrants: Math.atan2 documentation.Blackman
P
2

I found this short and simple:

const calculateAngle = (width, height) => Math.atan(width/height)/(Math.PI / 180) // Angle in degrees
Posen answered 6/6, 2022 at 13:12 Comment(0)
T
0

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

Til answered 24/2, 2017 at 14:5 Comment(0)
C
0

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

Note: As of Dec 5, 2018, the repository has been archived and processingjs.org redirects there.

With the development of p5js and the API advances in Processing itself, as well as Processing.js itself having been in maintenance mode for quite a few years now, this project has been archived as of December 2018.

Processing.js would like to thank everyone who contributed over the years: it's been an amazing project! The code will still be available in read-only mode, no releases will be pulled from any of the places it was distributed through, but the last version is, and will forever be, v1.6.6.

Thank you for your support, and happy coding (with newer solutions)!

Coblenz answered 24/2, 2017 at 16:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.