Open In App

JavaScript Program to Find All Angles of a Triangle

Last Updated : 26 Oct, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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.

Using the Law of Cosines

In this approach, first we will find the length of each side using vertices, then we will calculate the alpha beta gamma angles of triangles using the cosine of trigonometry, and we will find the cosine through Math.acos() method.

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 given coordinate of all three 
// vertices
  
// 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.acos(
        (b2 + c2 - a2) / (2 * b * c)
    );
      
    let beta = Math.acos(
        (a2 + c2 - b2) / (2 * a * c)
    );
    let gamma = Math.acos(
        (a2 + b2 - c2) / (2 * a * b)
    );
  
    // Converting to degree
    alpha = (alpha * 180) / Math.PI;
    beta = (beta * 180) / Math.PI;
    gamma = (gamma * 180) / Math.PI;
  
    // 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
);


Output

alpha :  90
beta :  45.00000000000001
gamma :  45.00000000000001

Using the Law of Sines

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
);


Output

alpha :  90
beta :  44.99999999999999
gamma :  44.99999999999999


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads