How to Find the Volume of a Tetrahedron Using Determinants in Java? - GeeksforGeeks

04 Nov, 2020
Given the vertices of a tetrahedron. The task is to determine the volume of that tetrahedron using determinants.
Approach:
1. Given the four vertices of the tetrahedron (x1, y1, z1), (x2, y2, z2), (x3, y3, z3), and (x4, y4, z4). Using these vertices create a (4 × 4) matrix in which the coordinate triplets form the columns of the matrix, with an extra row with each value as 1 appended at the bottom.
x1  x2  x3  x4
y1  y2  y3  y4
z1  z2  z3  z4
1   1   1    1
2. For a 4 × 4 matrix which has a row of 1’s at the bottom, we can use the given simplification formula to reduce into a (3 × 3) matrix.
x1-x4   x2-x4   x3-x4
y1-y4   y2-y4   y3-y4
z1-z4   z2-z4   z3-z4
3. Volume of the tetrahedron is equal to 1/6 times the absolute value of the above calculated determinant of the matrix. 
Input: x1=9, x2=3, x3=7, x4=9, y1=5, y2=0, y3=4, y4=6, z1=1, z2=0, z3=3, z4=0
Output: Volume of the Tetrahedron Using Determinants: 3.0

Input: x1=6, x2=8, x3=5, x4=9, y1=7, y2=1, y3=7, y4=1, z1=6, z2=9, z3=2, z4=6
Output: Volume of the Tetrahedron Using Determinants: 7.0

Java
// Java program to find the volume of a// tetrahedron using determinants  import java.io.*;  class VolumeOfADeterminant {    public static double determinant(double m[][], int n)    {        double dt = 0;          // if the matrix has only        // one element        if (n == 1) {            dt = m[0][0];        }          // if the matrix has 4 elements        // find determinant        else if (n == 2) {            dt = m[0][0] * m[1][1] - m[1][0] * m[0][1];        }          else {            dt = 0;            for (int j1 = 0; j1 < n; j1++) {                  double[][] w = new double[n - 1][];                for (int k = 0; k < (n - 1); k++) {                    w[k] = new double[n - 1];                }                for (int i = 1; i < n; i++) {                    int j2 = 0;                    for (int j = 0; j < n; j++) {                        if (j == j1)                            continue;                        w[i - 1][j2] = m[i][j];                        j2++;                    }                }                dt += Math.pow(-1.0, 1.0 + j1 + 1.0)                      * m[0][j1] * determinant(w, n - 1);            }        }        return dt;    }      public static void main(String args[])    {        // Input the vertices        int x1 = 5, x2 = 8, x3 = 1, x4 = 9, y1 = 5, y2 = 0,            y3 = 7, y4 = 8, z1 = 8, z2 = 3, z3 = 4, z4 = 1;          // create a 4 * 4 matrix        double[][] m = new double[4][4];          // Create a matrix of that vertices        m[0][0] = x1;        m[0][1] = x2;        m[0][2] = x3;        m[0][3] = x4;        m[1][0] = y1;        m[1][1] = y2;        m[1][2] = y3;        m[1][3] = y4;        m[2][0] = z1;        m[2][1] = z2;        m[2][2] = z3;        m[2][3] = z4;        m[3][0] = 1;        m[3][1] = 1;        m[3][2] = 1;        m[3][3] = 1;          // Converting the 4x4 matrix into 3x3        double[][] m1 = new double[3][3];        m1[0][0] = x1 - x4;        m1[0][1] = x2 - x4;        m1[0][2] = x3 - x4;        m1[1][0] = y1 - y4;        m1[1][1] = y2 - y4;        m1[1][2] = y3 - y4;        m1[2][0] = z1 - z4;        m1[2][1] = z2 - z4;        m1[2][2] = z3 - z4;          // find (determinant/6)        double deter = determinant(m1, 3) / 6;          // if determinant is negative        if (deter < 0)            System.out.println(                "Volume of the Tetrahedron Using Determinants: "                + (deter * -1));        else            System.out.println(                "Volume of the Tetrahedron Using Determinants: "                + (deter * -1));    }}
Volume of the Tetrahedron Using Determinants: 52.333333333333336

Java
Java Programs
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Functional Interfaces in Java
Stream In Java
Constructors in Java
Different ways of Reading a text file in Java
Exceptions in Java
Convert a String to Character array in Java
Java Programming Examples
Convert Double to Integer in Java
Implementing a Linked List in Java using Class
How to Iterate HashMap in Java?