问题:
已知一个三角形的三顶点坐标(皆为整点),求该三角形面积。
在小学,我们曾经学过三角形的面积公式 S = a h 2 S=\frac{ah}{2} S=2ah其中a是底长,h是高长。但很显然,这条公式在这里没有什么用处。所以我们需要一些其他的公式。
公式一 海伦公式 S = p ( p − a ) ( p − b ) ( p − c ) S=\sqrt {p(p-a)(p-b)(p-c)} S=p(p−a)(p−b)(p−c)其中p是半周长。a,b,c都是可以用两点之间的距离公式求出。代码如下。
#include<cstdio>
#include<cmath>
using namespace std;
int main()
{
int x1,y1,x2,y2,x3,y3;//linux不要这么写,下同
scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3);
double a=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
double b=sqrt((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3));
double c=sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2));
double p=(a+b+c)/2.0;
printf("%lf",sqrt(p*(p-a)*(p-b)*(p-c)));
return 0;
}
公式二 秦九韶公式 S = a 2 c 2 − ( a 2 − b 2 + c 2 2 ) 2 4 S=\sqrt{\cfrac{a^2c^2-(\cfrac{a^2-b^2+c^2}{2})^2}{4}} S=4a2c2−(2