四点共面 计算几何

点击打开链接

                     四点共面     

给出三维空间上的四个点(点与点的位置均不相同),判断这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"。 Sample Input
1
1 2 0
2 3 0
4 0 0
0 0 0
Sample Output
Yes

题解:

四个点两两组合,构造三个向量,形成一个行列式

若行列式的值为0,则共面;不为0,则不共面 


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
using namespace std;

struct Point
{
    int x,y,z;
} p[5];

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        for(int i=0; i<4; i++)
            scanf("%d %d %d",&p[i].x,&p[i].y,&p[i].z);
        Point s1,s2,s3;
        s1.x=p[1].x-p[0].x;
        s1.y=p[1].y-p[0].y;
        s1.z=p[1].z-p[0].z;
        s2.x=p[2].x-p[0].x;
        s2.y=p[2].y-p[0].y;
        s2.z=p[2].z-p[0].z;
        s3.x=p[3].x-p[0].x;
        s3.y=p[3].y-p[0].y;
        s3.z=p[3].z-p[0].z;
        int ans;
        ans=s1.x*s2.y*s3.z+s1.y*s2.z*s3.x+s1.z*s2.x*s3.y-s1.z*s2.y*s3.x-s1.x*s2.z*s3.y-s1.y*s2.x*s3.z;
        if(ans==0)
            puts("Yes");
        else
            puts("No");
    }
}


猜你喜欢

转载自blog.csdn.net/qq_40507857/article/details/80200415