EN
JavaScript - Math.acos() method example
8 points
The Math.acos
function returns number in radians in the range 0 to Math.PI. Based on this function we are able to calculate inverted cosine function value.
xxxxxxxxxx
1
// radians degrees
2
console.log( Math.acos( -2 ) ); // NaN
3
console.log( Math.acos( -1 ) ); // 3.1415926535897930 -> 180
4
console.log( Math.acos( -0.5 ) ); // 2.0943951023931957 -> ~120
5
console.log( Math.acos( 0 ) ); // 1.5707963267948966 -> 90
6
console.log( Math.acos( 0.5 ) ); // 1.0471975511965979 -> ~60
7
console.log( Math.acos( 1 ) ); // 0 -> 0
8
console.log( Math.acos( 2 ) ); // NaN
9
10
console.log( Math.acos( 0.7071067811865476 ) ); // 0.7853981633974483 -> 45 deg
11
console.log( Math.acos( 0.8660254037844387 ) ); // 0.5235987755982987 -> 30 deg
12
13
console.log(Math.acos( Math.cos( Math.PI / 4))); // 0.7853981633974483-> pi/4 (45deg)
14
console.log(Math.cos( Math.acos( Math.PI / 4))); // 0.7853981633974483-> pi/4 (45deg)
Another way to look at acos
function:
xxxxxxxxxx
1
function calculateAngle(b, h) {
2
return Math.acos(b / h);
3
}
4
5
/*
6
|\
7
| \ h
8
a | \
9
|__*\ <- angle
10
b
11
*/
12
13
var a, b, h;
14
15
// a, b and h build isosceles right triangle
16
a = 3;
17
b = a;
18
h = Math.sqrt(a * a + b * b);
19
console.log( calculateAngle(b, h) ); // 0.7853981633974483 <- 45 degrees
20
21
// a, b and h build half of equilateral triangle
22
a = 3;
23
b = a * Math.sqrt(3);
24
h = Math.sqrt(a * a + b * b);
25
console.log( calculateAngle(b, h) ); // 0.5235987755982987 <- ~30 degrees
26
27
// a, b and h are not able to build triangle
28
a = 3;
29
b = a;
30
h = 1;
31
console.log( calculateAngle(b, h) ); // NaN
Syntax | Math.acos(number) |
Parameters |
|
Result |
If value can not be calculated |
Description |
|
xxxxxxxxxx
1
function calculateAngle(b, h) {
2
var angle = Math.acos(b / h);
3
4
return (180 / Math.PI) * angle; // rad to deg conversion
5
}
6
7
/*
8
|\
9
| \ h
10
a | \
11
|__*\ <- angle
12
b
13
*/
14
15
var a, b, h;
16
17
// a, b and h build isosceles right triangle
18
a = 3;
19
b = a;
20
h = Math.sqrt(a * a + b * b);
21
console.log( calculateAngle(b, h) ); // 45 degrees
22
23
// a, b and h build half of equilateral triangle
24
a = 3;
25
b = a * Math.sqrt(3);
26
h = Math.sqrt(a * a + b * b);
27
console.log( calculateAngle(b, h) ); // ~30 degrees
28
29
// a, b and h are not able to build triangle
30
a = 3;
31
b = a;
32
h = 1;
33
console.log( calculateAngle(b, h) ); // NaN
xxxxxxxxxx
1
2
<html>
3
<head>
4
<style> #canvas { border: 1px solid black; } </style>
5
</head>
6
<body>
7
<canvas id="canvas" width="200" height="200"></canvas>
8
<script>
9
10
var canvas = document.querySelector('#canvas');
11
var context = canvas.getContext('2d');
12
13
// arccosine chart range
14
var x1 = -1.0;
15
var x2 = +1.0;
16
var y1 = 0;
17
var y2 = +Math.PI
18
19
var dx = 0.01;
20
21
var xRange = x2 - x1;
22
var yRange = y2 - y1;
23
24
function calculatePoint(x) {
25
var y = Math.acos(x);
26
27
// chart will be reversed horizontaly because of reversed canvas pixels
28
29
var nx = (x - x1) / xRange; // normalized x
30
var ny = 1.0 - (y - y1) / yRange; // normalized y
31
32
var point = {
33
x: nx * canvas.width,
34
y: ny * canvas.height
35
};
36
37
return point;
38
}
39
40
console.log('x range: <' + x1 + '; ' + x2 + '>');
41
console.log('y range: <' + y1 + '; ' + y2 + '> // angles in radians');
42
43
var point = calculatePoint(x1);
44
45
context.beginPath();
46
context.moveTo(point.x, point.y);
47
48
for (var x = x1 + dx; x < x2; x += dx) {
49
point = calculatePoint(x);
50
context.lineTo(point.x, point.y);
51
}
52
53
point = calculatePoint(x2);
54
context.lineTo(point.x, point.y);
55
context.stroke();
56
57
</script>
58
</body>
59
</html>