海伦公式
/*
海伦公式:给三条边abc就可以计算三角形面积S
p = (a+b+c)/2
S = sqrt( p*[p-a]*[p-b]*[p-c] )
别问为什么写的这么啰嗦,问就是降低理解难度
*/
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
typedef pair<double, double> P;
#define x first
#define y second
int main()
{
P pts[3];
for (int i = 0; i < 3; i++)
cin >> pts[i].x >> pts[i].y;
int k = 0;
double line[3]; // 记录三条边长度
for (int i = 0; i < 3; i++)
{
for (int j = i + 1; j < 3; j++) // 枚举三条边
{
double x1 = pts[i].x;
double y1 = pts[i].y;
double x2 = pts[j].x;
double y2 = pts[j].y;
double dx = x1 - x2;
double dy = y1 - y2;
line[k++] = sqrt(dx * dx + dy * dy); // 欧氏距离
}
}
double a = line[0];
double b = line[1];
double c = line[2];
double p = (a + b + c) / 2;
double res = sqrt(p * (p - a) * (p - b) * (p - c));
printf("%.03lf\n", res);
return 0;
}
向量的叉积
/*
向量的叉积:
S = 1/2(ab * bc) ab,bc指任意两边的向量,默认012-abc边
*/
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
typedef pair<double, double> P;
#define x first
#define y second
// 计算ab,bc的叉积
double cross(double x1, double y1, double x2, double y2)
{
double res = x1 * y2 - x2 * y1;
return res;
}
int main()
{
P pts[3];
for (int i = 0; i < 3; i++)
cin >> pts[i].x >> pts[i].y;
P vector[2]; // 存储向量ab,bc
vector[0].x = pts[0].x - pts[1].x;
vector[0].y = pts[0].y - pts[1].y;
vector[1].x = pts[1].x - pts[2].x;
vector[1].y = pts[1].y - pts[2].y;
double res = 0.5 * cross(vector[0].x, vector[0].y, vector[1].x, vector[1].y);
printf("%.03lf\n", res);
return 0;
}