#include<iostream>
#include<cmath>
using namespace std;
class Point { //坐标点类
public:
const double x, y;
Point(double x = 0.0, double y = 0.0) : x(x), y(y) {}
//**********found**********
double distanceTo(const Point& p )const { //到指定点的距离
return sqrt((x - p.x) * (x - p.x) + (y - p.y) * (y - p.y));
}
};
class Line { //线段类
public:
const Point p1, p2; //线段的两个端点
//**********found**********
Line(Point p1, Point p2) : p1(p1),p2(p2){}
double length()const { return p1.distanceTo(p2); } //线段的长度
};
class Triangle { //三角形类
public:
const Point p1, p2, p3; //三角形的三个顶点
//**********found**********
Triangle(Point(p1),Point(p2),Point(p3)) : p1(p1), p2(p2), p3(p3) {}
double length1()const { //边p1,p2的长度
return Line(p1, p2).length();
}
double length2()const { //边p2,p3的长度
return Line(p2, p3).length();
}
double length3()const { //边p3,p1的长度
return Line(p3, p1).length();
}
double area()const { //三角形面积
//**********found**********
double s = (length1()+length2() + length3())/2;
return sqrt(s * (s - length1()) * (s - length2()) * (s - length3()));
}
};
int main() {
Triangle r(Point(0.0, 8.0), Point(5.0, 0.0), Point(0.0, 0.0));
cout << "Side 1: " << r.length1() << endl;
cout << "Side 2: " << r.length2() << endl;
cout << "Side 3: " << r.length3() << endl;
cout << "area: " << r.area() << endl;
return 0;
}
C++-----计算三角形面积
于 2023-09-10 21:16:34 首次发布