Area (Base & Height)
Area (Three Sides – Heron's Formula)
Perimeter (Three Sides)
Side c (Side a, Side b, Angle C)
Side b (Side a, Side c, Angle B) – Ambiguous Case
Side c (Side a, Side b, Angle A) – Ambiguous Case
Angle C (Side a, Side b, Side c)
Angle B (Side a, Side b, Side c)
Results will appear here.
Understanding Triangles and Their Calculations
Triangles are fundamental geometric shapes, consisting of three sides and three angles. They are ubiquitous in mathematics, physics, engineering, and design. This calculator helps you determine various properties of a triangle based on the information you provide.
Common Triangle Calculations:
Area (Base & Height): The most common formula for the area of any triangle is:
Area = 0.5 * base * height
This is applicable when you know the length of one side (the base) and the perpendicular distance from that base to the opposite vertex (the height).
Area (Heron's Formula – Three Sides): If you know the lengths of all three sides (a, b, c), you can use Heron's formula to find the area. First, calculate the semi-perimeter (s):
s = (a + b + c) / 2
Then, the area is:
Area = sqrt(s * (s - a) * (s - b) * (s - c))
Perimeter (Three Sides): The perimeter is simply the sum of the lengths of all three sides:
Perimeter = a + b + c
Finding a Side (Law of Cosines): When you know two sides and the included angle (SAS), or all three sides (SSS), you can find the remaining side.
For SAS (given a, b, C):c² = a² + b² - 2ab * cos(C) For SSS (given a, b, c): This requires finding an angle first using the inverse Law of Cosines.
Finding an Angle (Law of Cosines): The Law of Cosines can also be rearranged to find any angle if all three sides are known:
cos(A) = (b² + c² - a²) / (2bc)cos(B) = (a² + c² - b²) / (2ac)cos(C) = (a² + b² - c²) / (2ab)
The angle is then found using the inverse cosine function (arccos).
Ambiguous Case (SSA – Side-Side-Angle): When given two sides and a non-included angle, there might be zero, one, or two possible triangles. This calculator can help determine the possible lengths of the third side or angles. The Law of Sines is often used here: a / sin(A) = b / sin(B) = c / sin(C).
Use Cases:
Geometry & Trigonometry Education: A valuable tool for students learning about triangle properties.
Construction & Engineering: Calculating areas for land plots, roof pitches, or structural components.
Navigation & Surveying: Determining distances and positions using triangulation principles.
Computer Graphics & Game Development: Essential for rendering 2D and 3D shapes and calculating object interactions.
Art & Design: Understanding geometric proportions and creating balanced designs.
function degToRad(degrees) {
return degrees * (Math.PI / 180);
}
function radToDeg(radians) {
return radians * (180 / Math.PI);
}
function updateInputs() {
var shapeType = document.getElementById("shape_type").value;
var dynamicInputsDiv = document.getElementById("dynamic-inputs");
dynamicInputsDiv.innerHTML = ""; // Clear previous inputs
if (shapeType === "area_base_height") {
dynamicInputsDiv.innerHTML += `
`;
} else if (shapeType === "side_sas") { // Side c from a, b, Angle C
dynamicInputsDiv.innerHTML += `
`;
} else if (shapeType === "side_ssa") { // Side b from a, c, Angle B
dynamicInputsDiv.innerHTML += `
`;
} else if (shapeType === "side_ssa2") { // Side c from a, b, Angle A
dynamicInputsDiv.innerHTML += `
`;
} else if (shapeType === "angle_sas") { // Angle C from a, b, c
dynamicInputsDiv.innerHTML += `
`;
} else if (shapeType === "angle_ssa") { // Angle B from a, b, c
dynamicInputsDiv.innerHTML += `
`;
}
}
function calculateTriangle() {
var shapeType = document.getElementById("shape_type").value;
var resultDiv = document.getElementById("result");
var resultHTML = "";
try {
if (shapeType === "area_base_height") {
var base = parseFloat(document.getElementById("base").value);
var height = parseFloat(document.getElementById("height").value);
if (isNaN(base) || isNaN(height) || base <= 0 || height <= 0) {
resultHTML = "Please enter valid positive numbers for base and height.";
} else {
var area = 0.5 * base * height;
resultHTML = "The Area is: " + area.toFixed(4) + " square units.";
}
} else if (shapeType === "area_sides") {
var a = parseFloat(document.getElementById("side_a_heron").value);
var b = parseFloat(document.getElementById("side_b_heron").value);
var c = parseFloat(document.getElementById("side_c_heron").value);
if (isNaN(a) || isNaN(b) || isNaN(c) || a <= 0 || b <= 0 || c <= 0) {
resultHTML = "Please enter valid positive numbers for all three sides.";
} else if ((a + b <= c) || (a + c <= b) || (b + c <= a)) {
resultHTML = "The given side lengths do not form a valid triangle (Triangle Inequality Theorem violation).";
} else {
var s = (a + b + c) / 2;
var area = Math.sqrt(s * (s – a) * (s – b) * (s – c));
resultHTML = "The Area is: " + area.toFixed(4) + " square units.";
}
} else if (shapeType === "perimeter_sides") {
var a = parseFloat(document.getElementById("side_a_perimeter").value);
var b = parseFloat(document.getElementById("side_b_perimeter").value);
var c = parseFloat(document.getElementById("side_c_perimeter").value);
if (isNaN(a) || isNaN(b) || isNaN(c) || a <= 0 || b <= 0 || c <= 0) {
resultHTML = "Please enter valid positive numbers for all three sides.";
} else if ((a + b <= c) || (a + c <= b) || (b + c <= a)) {
resultHTML = "The given side lengths do not form a valid triangle (Triangle Inequality Theorem violation).";
} else {
var perimeter = a + b + c;
resultHTML = "The Perimeter is: " + perimeter.toFixed(4) + " units.";
}
} else if (shapeType === "side_sas") { // Side c from a, b, Angle C
var a = parseFloat(document.getElementById("side_a_sas").value);
var b = parseFloat(document.getElementById("side_b_sas").value);
var angleC_deg = parseFloat(document.getElementById("angle_c_sas").value);
if (isNaN(a) || isNaN(b) || isNaN(angleC_deg) || a <= 0 || b <= 0 || angleC_deg = 180) {
resultHTML = "Please enter valid positive numbers for sides and an angle between 0 and 180 degrees.";
} else {
var angleC_rad = degToRad(angleC_deg);
var c_squared = (a * a) + (b * b) – (2 * a * b * Math.cos(angleC_rad));
var c = Math.sqrt(c_squared);
resultHTML = "Side c is: " + c.toFixed(4) + " units.";
}
} else if (shapeType === "side_ssa") { // Side b from a, c, Angle B
var a = parseFloat(document.getElementById("side_a_ssa").value);
var c = parseFloat(document.getElementById("side_c_ssa").value);
var angleB_deg = parseFloat(document.getElementById("angle_b_ssa").value);
if (isNaN(a) || isNaN(c) || isNaN(angleB_deg) || a <= 0 || c <= 0 || angleB_deg = 180) {
resultHTML = "Please enter valid positive numbers for sides and an angle between 0 and 180 degrees.";
} else {
var angleB_rad = degToRad(angleB_deg);
// Using Law of Sines to find Angle B first if needed, but here Angle B is given.
// We can use Law of Cosines to find side b: b^2 = a^2 + c^2 – 2ac*cos(B)
var b_squared = (a * a) + (c * c) – (2 * a * c * Math.cos(angleB_rad));
if (b_squared < 0) {
resultHTML = "No triangle can be formed with these inputs (check side lengths and angle).";
} else {
var b = Math.sqrt(b_squared);
resultHTML = "Side b is: " + b.toFixed(4) + " units.";
}
}
} else if (shapeType === "side_ssa2") { // Side c from a, b, Angle A
var a = parseFloat(document.getElementById("side_a_ssa2").value);
var b = parseFloat(document.getElementById("side_b_ssa2").value);
var angleA_deg = parseFloat(document.getElementById("angle_a_ssa2").value);
if (isNaN(a) || isNaN(b) || isNaN(angleA_deg) || a <= 0 || b <= 0 || angleA_deg = 180) {
resultHTML = "Please enter valid positive numbers for sides and an angle between 0 and 180 degrees.";
} else {
var angleA_rad = degToRad(angleA_deg);
var sinA = Math.sin(angleA_rad);
var ratio = (a * sinA) / b; // This is sin(B) if B is the angle opposite side b.
if (ratio > 1) {
resultHTML = "No triangle can be formed. Side opposite the given angle is too short.";
} else if (ratio === 1) {
// One solution, forms a right triangle if Angle A is 90
var angleB_rad = Math.asin(ratio);
var angleB_deg = radToDeg(angleB_rad);
var angleC_deg = 180 – angleA_deg – angleB_deg;
var angleC_rad = degToRad(angleC_deg);
var c = (b * Math.sin(angleC_rad)) / sinA;
resultHTML = "One possible triangle. Side c is: " + c.toFixed(4) + " units.";
} else {
// Two possible solutions (ambiguous case)
var angleB1_rad = Math.asin(ratio);
var angleB1_deg = radToDeg(angleB1_rad);
var angleC1_deg = 180 – angleA_deg – angleB1_deg;
var angleC1_rad = degToRad(angleC1_deg);
var c1 = (b * Math.sin(angleC1_rad)) / sinA;
var angleB2_deg = 180 – angleB1_deg;
var angleC2_deg = 180 – angleA_deg – angleB2_deg;
var angleC2_rad = degToRad(angleC2_deg);
var c2 = (b * Math.sin(angleC2_rad)) / sinA;
resultHTML = "Two possible triangles:" +
"Triangle 1: Side c = " + c1.toFixed(4) + " units." +
"Triangle 2: Side c = " + c2.toFixed(4) + " units.";
}
}
} else if (shapeType === "angle_sas") { // Angle C from a, b, c
var a = parseFloat(document.getElementById("side_a_angle_sas").value);
var b = parseFloat(document.getElementById("side_b_angle_sas").value);
var c = parseFloat(document.getElementById("side_c_angle_sas").value);
if (isNaN(a) || isNaN(b) || isNaN(c) || a <= 0 || b <= 0 || c <= 0) {
resultHTML = "Please enter valid positive numbers for all three sides.";
} else if ((a + b <= c) || (a + c <= b) || (b + c 1 || cosC < -1) { // Should not happen if triangle is valid, but good check
resultHTML = "Calculation error: Invalid cosine value.";
} else {
var angleC_rad = Math.acos(cosC);
var angleC_deg = radToDeg(angleC_rad);
resultHTML = "Angle C is: " + angleC_deg.toFixed(4) + " degrees.";
}
}
} else if (shapeType === "angle_ssa") { // Angle B from a, b, c
var a = parseFloat(document.getElementById("side_a_angle_ssa").value);
var b = parseFloat(document.getElementById("side_b_angle_ssa").value);
var c = parseFloat(document.getElementById("side_c_angle_ssa").value);
if (isNaN(a) || isNaN(b) || isNaN(c) || a <= 0 || b <= 0 || c <= 0) {
resultHTML = "Please enter valid positive numbers for all three sides.";
} else if ((a + b <= c) || (a + c <= b) || (b + c 1 || cosB < -1) { // Should not happen if triangle is valid, but good check
resultHTML = "Calculation error: Invalid cosine value.";
} else {
var angleB_rad = Math.acos(cosB);
var angleB_deg = radToDeg(angleB_rad);
resultHTML = "Angle B is: " + angleB_deg.toFixed(4) + " degrees.";
}
}
}
resultDiv.innerHTML = resultHTML;
} catch (error) {
resultDiv.innerHTML = "An error occurred during calculation. Please check your inputs.";
console.error(error);
}
}
// Initialize inputs on page load
document.addEventListener("DOMContentLoaded", function() {
updateInputs();
});