KO EN

Calculating trigonometric function in Javascript

by Lovefield

Java Script /

Hi, I'm Lovefield.

I recently had to use trigonometric functions. I had to change the condition rendered on the screen, but I had to calculate the value with the angle. In this article, I’m going to write how to use trigonometric functions in Javascript.

Frame 21.png

Let’s say you have a right triangle like the one above. The calculation formula depends on which value you know. Let’s set up a formula in order.

1. If you know the angle A and the length a of the base
The equation for finding height b and hypotenuse c is as follows.

Formula for finding hypotenuse c : a / Math.cos((A / 180) * Math.PI)
Formula for finding height b : a * Math.tan((A / 180) * Math.PI)

2. If you know the angle A and the height b
The equation for finding base line a and hypotenuse c is as follows.

Formula for finding base line a : b / Math.tan((A / 180) * Math.PI)
Formula for finding hypotenuse c : b / Math.sin((A / 180) * Math.PI)

3. If you know the angle A and hypotenuse c
The equation for finding base line a and height b is as follows.

Formula for finding base line a : c * Math.cos((A / 180) * Math.PI)
Formula for finding height b : c * Math.sin((A / 180) * Math.PI)

4. If you know the hypotenuse c and height b
Find out the angle A first and then use formula 2 or 3.

Formula for finding angle A : (Math.asin(b / c) * 180) / Math.PI

5. If you know the hypotenuse c and the length a of base
Find out the angle A first and then use formula 1 or 3.

Formula for finding angle A : (Math.acos(a / c) * 180) / Math.PI

6. If you know the height b and the length a of base
Find you the angle A first and then use formula 1 or 2.

Formula for finding angle A : (Math.atan(b / a) * 180) / Math.PI

For angle B, you can use the features of the triangle to obtain it. The sum of the triangular internal angles is always maintained at 180 degrees. So you just find angle A. '90 degrees' minus 'A angle'. That's all.

There are precautions when using formulas 1, 2, and 3. The trigonometric functions have special angles, each with a fixed value of sine, cosine, and tangent. The fixed values are as follows.

Angle of 30
- sine = 1 / 2
- cosine = Math.sqrt(3) / 2
- tangent = 1 / Math.sqrt(3)

Angle of 45
- sine = Math.sqrt(2) / 2
- cosine = Math.sqrt(2) / 2
- tangent = 1

Angle of 60
- sine = Math.sqrt(3) / 2
- cosine = 1/ 2
- tangent = Math.sqrt(3)

Description of the JavaScript Math method used in this article
- Math.cos = Return cosine value. Argument is radian value.
- Math.sin = Return sine value. Argument is radian value.
- Math.tan = Return tangent value. Argument is radian value.
- Math.acos = Return radian value. Argument is cosine value.
- Math.asin = Return radian value. Argument is sine value.
- Math.atan = Return radian value. Argument is tangent value.
- Math.PI = Return pi value.
- Math.sqrt = Return root value. ex) Math.sqrt(4) // 2
- Equation of radian = (degrees / 180) * Math.PI
- Radian inverse equation = (radian * 180) / Math.PI
- What is Radian? The value for the circular method, which is the method of expressing the angle by the length of the circular.

Author

Lovefield

Lovefield

Web Front-End developer

하고싶은게 많고, 나만의 서비스를 만들고 싶은 변태스러운 개발자입니다.

로그인

디코에 오신 것을 환영해요!
전문가들의 수많은 아티클 창고 🤓