3:计算三角形面积
总时间限制: 1000ms 内存限制: 65536kB
描述
平面上有一个三角形,它的三个顶点坐标分别为(x1, y1), (x2, y2), (x3, y3),那么请问这个三角形的面积是多少。
输入
输入仅一行,包括6个单精度浮点数,分别对应x1, y1, x2, y2, x3, y3。
输出
输出也是一行,输出三角形的面积,精确到小数点后两位。
样例输入
0 0 4 0 0 3
样例输出
6.00
提示
海伦公式
假设在平面内,有一个三角形,边长分别为a、b、c,三角形的面积S可由以下公式求得:
而公式里的p为半周长(周长的一半):
代码
#include "pch.h"
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
float input[6] = { 0 },temp;
for (int i = 0; i < 6; i++)
{
cin >> temp;
input[i] = temp;
}
float p = 0, a = 0,b = 0,c = 0,area = 0;
a = sqrt(powf((input[2] - input[0]), 2.0) + powf((input[3] - input[1]), 2.0));
b = sqrt(powf((input[4] - input[0]), 2.0) + powf((input[5] - input[1]), 2.0));
c = sqrt(powf((input[4] - input[2]), 2.0) + powf((input[5] - input[3]), 2.0));
p = (a + b + c) / 2.0;
area = sqrt(p*(p - a)*(p - b)*(p - c));
printf("%.2f", area);
return 0;
}