Javascript (pure) - Find exact triangle angle according to trigonometric ratio table

Asked

Viewed 2,176 times

2

I wonder if there is a way to find the exact angle of a triangle without depending on the table.

Tablea de razões trigonométricas

In my program, I’m taking the value in radian and turning it into degrees, but the value does not match the table.

My triangle is 233.5px high and 180 wide. When performing the sine, I find an angle of 45, but in the table would be 52. And the angle I need to find is that of the table. The reason for the question is that I must reach these values through the program because I cannot define them.

1 answer

2


You can use the function atan2 for this. Test below:

console.log(Math.atan2(233.5, 180) * 180 / Math.PI);

The console output is "52.37216305431494". As you may know, the * 180 / Math.PI is to convert the radian response to degrees.

Note that the parameters of atan2 are the y first and then the x. In your case, y is the height of the triangle (233.5) and x is the width (180).

To understand how the function works atan2, see that other question I answered.

  • 1

    Oops, thank you so much bro! It worked perfectly here, I’ll just read there to understand why. :)

  • @Pedrolucasflor Just one remark, in your question you said "233.5px" high. If you are searching for this value by the attribute some element displayed in the browser is very likely to get a top up and this influences the result.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.