C Program to Compute the Area of a Triangle Using Determinants

«
»
This is a C program to find the area of triangle using determinants.
Formula for the area of a triangle using determinants
x1 y1 1
Area=±1/2 x2 y2 1
x3 y3 1
The plus/minus in this case is meant to take whichever sign is needed so the answer is positive (non-negative). Do not say the area is both positive and negative.

Here is source code of the C Program to Compute the Area of a Triangle 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. int main(int argc, char **argv) {
  27.  
  28.     printf("Enter the points of the triangle:\n");
  29.     int x1, x2, x3, y1, y2, y3;
  30.     scanf("%d", &x1);
  31.     scanf("%d", &y1);
  32.  
  33.     scanf("%d", &x2);
  34.     scanf("%d", &y2);
  35.  
  36.     scanf("%d", &x3);
  37.     scanf("%d", &y3);
  38.  
  39.     double mat[3][3];
  40.     mat[0][0] = x1;
  41.     mat[0][1] = y1;
  42.     mat[0][2] = 1;
  43.     mat[1][0] = x2;
  44.     mat[1][1] = y2;
  45.     mat[1][2] = 1;
  46.     mat[2][0] = x3;
  47.     mat[2][1] = y3;
  48.     mat[2][2] = 1;
  49.  
  50.     printf("\nMatrix formed by the points: \n");
  51.     for (i = 0; i < 3; i++) {
  52.         for (j = 0; j < 3; j++) {
  53.             printf("%lf ", mat[i][j]);
  54.         }
  55.         printf("\n");
  56.     }
  57.  
  58.     float determinant = det(3, mat) * 0.5;
  59.     if (determinant < 0)
  60.         printf(
  61.                 "The area of triangle formed by (%d, %d), (%d, %d), (%d, %d) = %lf ",
  62.                 x1, y1, x2, y2, x3, y3, (determinant * -1));
  63.     else
  64.         printf(
  65.                 "The area of triangle formed by (%d, %d), (%d, %d), (%d, %d) = %lf ",
  66.                 x1, y1, x2, y2, x3, y3, (determinant));
  67.     return 0;
  68. }

Output:

advertisement
$ gcc TraingleArea.c
$ ./a.out
 
Enter the points of the triangle:
3 4
6 4
3 9
 
Matrix formed by the points: 
3 4 1 
6 4 1 
3 9 1 
The Area of the triangle formed by (3,4), (6,4), (3,9) = 7.5

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement
advertisement

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

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!
advertisement
advertisement
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Youtube | Instagram | Facebook | Twitter