Trigonometry
Examples
Filter
console.log(Math.hypot(3, 4));
// expected output: 5
console.log(Math.hypot(5, 12));
// expected output: 13
function getCircleY(radians, radius) {
return Math.sin(radians) * radius;
}
console.log(getCircleY(1, 10));
// expected output: 8.414709848078965
console.log(getCircleY(2, 10));
// expected output: 9.092974268256818
console.log(getCircleY(Math.PI, 10));
// expected output: 1.2246467991473533e-15
Math.sin(0); // 0
Math.sin(1); // 0.8414709848078965
Math.sin(Math.PI / 2); // 1
function getCircleX(radians, radius) {
return Math.cos(radians) * radius;
}
console.log(getCircleX(1, 10));
// expected output: 5.403023058681398
console.log(getCircleX(2, 10));
// expected output: -4.161468365471424
console.log(getCircleX(Math.PI, 10));
// expected output: -10
Math.cos(0); // 1
Math.cos(1); // 0.5403023058681398
Math.cos(Math.PI); // -1
Math.cos(2 * Math.PI); // 1
function getTanFromDegrees(degrees) {
return Math.tan(degrees * Math.PI/180);
}
console.log(getTanFromDegrees(0));
// expected output: 0
console.log(getTanFromDegrees(45));
// expected output: 0.9999999999999999
console.log(getTanFromDegrees(90));
// expected output: 16331239353195370
Math.tan(1); // 1.5574077246549023
// Calculates angle of a right-angle triangle in radians
function calcAngle(opposite, hypotenuse) {
return Math.asin(opposite / hypotenuse);
}
console.log(calcAngle(6, 10));
// expected output: 0.6435011087932844
console.log(calcAngle(5, 3));
// expected output: NaN
Math.asin(-2); // NaN
Math.asin(-1); // -1.5707963267948966 (-pi/2)
Math.asin(0); // 0
Math.asin(0.5); // 0.5235987755982989
Math.asin(1); // 1.5707963267948966 (pi/2)
Math.asin(2); // NaN
// Calculates angle of a right-angle triangle in radians
function calcAngle(adjacent, hypotenuse) {
return Math.acos(adjacent / hypotenuse);
}
console.log(calcAngle(8, 10));
// expected output: 0.6435011087932843
console.log(calcAngle(5, 3));
// expected output: NaN
Math.acos(-2); // NaN
Math.acos(-1); // 3.141592653589793
Math.acos(0); // 1.5707963267948966
Math.acos(0.5); // 1.0471975511965979
Math.acos(1); // 0
Math.acos(2); // NaN
// Calculates angle of a right-angle triangle in radians
function calcAngle(opposite, adjacent) {
return Math.atan(opposite / adjacent);
}
console.log(calcAngle(8, 10));
// expected output: 0.6747409422235527
console.log(calcAngle(5, 3));
// expected output: 1.0303768265243125
Math.atan(1); // 0.7853981633974483
Math.atan(0); // 0
Math.atan(-0); // -0
Math.atan(Infinity); // 1.5707963267948966
Math.atan(-Infinity); // -1.5707963267948966
// The angle that the line [(0,0);(x,y)] forms with the x-axis in a Cartesian coordinate system
Math.atan(y / x);
Computes the arc tangent of y/x, using the signs of the two to determine the quadrant of the return value.
function calcAngleDegrees(x, y) {
return Math.atan2(y, x) * 180 / Math.PI;
}
console.log(calcAngleDegrees(5, 5));
//expected output: 45
console.log(calcAngleDegrees(10, 10));
//expected output: 45
console.log(calcAngleDegrees(0, 10));
//expected output: 90
Math.atan2(90, 15); // 1.4056476493802699
Math.atan2(15, 90); // 0.16514867741462683
Math.atan2(±0, -0); // ±PI.
Math.atan2(±0, +0); // ±0.
Math.atan2(±0, -x); // ±PI for x > 0.
Math.atan2(±0, x); // ±0 for x > 0.
Math.atan2(-y, ±0); // -PI/2 for y > 0.
Math.atan2(y, ±0); // PI/2 for y > 0.
Math.atan2(±y, -Infinity); // ±PI for finite y > 0.
Math.atan2(±y, +Infinity); // ±0 for finite y > 0.
Math.atan2(±Infinity, x); // ±PI/2 for finite x.
Math.atan2(±Infinity, -Infinity); // ±3*PI/4.
Math.atan2(±Infinity, +Infinity); // ±PI/4.
console.log(Math.sinh(0));
// expected output: 0
console.log(Math.sinh(1));
// expected output: 1.1752011936438014
console.log(Math.sinh(-1));
// expected output: -1.1752011936438014
console.log(Math.sinh(2));
// expected output: 3.626860407847019
Math.sinh(0); // 0
Math.sinh(1); // 1.1752011936438014
console.log(Math.cosh(0));
// expected output: 1
console.log(Math.cosh(1));
// expected output: 1.543080634815244 (approximately)
console.log(Math.cosh(-1));
// expected output: 1.543080634815244 (approximately)
console.log(Math.cosh(2));
// expected output: 3.7621956910836314
Math.cosh(0); // 1
Math.cosh(1); // 1.5430806348152437
Math.cosh(-1); // 1.5430806348152437
console.log(Math.tanh(-1));
// expected output: -0.7615941559557649
console.log(Math.tanh(0));
// expected output: 0
console.log(Math.tanh(Infinity));
// expected output: 1
console.log(Math.tanh(1));
// expected output: 0.7615941559557649
Math.tanh(0); // 0
Math.tanh(Infinity); // 1
Math.tanh(1); // 0.7615941559557649
console.log(Math.asinh(1));
// expected output: 0.881373587019543
console.log(Math.asinh(0));
// expected output: 0
console.log(Math.asinh(-1));
// expected output: -0.881373587019543
console.log(Math.asinh(2));
// expected output: 1.4436354751788103
console.log(Math.acosh(0.999999999999));
// expected output: NaN
console.log(Math.acosh(2.5));
// expected output: 1.566799236972411
Math.acosh(-1); // NaN
Math.acosh(0); // NaN
Math.acosh(0.5); // NaN
Math.acosh(1); // 0
Math.acosh(2); // 1.3169578969248166
console.log(Math.atanh(-1));
// expected output: -Infinity
console.log(Math.atanh(0));
// expected output: 0
console.log(Math.atanh(0.5));
// expected output: 0.549306144334055 (approximately)
console.log(Math.atanh(1));
// expected output: Infinity
Math.atanh(-2); // NaN
Math.atanh(-1); // -Infinity
Math.atanh(0); // 0
Math.atanh(0.5); // 0.5493061443340548
Math.atanh(1); // Infinity
Math.atanh(2); // NaN