forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeronsFormula.java
More file actions
78 lines (73 loc) · 2.84 KB
/
HeronsFormula.java
File metadata and controls
78 lines (73 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package com.thealgorithms.maths;
/**
* Heron's Formula implementation for calculating the area of a triangle given
* its three side lengths.
* <p>
* Heron's Formula states that the area of a triangle whose sides have lengths
* a, b, and c is:
* Area = √(s(s - a)(s - b)(s - c))
* where s is the semi-perimeter of the triangle: s = (a + b + c) / 2
* </p>
*
* @see <a href="https://en.wikipedia.org/wiki/Heron%27s_formula">Heron's
* Formula - Wikipedia</a>
* @author satyabarghav
*/
public final class HeronsFormula {
/**
* Private constructor to prevent instantiation of this utility class.
*/
private HeronsFormula() {
}
/**
* Checks if all three side lengths are positive.
*
* @param a the length of the first side
* @param b the length of the second side
* @param c the length of the third side
* @return true if all sides are positive, false otherwise
*/
private static boolean areAllSidesPositive(final double a, final double b, final double c) {
return a > 0 && b > 0 && c > 0;
}
/**
* Checks if the given side lengths satisfy the triangle inequality theorem.
* <p>
* The triangle inequality theorem states that the sum of any two sides
* of a triangle must be greater than the third side.
* </p>
*
* @param a the length of the first side
* @param b the length of the second side
* @param c the length of the third side
* @return true if the sides can form a valid triangle, false otherwise
*/
private static boolean canFormTriangle(final double a, final double b, final double c) {
return a + b > c && b + c > a && c + a > b;
}
/**
* Calculates the area of a triangle using Heron's Formula.
* <p>
* Given three side lengths a, b, and c, the area is computed as:
* Area = √(s(s - a)(s - b)(s - c))
* where s is the semi-perimeter: s = (a + b + c) / 2
* </p>
*
* @param a the length of the first side (must be positive)
* @param b the length of the second side (must be positive)
* @param c the length of the third side (must be positive)
* @return the area of the triangle
* @throws IllegalArgumentException if any side length is non-positive or if the
* sides cannot form a valid triangle
*/
public static double herons(final double a, final double b, final double c) {
if (!areAllSidesPositive(a, b, c)) {
throw new IllegalArgumentException("All side lengths must be positive");
}
if (!canFormTriangle(a, b, c)) {
throw new IllegalArgumentException("Triangle cannot be formed with the given side lengths (violates triangle inequality)");
}
final double s = (a + b + c) / 2.0;
return Math.sqrt((s) * (s - a) * (s - b) * (s - c));
}
}