I am trying to calculate the area of a triangle with vertices
{{0,1000000000},{1,0},{0,-1000000000}}
It is easy to see that the area of this triangle should be 1,000,000,000, but when I try to calculate the area in Java using either Heron's formula or the Shoelace formula, I get 0 for the area.
I am pretty sure this is due to a rounding error while using double
s but I am not sure how to proceed. Any pointers?
Program:
private static double areaShoelace(int[][] v) {
return 0.5 * Math.abs(v[0][0]*v[1][1] + v[1][0]*v[2][1] + v[2][0]*v[0][1] +
v[1][0]*v[0][1] + v[2][0]*v[1][1] + v[0][0]*v[2][1]);
}
private static double areaHeron(double a, double b, double c) {
double p = (a + b + c) / 2.0d;
return Math.sqrt(p * (p - a) * (p - b) * (p - c));
}
private static double length(int[] a, int [] b) {
return Math.hypot(a[0] - b[0], a[1] - b[1]);
}
public static void main(String[] args) {
int[][] tri = new int[][]{{0,1000000000},{1,0},{0,-1000000000}};
System.out.println(areaShoelace(tri));
System.out.println(areaHeron(length(tri[0], tri[1]), length(tri[1],tri[2]), length(tri[0],tri[2])));
}
Output:
0.0
0.0