Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Consider the following triangle :

enter image description here

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!

like image 572
Sprout Coder Avatar asked Jan 05 '15 17:01

Sprout Coder


People also ask

How do you find the angle between two points in Javascript?

The Math. atan2() function returns the angle in the plane (in radians) between the positive x-axis and the ray from (0, 0) to the point (x, y), for Math. atan2(y, x) .


1 Answers

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º

like image 82
Umagon Avatar answered Oct 04 '22 22:10

Umagon