tetrahedron
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 720 Accepted Submission(s): 291
Problem Description
Given four points ABCD, if ABCD is a tetrahedron, calculate the inscribed sphere of ABCD.
Input
Multiple test cases (test cases
≤100
).
Each test cases contains a line of 12 integers [−1e6,1e6] indicate the coordinates of four vertices of ABCD.
Input ends by EOF.
Each test cases contains a line of 12 integers [−1e6,1e6] indicate the coordinates of four vertices of ABCD.
Input ends by EOF.
Output
Print the coordinate of the center of the sphere and the radius, rounded to 4 decimal places.
If there is no such sphere, output "O O O O".
If there is no such sphere, output "O O O O".
Sample Input
0 0 0 2 0 0 0 0 2 0 2 0 0 0 0 2 0 0 3 0 0 4 0 0
Sample Output
0.4226 0.4226 0.4226 0.4226 O O O O
Author
HIT
Source
Recommend
题意:给出四个点,要你求四面体内切球球心坐标及半径大小。无解输出"O O O O"。
解:首先如果四点共面,必然无解。(我的判断方法是:ABC三点组成的平面法向量n和向量AD如果点乘为0,那么四点共面)
否则,必然有解(四面体必有内切球,可以用分角面证明)
内切球球心(x,y,z)的求法是
令 S= S△ABC+S△ACD+S△ABD+S△BCD;
x= (S△ABC*D.x+S△ACD*B.x+S△ABD*C.x+S△BCD*A.x)/S;
y= (S△ABC*D.y+S△ACD*B.y+S△ABD*C.y+S△BCD*A.y)/S;
z= (S△ABC*D.z+S△ACD*B.z+S△ABD*C.z+S△BCD*A.z)/S;
半径大小R可以根据 方程 1.0/4*S△ABC*H= 1.0/4* S*R (依据四面体体积相等) 解得
细节上,求三角形面积用了海伦公式,求ABC面上的高H用了H= | 向量AB*法向量n/| 法向量n | |
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define for0(a, n) for (int (a) = 0; (a) < (n); (a)++)
#define for1(a, n) for (int (a) = 1; (a) <= (n); (a)++)
#define sqr(x) ((x)*(x))
const double eps=1e-7;
typedef long long ll;
typedef pair<int, int> pii;
const int INF =0x3f3f3f3f;
struct Point{
double x,y,z;
Point(){}
Point(double x,double y,double z):x(x),y(y),z(z){}
}A,B,C,D,O;
typedef Point Vector;
double R;
Vector operator-(const Point &A,const Point &B)
{
return Vector(A.x-B.x,A.y-B.y,A.z-B.z);
}
Vector operator-(const Point &A)
{
return Vector(-A.x,-A.y,-A.z);
}
double Dot(Vector A,Vector B)
{
return (A.x*B.x+A.y*B.y+A.z*B.z);
}
Vector Cross(Vector a,Vector b)
{
return Vector( (a.y*b.z-a.z*b.y),-(a.x*b.z-b.x*a.z),(a.x*b.y-a.y*b.x) );
}
double Length(Vector a)
{
return sqrt( Dot(a,a));
}
double Helen(Point A,Point B,Point C)//海伦公式
{
Vector AB=B-A;double c= Length(AB);
Vector AC=C-A;double b= Length(AC);
Vector BC=C-B;double a= Length(BC);
double p=(a+b+c)/2;
return sqrt( p*(p-a)*(p-b)*(p-c) );
}
bool solve(Point A,Point B,Point C,Point D)
{
Vector AB=B-A;
Vector BC=C-B;
Vector n= Cross(AB,BC); //法向量
double ret=Dot(D-A,n);
if ( fabs(ret)<eps ) return false;//四点共面
double S=Helen(A,B,C );
O.x+= S*D.x;
O.y+= S*D.y;
O.z+= S*D.z;
return true;
}
void getRandXYZ()
{
Vector AB=B-A;
Vector BC=C-B;
Vector n= Cross(AB,BC);
Vector AD=D-A;
if(Dot(AD,n)<0) n=-n;
double h= Dot(AD,n)/Length(n);
double V4= h*Helen(A,B,C);
double s=0;
s+=Helen(A,B,C);
s+=Helen(A,B,D);
s+=Helen(A,C,D);
s+=Helen(B,C,D);
R=V4/s;//半径求得
O.x/=s;//球心坐标求得
O.y/=s;
O.z/=s;
}
int main()
{
while(~scanf("%lf%lf%lf",&A.x,&A.y,&A.z))
{
scanf("%lf%lf%lf%lf%lf%lf%lf%lf%lf",&B.x,&B.y,&B.z,&C.x,&C.y,&C.z,&D.x,&D.y,&D.z );
memset(&O,0,sizeof O);
if(!solve(A,B,C,D) )
{
puts("O O O O");
continue;
}
solve(A,B,D,C);
solve(A,C,D,B);
solve(B,C,D,A);
getRandXYZ();
printf("%.4f %.4f %.4f %.4f\n",O.x,O.y,O.z,R);
}
return 0;
}