JavaScript Program to Find All Angles of a Triangle
Last Updated :
26 Oct, 2023
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
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
) => {
let a2 = lengthSquare(
vertices2,
vertices3
);
let b2 = lengthSquare(
vertices1,
vertices3
);
let c2 = lengthSquare(
vertices1,
vertices2
);
let a = Math.sqrt(a2);
let b = Math.sqrt(b2);
let c = Math.sqrt(c2);
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)
);
alpha = (alpha * 180) / Math.PI;
beta = (beta * 180) / Math.PI;
gamma = (gamma * 180) / Math.PI;
console.log( "alpha : " , alpha);
console.log( "beta : " , beta);
console.log( "gamma : " , gamma);
};
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
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
) => {
let a2 = lengthSquare(
vertices2,
vertices3
);
let b2 = lengthSquare(
vertices1,
vertices3
);
let c2 = lengthSquare(
vertices1,
vertices2
);
let a = Math.sqrt(a2);
let b = Math.sqrt(b2);
let c = Math.sqrt(c2);
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)
);
beta = (beta * 180) / Math.PI;
gamma = (gamma * 180) / Math.PI;
alpha = 180 - gamma - beta;
console.log( "alpha : " , alpha);
console.log( "beta : " , beta);
console.log( "gamma : " , gamma);
};
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
Share your thoughts in the comments
Please Login to comment...