51Nod 1265 四點共面
阿新 • • 發佈:2017-07-19
http too node text dot pac 如果 space printf
Output示例
1265 四點共面 基準時間限制:1 秒 空間限制:131072 KB 給出三維空間上的四個點(點與點的位置均不相同),判斷這4個點是否在同一個平面內(4點共線也算共面)。如果共面,輸出"Yes",否則輸出"No"。 Input
第1行:一個數T,表示輸入的測試數量(1 <= T <= 1000) 第2 - 4T + 1行:每行4行表示一組數據,每行3個數,x, y, z, 表示該點的位置坐標(-1000 <= x, y, z <= 1000)。Output
輸出共T行,如果共面輸出"Yes",否則輸出"No"。Input示例
1 1 2 0 2 3 0 4 0 0 0 0 0
Yes
題解:
確定空間中的四個點(三維)是否共面
1.
對於四個點, 以一個點為原點,對於其他三個點有A,B, C三個向量, 求出 A X B (cross product), 就是以A B構成的平面的一個法向量(如果 AB共線,則法向量為0), 在求其與C之間的點積, 如果為0, 則表示兩個向量為0。 所以四點共面。
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <map> #include<cmath> using namespace std; struct Node{ double x, y, z; }; int main(){ // freopen("in.txt", "r", stdin); int test_num; Node a[5]; double ans; scanf("%d", &test_num); while(test_num--){ for(int i=0; i<4; ++i){ scanf("%lf %lf %lf", &a[i].x, &a[i].y, &a[i].z); }// the cross product of (a,b) a[4].x = -(a[1].z - a[0].z)*(a[2].y - a[0].y)+ (a[1].y - a[0].y)*(a[2].z - a[0].z); a[4].y = (a[1].z - a[0].z)*(a[2].x-a[0].x) - (a[1].x - a[0].x)*(a[2].z - a[0].z); a[4].z = (a[1].x -a[0].x)*(a[2].y - a[0].y) - (a[1].y-a[0].y)*(a[2].x - a[0].x ); // the dot product of cp(a,b) and c ans = (a[3].x - a[0].x)*(a[4].x) + (a[3].y-a[0].y)*a[4].y + (a[3].z-a[0].z)*a[4].z; if( fabs(ans) <= 1e-9){ printf("Yes\n"); }else{ printf("No\n"); } } return 0; }
2.帶入平面方程驗證第四個點
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> using namespace std; struct node { int x, y, z; } a[10]; int main() { int T; cin>>T; while(T--) { for(int i=1; i<=4; i++) cin>>a[i].x>>a[i].y>>a[i].z; ///平面方程A*x+B*y+C*z+D=0; int A = ((a[2].y-a[1].y)*(a[3].z-a[1].z)-(a[2].z-a[1].z)*(a[3].y-a[1].y)); int B = ((a[2].z-a[1].z)*(a[3].x-a[1].x)-(a[2].x-a[1].x)*(a[3].z-a[1].z)); int C = ((a[2].x-a[1].x)*(a[3].y-a[1].y)-(a[2].y-a[1].y)*(a[3].x-a[1].x)); int D = -(A * a[1].x + B * a[1].y + C * a[1].z); int ret = A*a[4].x+B*a[4].y+a[4].z*C+D; if(ret == 0) puts("YES"); else puts("NO"); } return 0; }
51Nod 1265 四點共面