Top community members
All Wiki Articles Create Wiki Article

Have you ever heard about encyclopedia for code?

What about: Wiki for Code project?

* because I've tried to share code and my solutions on different services and it was removed or I was unwelcomed
   - maybe it wasn't good place, but this one is.

if you think you have solution that can help someone, you are welcome to share this solution - Click here

JavaScript - Math.atan() method example

0 contributions
13 points

The Math.atan function returns number in radians in the range -Math.PI/2 to +Math.PI/2.Ā The function calculates inverted tangent function value.

// ONLINE-RUNNER:browser;

function calculateAngle(a, b) {
	return Math.atan(a / b);
}

/*
  |\
  | \ h - for this function this information is not necessary
a |  \
  |__*\ <- angle
    b
*/

var a, b;

// a an b build isosceles right triangle
a = 3; 
b = a;
console.log( calculateAngle(a, b) ); // 0.7853981633974483 <- ~45 degrees

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

// a and b build very high (+Inf) and slim (~0) triangle
a = +Infinity; 
b = 0; 
console.log( calculateAngle(a, b) ); // 1.5707963267948966 <- ~90 degrees

1. Documentation

SyntaxMath.atan(number)
Parameters

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

Result

numberĀ value in radians in the range -Math.PI/2Ā to +Math.PI/2Ā (primitive value).

If value can not be calculatedĀ NaNĀ is returned.

Description

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


2. Working with degrees

// ONLINE-RUNNER:browser;

function calculateAngle(a, b) {
	var angle = Math.atan(a / b);

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

/*
  |\
  | \ h - for this function this information is not necessary
a |  \
  |__*\ <- angle
    b
*/

var a, b;

// a an b build isosceles right triangle
a = 3; 
b = a;
console.log( calculateAngle(a, b) ); // ~45 degrees

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

// a and b build very high (+Inf) and slim (~0) triangle
a = +Infinity; 
b = 0; 
console.log( calculateAngle(a, b) ); // ~90 degrees

3. Canvas plot example

// ONLINE-RUNNER:browser;

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

    // arctangent chart range
    var x1 = -10.0;
    var x2 = +10.0;
    var y1 = -Math.PI / 2;
    var y2 = +Math.PI / 2;

    var dx = 0.1;

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

    function calculatePoint(x) {
      var y = Math.atan(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

0 contributions

Checkout latest Findings & News:

Checkout latest questions:

Checkout latest wiki articles:

Hey šŸ‘‹
Would you like to know what we do?
  • Dirask is IT community, where we share coding knowledge and help each other to solve coding problems.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.
Read more