# 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;`
12. `        } else if (N == 2)`
13. `        {`
14. `            det = A * A - A * A;`
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[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;`
61. `        mat = x1;`
62. `        mat = x2;`
63. `        mat = x3;`
64. `        mat = x4;`
65. `        mat = y1;`
66. `        mat = y2;`
67. `        mat = y3;`
68. `        mat = y4;`
69. `        mat = z1;`
70. `        mat = z2;`
71. `        mat = z3;`
72. `        mat = z4;`
73. `        mat = 1;`
74. `        mat = 1;`
75. `        mat = 1;`
76. `        mat = 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;`
87. ` `
88. `        matrix = x1 - x4;`
89. `        matrix = x2 - x4;`
90. `        matrix = x3 - x4;`
91. `        matrix = y1 - y4;`
92. `        matrix = y2 - y4;`
93. `        matrix = y3 - y4;`
94. `        matrix = z1 - z4;`
95. `        matrix = z2 - z4;`
96. `        matrix = 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.

Note: Join free Sanfoundry classes at Telegram or Youtube 