题目
平面上有一个三角形,它的三个顶点坐标分别为(x1, y1), (x2, y2), (x3, y3),那么请问这个三角形的面积是多少,精确到小数点后两位。。
Input
输入仅一行,包括6个单精度浮点数,分别对应x1, y1, x2, y2, x3, y3。
Output
输出也是一行,输出三角形的面积,精确到小数点后两位。
Sample Input
0 0 4 0 0 3
Sample Output
6.00
思路分析:
海伦公式: S=√[p(p-a)(p-b)(p-c)] ,而公式里的p为半周长:p=(a+b+c)/2
边长计算:|AB| = √(x1 - x2)2+(y1-y2)2
代码实现:
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
int main()
{
double x1,x2,x3,y1,y2,y3,a,b,c,area,p;
cin>>x1>>y1>>x2>>y2>>x3>>y3;
a = sqrt((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1));//计算边长a
b = sqrt((x3 - x1)*(x3 - x1) + (y3 - y1)*(y3 - y1));//计算边长b
c = sqrt((x2 - x3)*(x2 - x3) + (y2 - y3)*(y2 - y3));//计算边长c
p = (a + b + c)/2.0;//计算半周长
area = sqrt(p*(p-a)*(p-b)*(p-c));//海伦公式求面积
cout<<setiosflags(ios::fixed)<<setprecision(2)<<area<<endl;
return 0;
}