#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
using namespace std;
bool collinear(double x1, double y1, double x2, double y2, double x3, double y3){
return (y1 - y2) * (x2 - x3) == (y2 - y3) * (x1 - x2);
}
double distance(double xi, double yi, double xj, double yj){
return (sqrt(pow(xj - xi, 2) + pow(yj - yi, 2)));
}
bool isRight(double lengthP1P2, double lengthP2P3, double lengthP1P3){
if(lengthP1P2 > lengthP2P3 && lengthP1P2 > lengthP1P3){
return pow(lengthP1P2, 2) == pow(lengthP2P3, 2) + pow(lengthP1P3, 2);
}
else if(lengthP2P3 > lengthP1P2 && lengthP2P3 > lengthP1P3){
return pow(lengthP2P3, 2) == pow(lengthP1P2, 2) + pow(lengthP1P3, 2);
}
else if(lengthP1P3 > lengthP1P2 && lengthP1P3 > lengthP2P3){
return pow(lengthP1P3, 2) == pow(lengthP2P3, 2) + pow(lengthP1P2, 2);
}
}
bool isEquilateral(double lengthP1P2, double lengthP2P3, double lengthP1P3){
return lengthP1P2 == lengthP2P3 && lengthP1P2 == lengthP1P3;
}
bool isIsosceles(double lengthP1P2, double lengthP2P3, double lengthP1P3){
return lengthP1P2 == lengthP2P3 || lengthP1P2 == lengthP1P3 || lengthP2P3 == lengthP1P3;
}
void calcTriangle(double lengthP1P2, double lengthP2P3, double lengthP1P3, double& perimeter, double& area){
perimeter = lengthP1P2 + lengthP2P3 + lengthP1P3;
double p = perimeter / 2;
area = sqrt(p * (p - lengthP1P2) * (p - lengthP2P3) * (p - lengthP1P3));
}
int main(){
cout << setprecision(3) << fixed;
string fileName;
cout << "Enter the name of the data file -> ";
cin >> fileName;
ifstream in;
in.open(fileName.c_str());
double x1, y1, x2, y2, x3, y3;
double l12, l23, l13;
double p, a;
if(in.is_open()){
cout << "Triangle Program Report" << endl;
cout << "=========================================================================" << endl;
while(in >> x1){
in >> y1 >> x2 >> y2 >> x3 >> y3;
cout << "P1 = (" << x1 << ", " << y1 << "), P2 = (" << x2 << ", " << y2 << "),P3 = (" << x3 << ", " << y3 << ")" << endl;
if(collinear(x1, y1, x2, y2, x3, y3)){
cout << "are collinear and cannot be vertices of a triangle." << endl;
}
else{
l12 = distance(x1, y1, x2, y2);
l23 = distance(x2, y2, x3, y3);
l13 = distance(x1, y1, x3, y3);
cout << "||P1P2|| = " << l12 << " ||P2P3|| = " << l23 << " ||P1P3|| = " << l13 << endl;
calcTriangle(l12, l23, l13, p, a);
cout << "Perimeter = \t" << p << endl;
cout << "area = \t\t" << a << endl;
cout << "Classification:" << endl;
cout << "Right:\t\t";
if(isRight(l12, l23, l13)){
cout << "Y" << endl;
}
else{
cout << "N" << endl;
}
cout << "Isosceles:\t";
if(isIsosceles(l12, l23, l13)){
cout << "Y" << endl;
}
else{
cout << "N" << endl;
}
cout << "Scalene:\t";
if(!(isEquilateral(l12, l23, l13) || isIsosceles(l12, l23, l13))){
cout << "Y" << endl;
}
else{
cout << "N" << endl;
}
cout << "Equilateral:\t";
if(isEquilateral(l12, l23, l13)){
cout << "Y" << endl;
}
else{
cout << "N" << endl;
}
}
cout << "-------------------------------------------------------------------------" << endl;
}
cout << "*** END OF REPORT ***" << endl;
cout << "===============================================" << endl;
}
else{
cout << "Unable to open " << fileName << " for input." << endl;
}
return 0;
}