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

In this tutorial, we going to know about finding the volume of a tetrahedron using determinants in Java. One-sixth of the absolute value of the matrix's computed determinant equals the volume of the tetrahedron. The volume of the tetrahedron, created by three vectors originating from one of the vertices, is one-sixth that of the corresponding parallelepiped. The signed volume of the parallelepiped is equal to the determinant of the matrix formed by the three vectors, which is formed by taking the cross product of two of them and combining them with the third. thinking about the vertices of a tetrahedron. Its goal is to determine which determinants to use in order to obtain that tetrahedron's volume.
Considering the tetrahedron's vertices. The objective is to use determinants to find that tetrahedron's volume.

Examples:

Example 1:

Input: a1=2, a2=4, a3=1, a4=3, b1=5, b2=6, b3=2, b4=4, c1=3, c2=0, c3=2, c4=5

Output: Volume of the Tetrahedron Using Determinants: 11.0

Explanation: We calculate the determinant of the matrix, in this case the determinant is 11.0.

Approach: Using determinant method

ALGORITHM:

Step 1: 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 in the given four vertices of the tetrahedron (x1, y1, z1), (x2, y2, z2), (x3, y3, z3), and (x4, y4, z4).

`a1  a2  a3  a4b1  b2  b3  b4c1  c2  c3  c41   1    1    1`

Step 2: Use the simplification formula to reduce into a (3 × 3) matrix for a 4 × 4 matrix which has a row of 1’s at the bottom.

`a1-a4   a2-a4   a3-a4b1-b4   b2-b4   b3-b4c1-c4   c2-c4   c3-c4`

Step 3: One-sixth of the absolute value of the matrix's determined determinant is equal to the volume of the tetrahedron.

Filename: VolumeOfATetrahedon.java

`import java.io.*;public class VolumeOfATetrahedon{    public static double determinant(double x[][], int y) {        double Determinant = 0;        // If there is just one element in the matrix        if (y == 1)       {            Determinant = x[0][0];        }        // If there are four elements in the matrix        // Find out the determinant        else if (y == 2)       {            Determinant = x[0][0] * x[1][1] - x[1][0] * x[0][1];        }        else {            for (int k1 = 0; k1 < y; k1++)              {                double[][] z = new double[y - 1][];                for (int j = 0; j < (y - 1); j++)               {                    z[j] = new double[y - 1];                }                for (int j = 1; j < y; j++)               {                    int k2 = 0;                    for (int k = 0; k < y; k++)                      {                        if (k == k1)                            continue;                        z[j - 1][k2] = x[j][k];                        k2++;                    }                }                Determinant += Math.pow(-1.0, 1.0 + k1 + 1.0) * x[0][k1] * determinant(z, y - 1);            }        }        return Determinant;    }    public static void main(String args[])    {        int a1 = 2, a2 = 4, a3 = 1, a4 = 3, b1 = 5, b2 = 6, b3 = 2, b4 = 4, c1 = 3, c2 = 0, c3 = 2, c4 = 5;        // Make a 4 * 4 matrix        double[][] x = new double[4][4];        // Make a matrix of that vertices        x[0][0] = a1;        x[0][1] = a2;        x[0][2] = a3;        x[0][3] = a4;        x[1][0] = b1;        x[1][1] = b2;        x[1][2] = b3;        x[1][3] = b4;        x[2][0] = c1;        x[2][1] = c2;        x[2][2] = c3;        x[2][3] = c4;        x[3][0] = 1;        x[3][1] = 1;        x[3][2] = 1;        x[3][3] = 1;        // Change the 4x4 matrix into 3x3        double[][] x1 = new double[3][3];        x1[0][0] = a1 - a4;        x1[0][1] = a2 - a4;        x1[0][2] = a3 - a4;        x1[1][0] = b1 - b4;        x1[1][1] = b2 - b4;        x1[1][2] = b3 - b4;        x1[2][0] = c1 - c4;        x1[2][1] = c2 - c4;        x1[2][2] = c3 - c4;        double Determinant = determinant(x1, 3) / 6;        if (Determinant < 0)            System.out.println("Volume of the Tetrahedron Using Determinants: " + (Determinant * -1));        else            System.out.println("Volume of the Tetrahedron Using Determinants: " + (Determinant * -1));    }}`

Output:

`Volume of the Tetrahedron Using Determinants: -4.166666666666667`

Time complexity: The time complexity of the program is O(N^3).

Space complexity: The space complexity of the program is O(N).