
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to Compute the Area of a Triangle Using Determinants
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
- Related Articles
- Compute log-determinants for a stack of matrices in Python
- Java program to find the area of a triangle
- Java Program to calculate the area of a triangle using Heron's Formula
- How to Calculate the Area of a Triangle using Python?
- Golang Program to Create a Class and Compute the Area and Perimeter of a Circle
- Python Program to Create a Class and Compute the Area and the Perimeter of the Circle
- Golang Program to Find the Area of a Triangle Given All Three Sides
- C++ program to find the Area of the Largest Triangle inscribed in a Hexagon?
- Program to calculate area and perimeter of equilateral triangle
- Program to calculate area of Circumcircle of an Equilateral Triangle
- Other Determinants of Helping
- How to compute the area and perimeter of an image contour using OpenCV Python?
- C program to print area of triangle, square, circle, rectangle and polygon using switch case.
- Swift Program to Calculate Area and Perimeter of Equilateral Triangle
- Area of Circumcircle of a Right Angled Triangle in C Program?

Advertisements