tetrahedron (公式)
我是直接搬运了某大佬的代码,毕竟我不清楚如何计算这个东西。
其中四点共面的求法就是体积为0,然后圆心和半径就公式了。
#include<cstdio> #include<iostream> #include<cmath> using namespace std; #define eps 1e-8 struct node { double x,y,z; }a,b,c,d,ans; ///面积公式 double get(node a,node b,node c) { double x1=b.x-a.x,y1=b.y-a.y,z1=b.z-a.z; double x2=c.x-a.x,y2=c.y-a.y,z2=c.z-a.z; double d1=y1*z2-y2*z1; double d2=x1*z2-x2*z1; double d3=x1*y2-x2*y1; return sqrt(d1*d1+d2*d2+d3*d3)*0.5; } ///体积 double Get(node a,node b,node c,node d) { double x1=b.x-a.x,y1=b.y-a.y,z1=b.z-a.z; double x2=c.x-a.x,y2=c.y-a.y,z2=c.z-a.z; double x3=d.x-a.x,y3=d.y-a.y,z3=d.z-a.z; double ans=x1*(y2*z3-y3*z2)-y1*(x2*z3-x3*z2)+z1*(x2*y3-x3*y2); return abs(ans)/6.0; } int main() { while(~scanf("%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf",&a.x,&a.y,&a.z,&b.x,&b.y,&b.z,&c.x,&c.y,&c.z,&d.x,&d.y,&d.z)) { double s1,s2,s3,s4,V; V=Get(a,b,c,d); s1=get(b,c,d); s2=get(a,c,d); s3=get(a,b,d); s4=get(a,b,c); double t=s1+s2+s3+s4; if(V<eps) { printf("O O O O\n"); continue; } ans.x=(s1*a.x+s2*b.x+s3*c.x+s4*d.x)/t; ans.y=(s1*a.y+s2*b.y+s3*c.y+s4*d.y)/t; ans.z=(s1*a.z+s2*b.z+s3*c.z+s4*d.z)/t; double r=V*3.0/t; printf("%.4lf %.4lf %.4lf %.4lf\n",ans.x,ans.y,ans.z,r); } }
more crazy more get!