Robotics
Here’s a basic JavaScript code snippet demonstrating the three primary trigonometric functions (sin, cos, tan) along with some additional functions like asin, acos, and atan:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Trigonometric Functions</title>
</head>
<body>
<h1>Trigonometric Functions in JavaScript</h1>
<p>Check the console for the results.</p>
<script>
// Angle in degrees
let angleInDegrees = 30;
// Convert angle to radians
let angleInRadians = angleInDegrees * (Math.PI / 180);
// Trigonometric calculations
let sineValue = Math.sin(angleInRadians);
let cosineValue = Math.cos(angleInRadians);
let tangentValue = Math.tan(angleInRadians);
// Inverse functions
let arcsineValue = Math.asin(sineValue); // Result in radians
let arccosineValue = Math.acos(cosineValue); // Result in radians
let arctangentValue = Math.atan(tangentValue); // Result in radians
// Displaying the results in the console
console.log(`Sine(${angleInDegrees}°) = ${sineValue}`);
console.log(`Cosine(${angleInDegrees}°) = ${cosineValue}`);
console.log(`Tangent(${angleInDegrees}°) = ${tangentValue}`);
console.log(`Arcsine(${sineValue}) = ${arcsineValue * (180 / Math.PI)}°`);
console.log(`Arccosine(${cosineValue}) = ${arccosineValue * (180 / Math.PI)}°`);
console.log(`Arctangent(${tangentValue}) = ${arctangentValue * (180 / Math.PI)}°`);
</script>
</body>
</html>
Explanation:
1. Math.sin(), Math.cos(), and Math.tan() calculate sine, cosine, and tangent of an angle in radians.
2. Math.asin(), Math.acos(), and Math.atan() return the arcsine, arccosine, and arctangent, respectively.
3. The angle is converted from degrees to radians using the formula:
radians = degrees * (π / 180).
4. The output is displayed in the browser's console.
Comments
Post a Comment