Visual C++ - Find Area and Perimeter of a Triangle Given 3 points
Here is the Visual C++ source code to find Area or Perimeter of a Triangle given 3 points
Heron's formula for area calculation is given below:
perimeter = A + B + C
float s = perimeter / 2;
area = sqrt( s * (s-A) * (s-B) * (s-C))
Where,
A = Distance between point A and B
B = Distance between point B and C
C = Distance between point C and A
Source Code
#include <iostream>
#include <math.h>
int Calculate_Area_Perimeter_Triangle(float ax, float ay, float bx, float by, float cx, float cy, float *perimeter, float *area)
{
float A = sqrt((double)(bx-ax) * (bx-ax) + (by-ay) * (by-ay));
float B = sqrt((double)(bx-cx) * (bx-cx) + (by-cy) * (by-cy));
float C = sqrt((double)(ax-cx) * (ax-cx) + (ay-cy) * (ay-cy));
// Heron's formula for area calculation
// area = sqrt( s * (s-a) * (s-b) * (s-c))
float s = (A + B + C) / 2;
*perimeter = A + B + C;
*area = sqrt( s * (s-A) * (s-B) * (s-C));
// area = 1/2 * base * height
// if side A is base, then height
float height = (*area * 2) / A;
return 1;
}
void main()
{
float m1, c1, m2, c2;
float ax, ay, bx, by, cx, cy;
float perimeter, area;
float angleA, angleB, angleC;
int nCountIntersections = 0;
std::cout << " Program to find the Area and Perimeter of a triangle:\n";
std::cout << "Enter Triangle Point A - X: ";
std::cin >> ax;
std::cout << "Enter Triangle Point A - Y: ";
std::cin >> ay;
std::cout << "Enter Triangle Point B - X: ";
std::cin >> bx;
std::cout << "Enter Triangle Point B - Y: ";
std::cin >> by;
std::cout << "Enter Triangle Point C - X: ";
std::cin >> cx;
std::cout << "Enter Triangle Point C - Y: ";
std::cin >> cy;
Calculate_Area_Perimeter_Triangle(ax, ay, bx, by, cx, cy, &perimeter, &area);
std::cout << "\nPerimeter: " << perimeter;
std::cout << "\nArea: " << area;
}
Output
Program to find the given type of the triangle:
Enter Triangle Point A - X: 10
Enter Triangle Point A - Y: 10
Enter Triangle Point B - X: 20
Enter Triangle Point B - Y: 10
Enter Triangle Point C - X: 15
Enter Triangle Point C - Y: 15
Perimeter: 24.1421
Area: 25.0000
Press any key to continue . . .
|