最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin3浏览0评论

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!

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!

Share Improve this question edited Jan 5, 2015 at 17:45 Kawd asked Jan 5, 2015 at 17:33 KawdKawd 4,45011 gold badges40 silver badges84 bronze badges 2
  • At the very least, it looks like you're taking the sine of the sineOfAngleX, which is wrong to start with. (thanks whoever -1'd me, I meant that to be a comment rather than an answer) – Paul Commented Jan 5, 2015 at 17:45
  • sin^-1(a) here does not mean 1/sin(a), but rather "the inverse function of sin"(a), thus arcsin (a.k.a. asin). To write 1/sin(a) with ^-1, one would add parenthesis : (sin(a))^-1. Note that sin(a^-1) means sin(1/a), and sin(a)^-1 is ambiguous, thus should be avoided. – Cimbali Commented Jan 6, 2015 at 0:56
Add a comment  | 

2 Answers 2

Reset to default 20

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)

发布评论

评论列表(0)

  1. 暂无评论