# C++ Program to Compute the Area of a Triangle Using Determinants

C++Server Side ProgrammingProgramming

In this section we will see how to find the area of a triangle in 2D coordinate space using matrix determinants. In this case we are considering the space is 2D. So we are putting each points in the matrix. Putting x values at the first column, y into the second and taking 1 as the third column. Then find the determinant of them. The area of the triangle will be half of the determinant value. If the determinant is negative, then simply take the absolute value of it.

$$Area\:=\:absolute\:of\begin{pmatrix}\frac{1}{2} \begin{vmatrix} x_1\:\:y_1\:\:1 \\ x_2\:\:y_2\:\:1 \\ x_3\:\:y_3\:\:1 \end{vmatrix} \end{pmatrix}$$

Here we are assuming that this is 3x3 matrix, so the determinant function cannot find determinant of matrix which is not 3x3.

## Example Code

#include<iostream>
#include<cmath>
using namespace std;
double det(double M[3][3]) {
double t1 = (M[1][1] * M[2][2])-(M[1][2] * M[2][1]);
double t2 = (M[1][0] * M[2][2])-(M[1][2] * M[2][0]);
double t3 = (M[1][0] * M[2][1])-(M[1][1] * M[2][0]);
return (M[0][0]*t1) + (-M[0][1]*t2) + (M[0][2]*t3);
}
main() {
double M[3][3];
cout << "Enter Point p1 (x, y):";
cin >> M[0][0] >> M[0][1];
M[0][2] = 1;
cout << "Enter Point p2 (x, y):";
cin >> M[1][0] >> M[1][1];
M[1][2] = 1;
cout << "Enter Point p3 (x, y):";
cin >> M[2][0] >> M[2][1];
M[2][2] = 1;
int determinant = det(M);
cout << "The area is: " << fabs(determinant) * 0.5;
}

## Output

Enter Point p1 (x, y):3 4
Enter Point p2 (x, y):6 4
Enter Point p3 (x, y):3 9
The area is: 7.5
Updated on 04-Jul-2020 14:33:05