# 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) {
double t1 = (M * M)-(M * M);
double t2 = (M * M)-(M * M);
double t3 = (M * M)-(M * M);
return (M*t1) + (-M*t2) + (M*t3);
}
main() {
double M;
cout << "Enter Point p1 (x, y):";
cin >> M >> M;
M = 1;
cout << "Enter Point p2 (x, y):";
cin >> M >> M;
M = 1;
cout << "Enter Point p3 (x, y):";
cin >> M >> M;
M = 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