How to use the Law of Sines In Javascript
Similarly, in this approach, we will calculate angle through the math.asin() of trigonometry through the length of all sides of the triangle, and we will find length through their vertices using the distance between two vertices formula of mathematics.
Syntax:
let xDiff = point1[0] - point2[0];
let yDiff = point1[1] - point2[1];
return xDiff * xDiff + yDiff * yDiff;
Javascript
// JavaScript program to find all three angles // of a triangle // Returns square of distance b/w two points function lengthSquare(X, Y) { let xDiff = X[0] - Y[0]; let yDiff = X[1] - Y[1]; return ( xDiff * xDiff + yDiff * yDiff ); } const calculateAngle = ( vertices1, vertices2, vertices3 ) => { // Square of lengths be a2, b2, c2 let a2 = lengthSquare( vertices2, vertices3 ); let b2 = lengthSquare( vertices1, vertices3 ); let c2 = lengthSquare( vertices1, vertices2 ); // Length of sides be a, b, c let a = Math.sqrt(a2); let b = Math.sqrt(b2); let c = Math.sqrt(c2); // From Cosine law let alpha = Math.asin( (b2 + c2 - a2) / (2 * b * c) ); let beta = Math.asin( (a2 + c2 - b2) / (2 * a * c) ); let gamma = Math.asin( (a2 + b2 - c2) / (2 * a * b) ); // Converting to degree beta = (beta * 180) / Math.PI; gamma = (gamma * 180) / Math.PI; alpha = 180 - gamma - beta; // Printing all the angles console.log( "alpha : " , alpha); console.log( "beta : " , beta); console.log( "gamma : " , gamma); }; // Driver code let vertices1 = [0, 0]; let vertices2 = [0, 5]; let vertices3 = [5, 0]; calculateAngle( vertices1, vertices2, vertices3 ); |
alpha : 90 beta : 44.99999999999999 gamma : 44.99999999999999
JavaScript Program to Find All Angles of a Triangle
In this article, we are going to implement a program through which we can calculate each angle of a triangle by its vertices. We will input the vertices of the triangle and return the angle to the main program.
Examples:
Input : Vertex A = (0, 5),
Vertex B = (0, 0),
Vertex C = (5, 0)
Output : 90, 45, 45
All possible approaches to find the angle.
Table of Content
- Using the Law of Cosines
- Using the Law of Sines