题目链接
题目大意
输入一个四面体求其内心,若不存在内心则输出“O O O O”
解题思路
其实这道题思路很简单,只要类推一下三角形内心公式就可以了。
至于如何判断无解,计算一下体积若V<=0则无解
Code
#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-8;
struct point{
double x, y, z;
point (double xx = 0, double yy = 0, double zz = 0){
x = xx;
y = yy;
z = zz;
}
friend point operator - (const point &a, const point &b){
return point(a.x - b.x, a.y - b.y, a.z - b.z);
}
} p[4];
double cross(point p1, point p2){
return sqrt(pow(p1.y * p2.z - p1.z * p2.y, 2) + pow(-(p1.x * p2.z - p1.z * p2.x), 2) + pow(p1.x * p2.y - p1.y * p2.x, 2));
}
double vol(point p1, point p2, point p3){
return fabs((p2.y * p3.z - p2.z * p3.y) * p1.x - (p2.x * p3.z - p2.z * p3.x) * p1.y + (p2.x * p3.y - p2.y * p3.x) * p1.z);
}
int main(){
while (~scanf("%lf%lf%lf", &p[0].x, &p[0].y, &p[0].z)){
for (int i = 1; i < 4; i++) scanf("%lf%lf%lf", &p[i].x, &p[i].y, &p[i].z);
double V = vol(p[1] - p[0], p[2] - p[0], p[3] - p[0]);
if (V <= eps){
printf("O O O O
");
continue;
}
V = V / 6;
double s1 = 0.5 * cross(p[1] - p[0], p[2] - p[0]);
double s2 = 0.5 * cross(p[1] - p[0], p[3] - p[0]);
double s3 = 0.5 * cross(p[2] - p[0], p[3] - p[0]);
double s4 = 0.5 * cross(p[1] - p[2], p[3] - p[2]);
double S = s1 + s2 + s3 + s4;
double r = V * 3 / S;
double x = (p[0].x * s4 + p[1].x * s3 + p[2].x * s2 + p[3].x * s1) / S;
double y = (p[0].y * s4 + p[1].y * s3 + p[2].y * s2 + p[3].y * s1) / S;
double z = (p[0].z * s4 + p[1].z * s3 + p[2].z * s2 + p[3].z * s1) / S;
printf("%.4f %.4f %.4f %.4f
", x, y, z, r);
}
return 0;
}