/
NikolayIvkin
/
TheAlgorithms
Обзор
Документация
Войти
/
NikolayIvkin
/
TheAlgorithms
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
src/main/java/com/thealgorithms/maths/HeronsFormula.java
35 строк
1 KB
Appari Satya Barghav
Herons : Changed the signature of the function (#4686)
24 окт 2023, 00:09
Не верифицирован
24 окт 2023, 00:09
9dae389
Код
Авторство
О чём код?
package com.thealgorithms.maths; /** * Wikipedia for HeronsFormula => https://en.wikipedia.org/wiki/Heron%27s_formula * Find the area of a triangle using only side lengths */ public final class HeronsFormula { /* * A function to get the Area of a Triangle using Heron's Formula * @param s1,s2,s3 => the three sides of the Triangle * @return area using the formula (√(s(s – s1)(s – s2)(s – s3))) * here s is called semi-perimeter and it is the half of the perimeter (i.e; s = (s1+s2+s3)/2) * @author satyabarghav */ private HeronsFormula() { } private static boolean areAllSidesPositive(final double a, final double b, final double c) { return a > 0 && b > 0 && c > 0; } private static boolean canFormTriangle(final double a, final double b, final double c) { return a + b > c && b + c > a && c + a > b; } public static double herons(final double a, final double b, final double c) { if (!areAllSidesPositive(a, b, c) || !canFormTriangle(a, b, c)) { throw new IllegalArgumentException("Triangle can't be formed with the given side lengths"); } final double s = (a + b + c) / 2.0; return Math.sqrt((s) * (s - a) * (s - b) * (s - c)); } }