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.
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.
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int i, j, c;
double det(int n, double mat[3][3]) {
double submat[3][3];
float d;
for (c = 0; c < n; c++) {
int subi = 0; //submatrix's i value
for (i = 1; i < n; i++) {
int subj = 0;
for (j = 0; j < n; j++) {
if (j == c)
continue;
submat[subi][subj] = mat[i][j];
subj++;
}
subi++;
}
d = d + (pow(-1, c) * mat[0][c] * det(n - 1, submat));
}
return d;
}
int main(int argc, char **argv) {
printf("Enter the points of the triangle:\n");
int x1, x2, x3, y1, y2, y3;
scanf("%d", &x1);
scanf("%d", &y1);
scanf("%d", &x2);
scanf("%d", &y2);
scanf("%d", &x3);
scanf("%d", &y3);
double mat[3][3];
mat[0][0] = x1;
mat[0][1] = y1;
mat[0][2] = 1;
mat[1][0] = x2;
mat[1][1] = y2;
mat[1][2] = 1;
mat[2][0] = x3;
mat[2][1] = y3;
mat[2][2] = 1;
printf("\nMatrix formed by the points: \n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf("%lf ", mat[i][j]);
}
printf("\n");
}
float determinant = det(3, mat) * 0.5;
if (determinant < 0)
printf(
"The area of triangle formed by (%d, %d), (%d, %d), (%d, %d) = %lf ",
x1, y1, x2, y2, x3, y3, (determinant * -1));
else
printf(
"The area of triangle formed by (%d, %d), (%d, %d), (%d, %d) = %lf ",
x1, y1, x2, y2, x3, y3, (determinant));
return 0;
}
Output:
$ 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 Books in C Programming, Data Structures and Algorithms.
Related Posts:
- Apply for Computer Science Internship
- Apply for C Internship
- Check C Books
- Check Computer Science Books
- Practice BCA MCQs