Area-Of-Triangle-In-C++
INTRODUCTION
There are several ways to compute the area of a triangle, which is a fundamental geometry notion. Heron's formula is one way that is frequently utilized. Heron's formula enables us to determine the area of a triangle without knowing its height or any of its angles if we just know the lengths of its three sides (let's call them sides a, b, and c).
Method-1 using Heron’s formula
- The semi-perimeter of a triangle, represented by the letter "s," serves as the foundation for Heron's formula.
The semi-perimeter is equal to (a + b + c) / 2, or half of the sum of 3 sides.
- You can create a function in C++ that accepts the lengths of the triangle's three sides as input and uses Heron's formula to calculate the area. It is crucial to confirm that a valid triangle is formed by the input sides. Checking whether each side is bigger than zero and determining whether the total of any two sides is more than the third side are both required. Invalid input sides should result in the program handling the problem appropriately.
- Heron's formula can be used in different applications requiring geometry & graphics programming since it accurately calculates a triangle's area considering only the lengths of its sides.
The formula to determine the area using the semi-perimeter is:
Area = sqrt(s * (s-a) * (s-b) * (s-c))
The square root function is denoted by the symbol "sqrt" in this formula.
EXAMPLE
#include <iostream>
#include <cmath>
int main() {
int a, b, c;
std::cout << "Enter the three sides of the triangle: ";
std::cin >> a >> b >> c;
float x = (float)(a + b + c) / 2;
float area = sqrt(x * (x - a) * (x - b) * (x - c));
std::cout << "Area = " << area << std::endl;
return 0;
}
OUTPUT
![AREA OF TRIANGLE IN C++](https://static.tutorialandexample.com/cpp/area-of-triangle-in-cpp1.png)
Method-2 using Base and Height
Using the dimensions of the triangle's base and height, this approach determines the area of the triangle. Any side of a triangle to which the height is perpendicular is considered the base of the triangle. The height of a triangle is equal to the total length of the perpendicular line connecting its base to its opposite vertex.
Area is calculated using the formula Area = (base * height) / 2
EXAMPLE
#include <iostream>
double calculateArea(double base, double height) {
return (base * height) / 2;
}
int main() {
double base, height;
std::cout << "Enter the base and height of the triangle: ";
std::cin >> base >> height;
double area = calculateArea(base, height);
std::cout << "The area of the triangle is: " << area << std::endl;
return 0;
}
OUTPUT
![AREA OF TRIANGLE IN C++](https://static.tutorialandexample.com/cpp/area-of-triangle-in-cpp2.png)
The height and base are input parameters for the calculateArea function in the provided sample of code. It computes the area using the formula and then returns the result. The main function executes the calculateArea function, shows the calculated area, then asks the user to specify the triangle's base and height.
Method-3 using Co-ordinates
Considering the coordinates of a triangle's three vertices, this approach determines the area of the triangle. It uses the Gauss's area formula, which uses the shoelace method to calculate the area from the parameters.
Area = 0.5 * (x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2))
where (x1, y1), (x2, y2), and (x3, y3) are the dimensions of the triangle's three vertices.
EXAMPLE
#include <iostream>
#include <cmath>
long A(long m1, long n1, long m2, long n2, long m3, long n3) {
return 0.5 * std::abs((m1 * (n2 - n3) + m2 * (n3 - n1) + m3 * (n1 - n2)));
}
int main() {
long m1, n1, m2, n2, m3, n3;
std::cout << "Enter the coordinates:\n";
std::cout << "Vertex 1 (m1, n1): ";
std::cin >> m1 >> n1;
std::cout << "Vertex 2 (m2, n2): ";
std::cin >> m2 >> n2;
std::cout << "Vertex 3 (m3, n3): ";
std::cin >> m3 >> n3;
long area = A(m1, n1, m2, n2, m3, n3);
std::cout << "Area is : " << area << std::endl;
return 0;
}
OUTPUT
![AREA OF TRIANGLE IN C++](https://static.tutorialandexample.com/cpp/area-of-triangle-in-cpp3.png)
The Shoelace formula is used to determine the area, and the result is returned. The A function is called by the main function, which also shows the calculated area after asking the user to provide the coordinates of the three vertices.
CONCLUSION
In conclusion, there are a number of benefits and things to think about while computing a triangle's area in C++. C++ delivers accurate and trustworthy results by precisely performing the area calculation formula. Because of its adaptability, C++ may be integrated into a wide range of systems and applications. Wrapping the calculation in a single function promotes reusability while improving the modularity and maintainability of the code.