Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Calculating the area of a triangle using its three sides in JavaScript
In JavaScript, calculating the area of a triangle using its three sides can be accomplished through mathematical formulas. This is useful when you know the side lengths but not the height or base measurements.
Problem Statement
Create a JavaScript function that calculates the area of a triangle given the lengths of its three sides.
Sample Input:
Side A: 3 units Side B: 4 units Side C: 5 units
Sample Output:
Area: 6
Approach
We'll explore two mathematical methods to solve this problem:
- Using Heron's Formula
- Using Law of Cosines
Using Heron's Formula
Heron's formula calculates the area using the semi-perimeter (half the perimeter). The formula is: Area = ?[s(s-a)(s-b)(s-c)], where s is the semi-perimeter.
Steps:
- Calculate semi-perimeter: s = (a + b + c) / 2
- Apply Heron's formula
- Return the square root of the result
Example
function calculateAreaHeron(side1, side2, side3) {
// Calculate the semi-perimeter
const s = (side1 + side2 + side3) / 2;
// Calculate the area using Heron's formula
const area = Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));
return area;
}
// Example usage
const side1 = 3;
const side2 = 4;
const side3 = 5;
const area = calculateAreaHeron(side1, side2, side3);
console.log("Area using Heron's formula:", area);
Area using Heron's formula: 6
Using Law of Cosines
The Law of Cosines can be used to find angles, then apply the formula: Area = (1/2) × a × b × sin(C), where C is the angle between sides a and b.
Steps:
- Use Law of Cosines to find one angle
- Apply the area formula with sine
- Return the calculated area
Example
function calculateAreaCosine(side1, side2, side3) {
// Calculate angle C using the Law of Cosines
// cos(C) = (a² + b² - c²) / (2ab)
const cosC = (side1 * side1 + side2 * side2 - side3 * side3) / (2 * side1 * side2);
const angleC = Math.acos(cosC);
// Calculate area using: area = (1/2) * a * b * sin(C)
const area = (1 / 2) * side1 * side2 * Math.sin(angleC);
return area;
}
// Example usage
const sideA = 3;
const sideB = 4;
const sideC = 5;
const triangleArea = calculateAreaCosine(sideA, sideB, sideC);
console.log("Area using Law of Cosines:", triangleArea);
Area using Law of Cosines: 6
Comparison
| Method | Complexity | Best Use Case |
|---|---|---|
| Heron's Formula | Simple | When you only need the area |
| Law of Cosines | More complex | When you also need angle information |
Complete Example with Validation
function calculateTriangleArea(a, b, c) {
// Validate triangle inequality
if (a + b
Triangle (3,4,5): 6
Triangle (5,5,5): 21.65
Invalid (1,2,5): Invalid triangle: sides don't satisfy triangle inequality
Conclusion
Both Heron's formula and the Law of Cosines provide accurate methods for calculating triangle area from three sides. Heron's formula is simpler and more direct, making it the preferred choice for most applications.
