7:计算三角形面积
描述
平面上有一个三角形,它的三个顶点坐标分别为(x1, y1), (x2, y2), (x3, y3),那么请问这个三角形的面积是多少。
输入
输入仅一行,包括6个单精度浮点数,分别对应x1, y1, x2, y2, x3, y3。
输出
输出也是一行,输出三角形的面积,精确到小数点后两位。
使用海伦公式计算面积
include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
float x1,x2,x3,y1,y2,y3,a,b,c,p,s;
cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3;
a = sqrt(pow((x1-x2),2)+pow((y1-y2),2));
b = sqrt(pow((x2-x3),2)+pow((y2-y3),2));
c = sqrt(pow((x3-x1),2)+pow((y3-y1),2));
p = (a+b+c)/2;
s = sqrt(p*(p-a)*(p-b)*(p-c));
cout << fixed << setprecision(2) << s << endl; return 0;
}