1403: 三角形面积
Time Limit: 1 Sec Memory Limit: 128 MB Special JudgeSubmit: 176 Solved: 62
[ Submit][ Status][ Web Board]
Description
给出三维空间上的三个点,计算以这三个点为顶点的三角形的面积。
Input
输入的第一行包含一个整数T (T > 0),表示一共有T组测试数据。
每组数据占三行,每行各描述了一个点的坐标。点的坐标会以x y z的形式给出,表示这个点的坐标为(x, y, z),其中x, y, z均为[-100, 100]范围内的浮点数。
Output
对于每组测试数据,输出三角形的面积(和标准答案的误差不超过10-6即可)。
Sample Input
2
0 1 0
1 0 0
0 0 0
0 0.0 1.00
1.0 0.00 0
0.00 1.0 0
Sample Output
0.50
0.8660254
三角形面积公式S=0.5*A*B*sinC.利用余弦定理求出角度吧。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <queue>
#include <map>
#include <stack>
#include <list>
#include <vector>
#include <ctime>
#define LL __int64
#define eps 1e-8
using namespace std;
struct point
{
double x,y,z;
point(double x=0,double y=0,double z=0):x(x),y(y),z(z){}
}a,b,c;
point operator - (point a,point b)
{
return point(a.x-b.x,a.y-b.y,a.z-b.z);
}
double dis(point a,point b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)+(a.z-b.z)*(a.z-b.z));
}
int main()
{
int n;
scanf("%d",&n);
while (n--)
{
scanf("%lf%lf%lf",&a.x,&a.y,&a.z);
scanf("%lf%lf%lf",&b.x,&b.y,&b.z);
scanf("%lf%lf%lf",&c.x,&c.y,&c.z);
double A=dis(a,b);
double B=dis(b,c);
double C=dis(a,c);
double D=2*A*B;
double ac=(A*A+B*B-C*C)/D;
double sc=sqrt(1-ac*ac);
double ans=0.5*A*B*sc;
printf("%.6lf\n",ans);
}
return 0;
}