计算几何初步——共面判断(FZU1393)
先判断第三点是否与前2点共线,是的话,这4点肯定共面。
反之则以这3点确定一个平面方程,以此判断第四点是否在同一个面上。
View Code
1 #include<iostream>
2 #include<iostream>
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"<<endl;
53 } else {
54 point direction1(data[1].x - data[0].x,data[1].y - data[0].y,data[1].z - data[0].z);
55 point direction2(data[2].x - data[0].x,data[2].y - data[0].y,data[2].z - data[0].z);
56 point direction = cross(direction1,direction2);
57 if (is_interface(direction,data[0],data[3])) {
58 cout<<"Yes"<<endl;
59 } else {
60 cout<<"No"<<endl;
61 }
62 }
63 }
64 return 0;
65 }