51Nod 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
Output示例
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
");
        }else{
            printf("No
");
        }
    }
    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;
}
原文地址:https://www.cnblogs.com/kimsimple/p/7203519.html