Java Program to Find the Volume of a Tetrahedron using Determinants

This is a Java Program to compute volume of tetrahedron using determinants. Call the four vertices of the tetrahedron (a, b, c), (d, e, f), (g, h, i), and (p, q, r). Now create a 4-by-4 matrix in which the coordinate triples form the columns of the matrix, with a row of 1’s appended at the bottom:
a d g p
b e h q
c f i r
1 1 1 1
The volume of the tetrahedron is 1/6 times the absolute value of the matrix determinant. For any 4-by-4 matrix that has a row of 1’s along the bottom, you can compute the determinant with a simplification formula that reduces the problem to a 3-by-3 matrix
a-p d-p g-p
b-q e-q h-q
c-r f-r i-r

Here is the source code of the Java Program to Compute the Volume of a Tetrahedron Using Determinants. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. //This is a java program to find the volume of tetrahedron using a method of determinant
  2. import java.util.Random;
  3.  
  4. public class Volume_Tetrahedron_Determinants
  5. {
  6.     public static double determinant(double A[][], int N)
  7.     {
  8.         double det = 0;
  9.         if (N == 1)
  10.         {
  11.             det = A[0][0];
  12.         } else if (N == 2)
  13.         {
  14.             det = A[0][0] * A[1][1] - A[1][0] * A[0][1];
  15.         } else
  16.         {
  17.             det = 0;
  18.             for (int j1 = 0; j1 < N; j1++)
  19.             {
  20.                 double[][] m = new double[N - 1][];
  21.                 for (int k = 0; k < (N - 1); k++)
  22.                 {
  23.                     m[k] = new double[N - 1];
  24.                 }
  25.                 for (int i = 1; i < N; i++)
  26.                 {
  27.                     int j2 = 0;
  28.                     for (int j = 0; j < N; j++)
  29.                     {
  30.                         if (j == j1)
  31.                             continue;
  32.                         m[i - 1][j2] = A[i][j];
  33.                         j2++;
  34.                     }
  35.                 }
  36.                 det += Math.pow(-1.0, 1.0 + j1 + 1.0) * A[0][j1]
  37.                         * determinant(m, N - 1);
  38.             }
  39.         }
  40.         return det;
  41.     }
  42.  
  43.     public static void main(String args[])
  44.     {
  45.         Random random = new Random();
  46.         int x1, x2, x3, x4, y1, y2, y3, y4, z1, z2, z3, z4;
  47.         x1 = random.nextInt(10);
  48.         x2 = random.nextInt(10);
  49.         x3 = random.nextInt(10);
  50.         x4 = random.nextInt(10);
  51.         y1 = random.nextInt(10);
  52.         y2 = random.nextInt(10);
  53.         y3 = random.nextInt(10);
  54.         y4 = random.nextInt(10);
  55.         z1 = random.nextInt(10);
  56.         z2 = random.nextInt(10);
  57.         z3 = random.nextInt(10);
  58.         z4 = random.nextInt(10);
  59.  
  60.         double[][] mat = new double[4][4];
  61.         mat[0][0] = x1;
  62.         mat[0][1] = x2;
  63.         mat[0][2] = x3;
  64.         mat[0][3] = x4;
  65.         mat[1][0] = y1;
  66.         mat[1][1] = y2;
  67.         mat[1][2] = y3;
  68.         mat[1][3] = y4;
  69.         mat[2][0] = z1;
  70.         mat[2][1] = z2;
  71.         mat[2][2] = z3;
  72.         mat[2][3] = z4;
  73.         mat[3][0] = 1;
  74.         mat[3][1] = 1;
  75.         mat[3][2] = 1;
  76.         mat[3][3] = 1;
  77.  
  78.         System.out
  79.                 .println("The matrix formed by the coordinates of the tetrahedron is: ");
  80.         for (int i = 0; i < 4; i++)
  81.         {
  82.             for (int j = 0; j < 4; j++)
  83.                 System.out.print(mat[i][j] + " ");
  84.             System.out.println();
  85.         }
  86.         double[][] matrix = new double[3][3];
  87.  
  88.         matrix[0][0] = x1 - x4;
  89.         matrix[0][1] = x2 - x4;
  90.         matrix[0][2] = x3 - x4;
  91.         matrix[1][0] = y1 - y4;
  92.         matrix[1][1] = y2 - y4;
  93.         matrix[1][2] = y3 - y4;
  94.         matrix[2][0] = z1 - z4;
  95.         matrix[2][1] = z2 - z4;
  96.         matrix[2][2] = z3 - z4;
  97.         System.out.println("Matrix after simplification: ");
  98.         for (int i = 0; i < 3; i++)
  99.         {
  100.             for (int j = 0; j < 3; j++)
  101.                 System.out.print(matrix[i][j] + " ");
  102.             System.out.println();
  103.         }
  104.         double det = determinant(matrix, 3) / 6;
  105.         if (det < 0)
  106.             System.out.println("The Area of the tetrahedron formed by (" + x1
  107.                     + "," + y1 + "," + z1 + "),(" + x2 + "," + y2 + "," + z2
  108.                     + "),(" + x3 + "," + y3 + "," + z3 + "), = " + (det * -1));
  109.         else
  110.             System.out.println("The Area of the tetrahedron formed by (" + x1
  111.                     + "," + y1 + "," + z1 + "),(" + x2 + "," + y2 + "," + z2
  112.                     + "),(" + x3 + "," + y3 + "," + z3 + "), = " + (det * -1));
  113.     }
  114. }

Output:

$ javac Volume_Tetrahedron_Determinants.java
$ java Volume_Tetrahedron_Determinants
 
The matrix formed by the coordinates of the tetrahedron is: 
0.0 9.0 6.0 0.0 
4.0 2.0 1.0 1.0 
3.0 4.0 7.0 5.0 
1.0 1.0 1.0 1.0 
Matrix after simplification: 
0.0 9.0 6.0 
3.0 1.0 0.0 
-2.0 -1.0 2.0 
The Area of the tetrahedron formed by (0,4,3),(9,2,4),(6,1,7), = 10.0

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.