Languages
[Edit]
EN

JavaScript - Math.acos() method example

8 points
Created by:
Root-ssh
177980

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.

// ONLINE-RUNNER:browser;

//                                   radians                degrees
console.log( Math.acos( -2   ) ); // NaN
console.log( Math.acos( -1   ) ); // 3.1415926535897930 ->  180
console.log( Math.acos( -0.5 ) ); // 2.0943951023931957 -> ~120
console.log( Math.acos(  0   ) ); // 1.5707963267948966 ->   90
console.log( Math.acos(  0.5 ) ); // 1.0471975511965979 ->  ~60
console.log( Math.acos(  1   ) ); // 0                  ->    0
console.log( Math.acos(  2   ) ); // NaN

console.log( Math.acos(  0.7071067811865476   ) ); // 0.7853981633974483 -> 45 deg
console.log( Math.acos(  0.8660254037844387   ) ); // 0.5235987755982987 -> 30 deg

console.log(Math.acos( Math.cos(  Math.PI / 4))); // 0.7853981633974483-> pi/4 (45deg)
console.log(Math.cos(  Math.acos( Math.PI / 4))); // 0.7853981633974483-> pi/4 (45deg)

 Another way to look at acos function:

// ONLINE-RUNNER:browser;

function calculateAngle(b, h) {
	return Math.acos(b / h);
}

/*
  |\
  | \ h
a |  \
  |__*\ <- angle
    b
*/

var a, b, h;

// a, b and h build isosceles right triangle
a = 3; 
b = a;
h = Math.sqrt(a * a + b * b);
console.log( calculateAngle(b, h) ); // 0.7853981633974483 <-  45 degrees

// a, b and h build half of equilateral triangle
a = 3; 
b = a * Math.sqrt(3); 
h = Math.sqrt(a * a + b * b);
console.log( calculateAngle(b, h) ); // 0.5235987755982987 <- ~30 degrees

// a, b and h are not able to build triangle
a = 3; 
b = a; 
h = 1;
console.log( calculateAngle(b, h) ); // NaN

1. Documentation

SyntaxMath.acos(number)
Parameters

number - integer or float number value that represents result of operation adjacent / hypotenuse on right triangle (primitive value).

number should be in the range -1 to +1.

Result

number value in radians in the range 0 to Math.PI (primitive value).

If value can not be calculated NaN is returned.

Description

acos is a static method that takes only one parameter and returns an approximation of the result of the mathematical function arccosine(x).


2. Working with degrees

// ONLINE-RUNNER:browser;

function calculateAngle(b, h) {
	var angle = Math.acos(b / h);

    return (180 / Math.PI) * angle; // rad to deg conversion
}

/*
  |\
  | \ h
a |  \
  |__*\ <- angle
    b
*/

var a, b, h;

// a, b and h build isosceles right triangle
a = 3; 
b = a;
h = Math.sqrt(a * a + b * b);
console.log( calculateAngle(b, h) ); // 45 degrees

// a, b and h build half of equilateral triangle
a = 3; 
b = a * Math.sqrt(3); 
h = Math.sqrt(a * a + b * b);
console.log( calculateAngle(b, h) ); // ~30 degrees

// a, b and h are not able to build triangle
a = 3; 
b = a; 
h = 1;
console.log( calculateAngle(b, h) ); // NaN

3. Canvas plot example

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<head>
  <style> #canvas { border: 1px solid black; } </style>
</head>
<body>
  <canvas id="canvas" width="200" height="200"></canvas>
  <script>
    
    var canvas = document.querySelector('#canvas');
    var context = canvas.getContext('2d');

    // arccosine chart range
    var x1 = -1.0;
    var x2 = +1.0;
    var y1 =  0;
    var y2 = +Math.PI

    var dx = 0.01;

    var xRange = x2 - x1;
    var yRange = y2 - y1;

    function calculatePoint(x) {
      var y = Math.acos(x);

      // chart will be reversed horizontaly because of reversed canvas pixels

      var nx = (x - x1) / xRange;       // normalized x
      var ny = 1.0 - (y - y1) / yRange; // normalized y
      
      var point = {
        x: nx * canvas.width,
        y: ny * canvas.height
      };

      return point;
    }

    console.log('x range: <' + x1 + '; ' + x2 + '>');
    console.log('y range: <' + y1 + '; ' + y2 + '> // angles in radians');

    var point = calculatePoint(x1);
    
    context.beginPath();
    context.moveTo(point.x, point.y);

    for (var x = x1 + dx; x < x2; x += dx) {
      point = calculatePoint(x);
      context.lineTo(point.x, point.y);
    }

    point = calculatePoint(x2);
    context.lineTo(point.x, point.y);
    context.stroke();

  </script>
</body>
</html>

References

  1. Inverse trigonometric functions - Wikipedia

Alternative titles

  1. JavaScript - Math.acos() documentation with examples
  2. js - Math.acos() method documentation with examples
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

JavaScript - Math documentation (EN)

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join