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

Output:

$ gcc TetrahedronVolume.c
$ ./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

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

Here’s the list of Best Books in C 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.