How to note tangent to the power -1 in JS - javascript

I want to calculate direction of mousemove in angles. I found formula but i dont know how to note it:
theta = tan^–1(y/x)
Can you help me? I used this page for solution https://www.dummies.com/education/science/physics/how-to-find-a-vectors-magnitude-and-direction/

Do you mean arc tangent? There is a function called atan() for that, which gives you your angle in radians.
vat theta = Math.atan(y/x)

Related

three.js - get X,Y,Z rotation values from Matrix4

In three.js is there a way to tell/compute/get specific rotation values (X/Y/Z) from the Matrix4 in degrees (float), please?
As unfortunately among many of its functions there is no such directly implemented.
Solution to this question was actually "simple" (at least for an experienced three.js user out there as I was told by such user elsewhere): use three.js Euler class .setFromRotationMatrix() function and use correct order (in my case I first wrongly thought it is simply XYZ while it actually was YZX - beware with this, you have to get this part right!) - and yea, that's it!
I think your question is "Q: How do I convert radians to degrees?"
var radians = degrees * Math.PI / 180.0;
var degrees = radians * 180.0 / Math.PI;

how does atan2 work? which angle is actually calculated?

As you can see in the picture, I have a line and two points(p1 and p4). what I need to do is to get snapped point of p1/p4 on the line and then use atan2 to calculate the angle between (p1 and p2) and (p3 and p4). Now, I have two formulas:
var anglep1p2 = Math.atan2(p2[1] - p1[1], p2[0] - p1[0]) * 180 / Math.PI;
var anglep4p3 = Math.atan2(p4[1] - p3[1], p4[0] - p3[0]) * 180 / Math.PI;
anglep1p2 is calculated 103.66797855556482
anglep4p3 is calculated -76.74971541138642
I wonder how does atan2 calculate those values?
thanks for any help
These answers do make sense. You are sort of calculating a the angle of a single line, starting from the positive x-axis. The way you calculate anglep1p2, it corresponds to the line drawn from p1 to p2.
If you plunk the origin of a coordinate system at the starting point p1 (you put it at p2 in your diagram), then the number you get should be the rotation from the positive x-axis to the line you drew - a bit over 90 degrees makes intuitive sense.
Your second result is flipped from your first (notice you used p4/p3 in the same order as your variable name, whereas you reversed this order in the p1/p2 case). To avoid confusion, I'd use the p1/p2 case to gain understanding, then apply it the same way to the other case once you know what you want.
If you have a specific geometry/relationship problem you need to figure out, you can provide the details and I might be able to help more specifically.

Find Inverse Tangent?

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.
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));
I found this short and simple:
const calculateAngle = (width, height) => Math.atan(width/height)/(Math.PI / 180) // Angle in degrees
Try using Math.atan (outputs angle in radians) and some trigonometry.
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()
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)!

How to draw in polar coordinates with P5.js?

There is a function angleMode(mode); in P5 documentation that sets mode either RADIANS or DEGREES but I can't figure out how to use it or how to draw in polar coordinates in p5.js. Does anybody know how to do it with p5.js?
angleMode() changes wheter p5.js interprets your angle values as radians and degrees. It has no impact on the way drawing is done.
You can just use trigonometric functions to translate between polar and cartesian.
var x = r * cos(angle)
var y = r * sin(angle)
point(x, y);
If you need more information, you can see this example in p5.js documentation: https://p5js.org/examples/math-polartocartesian.html
The coding train also has a video tutorial on this: https://www.youtube.com/watch?v=N633bLi_YCw
RADIANS and DEGREES are the constants like 0 and 1. Therefore:
angleMode(DEGREES);// sets the mode

How do I calculate the angle of a right triangle using the Javascript Math library?

Consider the following triangle :
I want to calculate angle X
I have the following :
var opposite = 2.5;
var hypotenuse = 5;
var sinOfAngleX = opposite / hypotenuse; // 0.5
So now I know that Math.sin(valueofAngleX) would return 0.5.
But how do I get the value of an angle if I know the sine of the angle, using the Math() library?
According to this tutorial, I need to do :
var angleX = sin to the power of negative one of 0.5
but I don't know how to translate that into JS.
I tried :
var angleX = Math.pow(Math.sin(sinOfAngleX), -1);
but it returns 2.085829642933488 which is obviously wrong. The correct answer should be 30
And in case all of the above is wrong to begin with, does anyone know how I can calculate angle X correctly using the JS Math() library?
Thanks!
You can know the angle of any sin with this formula:
Math.asin(sinOfAngleX) * 180/Math.PI
With sinOfAngleX = 0.5, Math.asin(sinOfAngleX) would give 0.5235987755982989. This is expressed in radians. To pass it to degrees you can multiply by 180/Math.PI, which results in 30º
In ES6 you can use the build it method from Math Object
Math.hypot(3, 4)

Categories

Resources