Geometric Formulas in Java
A. Heron's Formula for Triangle Area in Java:
For a triangle with side lengths $a$, $b$, and $c$, the semi-perimeter is $s = rac{a + b + c}{2}$, and the area is given by Heron's formula: $ ext{Area} = \sqrt{s(s - a)(s - b)(s - c)}$.
double s = (a + b + c) / 2.0;
double area = Math.sqrt(s * (s - a) * (s - b) * (s - c));
B. Distance Between Two Cartesian Coordinates:
The Euclidean distance between $(x_1, y_1)$ and $(x_2, y_2)$ is $d = \sqrt{(x_2 - x_1)^2 + (y_2 - y_1)^2}$:
double distance = Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
// Or using Math.hypot directly:
double dist = Math.hypot(x2 - x1, y2 - y1);