先判断第三点是否与前2点共线,是的话,这4点肯定共面。
反之则以这3点确定一个平面方程,以此判断第四点是否在同一个面上。
View Code
1 #include2 #include 3 #define EPS 1e-8 4 using namespace std; 5 6 struct point { 7 double x, y, z; 8 point (){} 9 point (double _x, double _y, double _z):x(_x),y(_y),z(_z){} 10 }data[4]; 11 12 bool equal_0(point &p) 13 { 14 if (p.x+p.y+p.z > EPS || p.x+p.y+p.z < -EPS)return false; 15 return true; 16 } 17 18 point cross(point &p, point &q) 19 { 20 point temp(p.y*q.z-p.z*q.y,p.z*q.x-p.x*q.z,p.x*q.y-p.y*q.x); 21 return temp; 22 } 23 24 bool on_line(point &p1, point &p2, point &temp) 25 { 26 point direction1(p2.x-p1.x,p2.y-p1.y,p2.z-p1.z); 27 point direction2(temp.x-p1.x,temp.y-p1.y,temp.z-p1.z); 28 point direction3 = cross(direction1,direction2); 29 if (equal_0(direction3)) { 30 return true; 31 } 32 return false; 33 } 34 35 bool is_interface(point &k, point &a, point &p) 36 { 37 if (k.x*(p.x - a.x) + k.y*(p.y - a.y) + k.z*(p.z - a.z) <= EPS && 38 k.x*(p.x - a.x) + k.y*(p.y - a.y) + k.z*(p.z - a.z) >= -EPS) 39 return true; 40 return false; 41 } 42 43 int main() 44 { 45 int T; 46 scanf("%d",&T); 47 while (T--) { 48 for (int i(0); i<4; ++i) { 49 scanf("%lf%lf%lf",&data[i].x,&data[i].y,&data[i].z); 50 } 51 if (on_line(data[0],data[1],data[2])) { 52 cout<<"Yes"<