C++ Program to Find the Volume of a Tetrahedron Using Determinants

This is a C++ Program to find the volume of tetrahedron.
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 colums 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 source code of the C++ Program to Compute the Volume of a Tetrahedron Using Determinants. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<iostream>
  4. #include<math.h>
  5.  
  6. using namespace std;
  7.  
  8. double det(int n, double mat[3][3])
  9. {
  10.     double submat[3][3];
  11.     float d;
  12.     for (int c = 0; c < n; c++)
  13.     {
  14.         int subi = 0; //submatrix's i value
  15.         for (int i = 1; i < n; i++)
  16.         {
  17.             int subj = 0;
  18.             for (int j = 0; j < n; j++)
  19.             {
  20.                 if (j == c)
  21.                     continue;
  22.                 submat[subi][subj] = mat[i][j];
  23.                 subj++;
  24.             }
  25.             subi++;
  26.  
  27.         }
  28.         d = d + (pow(-1, c) * mat[0][c] * det(n - 1, submat));
  29.     }
  30.     return d;
  31. }
  32.  
  33. int main(int argc, char **argv)
  34. {
  35.  
  36.     cout << "Enter the points of the triangle:\n";
  37.     int x1, x2, x3, x4, y1, y2, y3, y4, z1, z2, z3, z4;
  38.     cin >> x1;
  39.     cin >> x2;
  40.     cin >> x3;
  41.     cin >> x4;
  42.     cin >> y1;
  43.     cin >> y2;
  44.     cin >> y3;
  45.     cin >> y4;
  46.     cin >> z1;
  47.     cin >> z2;
  48.     cin >> z3;
  49.     cin >> z4;
  50.     double mat[4][4];
  51.     mat[0][0] = x1;
  52.     mat[0][1] = x2;
  53.     mat[0][2] = x3;
  54.     mat[0][3] = x4;
  55.     mat[1][0] = y1;
  56.     mat[1][1] = y2;
  57.     mat[1][2] = y3;
  58.     mat[1][3] = y4;
  59.     mat[2][0] = z1;
  60.     mat[2][1] = z2;
  61.     mat[2][2] = z3;
  62.     mat[2][3] = z4;
  63.     mat[3][0] = 1;
  64.     mat[3][1] = 1;
  65.     mat[3][2] = 1;
  66.     mat[3][3] = 1;
  67.  
  68.     cout << "\nMatrix formed by the points: \n";
  69.     for (int i = 0; i < 4; i++)
  70.     {
  71.         for (int j = 0; j < 4; j++)
  72.         {
  73.             cout << mat[i][j] << " ";
  74.         }
  75.         cout << endl;
  76.     }
  77.     double matrix[3][3];
  78.  
  79.     matrix[0][0] = x1 - x4;
  80.     matrix[0][1] = x2 - x4;
  81.     matrix[0][2] = x3 - x4;
  82.     matrix[1][0] = y1 - y4;
  83.     matrix[1][1] = y2 - y4;
  84.     matrix[1][2] = y3 - y4;
  85.     matrix[2][0] = z1 - z4;
  86.     matrix[2][1] = z2 - z4;
  87.     matrix[2][2] = z3 - z4;
  88.     for (int i = 0; i < 3; i++)
  89.     {
  90.         for (int j = 0; j < 3; j++)
  91.         {
  92.             cout << matrix[i][j] << " ";
  93.         }
  94.         cout << endl;
  95.     }
  96.     float determinant = det(3, matrix) / 6;
  97.     if (determinant < 0)
  98.         cout << "The Area of the tetrahedron formed by (" << x1 << "," << y1
  99.                 << "," << z1 << "), (" << x2 << "," << y2 << "," << z2
  100.                 << "), (" << x3 << "," << y3 << "," << z3 << "), (" << x4 << ","
  101.                 << y4 << "," << z4 << ") = " << (determinant * -1);
  102.     else
  103.         cout << "The Area of the tetrahedron formed by (" << x1 << "," << y1
  104.                 << "," << z1 << "), (" << x2 << "," << y2 << "," << z2
  105.                 << "), (" << x3 << "," << y3 << "," << z3 << "), (" << x4 << ","
  106.                 << y4 << "," << z4 << ") = " << determinant;
  107.     return 0;
  108. }

Output:

$ g++ TetrahedronVolume.cpp
$ a.out
 
Enter the points of the triangle:
0 9 6 0
4 2 1 1
3 4 7 5
 
Matrix formed by the points: 
0 9 6 0 
4 2 1 1 
3 4 7 5 
1 1 1 1 
 
0 9 6 
3 1 0 
-2 -1 2 
The Area of the tetrahedron formed by (0,4,3), (9,2,4), (6,1,7), (0,1,5) = 10.0
------------------
(program exited with code: 0)
Press return to continue

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

advertisement
advertisement

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

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.