1. 程式人生 > >用類 定位三角形求三角形三邊及面積

用類 定位三角形求三角形三邊及面積

//新人第一次發哈哈//
//版本號 1.0    //
//作者暴走竹竿   //
#include<iostream>
#include<cmath>
using namespace std;
class point {
public:
	point(int xx = 0, int yy = 0) {
		x = xx;
		y = yy;
	}
	int getx() { return x; }
	int gety() { return y; }
private:
	int x, y;
};
class Line {
public:
	Line(point xp1, point xp2, point xp3);
	Line(Line &l);
	double getlen() { return len; }
private:
	point p1, p2, p3;
	double len;
};
Line::Line(point xp1, point xp2, point xp3) :p1(xp1), p2(xp2), p3(xp3)
{
	double x = static_cast<double>(p1.getx() - p2.getx());
	double y = static_cast<double>(p1.gety() - p2.gety());
	len = sqrt(x*x + y*y);
}
int main() {
	double a = 0;
	double area = 0;
	point myp1(1, 1), myp2(4, 5), myp3(1, 5);
	Line line(myp3, myp2, myp1);
	Line line2(myp1, myp2, myp3);
	Line line3(myp1, myp3, myp2);
	cout << line.getlen() << endl;
	cout << line2.getlen() << endl;
	cout << line3.getlen() << endl;
	a = (line.getlen() + line2.getlen() + line3.getlen()) / 2;
	area= sqrt(a*(a - line.getlen())*(a-line2.getlen())*(a - line3.getlen()));
	cout << "面積為" << area << endl;
	return 0;
}