[51Nod](1265)四点共面 ---- 计算几何(四点共面模板)

给出三维空间上的四个点(点与点的位置均不相同),判断这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

关于计算几何: 直接学习的是CSDN一位超励志的学姐的模板,也希望这个学姐今天考研顺利吧!
直接摆上她的地址:
https://blog.csdn.net/f_zyj/article/details/52060576

原理: 空间中3个向量的混合积如果!=0,说明能够构成平行六面体,即4点不共面,如果==0,说明4点共面,也代表平行六面体体积是0

AC代码:

#include<bits/stdc++.h>
using namespace std;
struct  point
{
    double x, y, z;
    point  operator - (point &o)
    {
        point  ans;
        ans.x = this->x - o.x;
        ans.y = this->y - o.y;
        ans.z = this->z - o.z;
        return ans;
    }
};

double  dot_product(const point &a, const point &b)
{
    return a.x * b.x + a.y * b.y + a.z * b.z;
}

point cross_product(const point &a, const point &b)
{
    point  ans;
    ans.x = a.y * b.z - a.z * b.y;
    ans.y = a.z * b.x - a.x * b.z;
    ans.z = a.x * b.y - a.y * b.x;
    return ans;
}
int main()
{
    #ifdef LOCAL
    freopen("in.txt","r",stdin);
    #endif // LOCAL
    ios_base::sync_with_stdio(false);
    cin.tie(NULL),cout.tie(NULL);
    point p[4];
    int t;
    cin>>t;
    while(t--)
    {
        for (int i = 0; i < 4; ++i)
        {
            cin>>p[i].x>>p[i].y>>p[i].z;
        }
        puts(dot_product(p[3] - p[0], cross_product(p[2] - p[0], p[1] - p[0])) == 0.0 ? "Yes\n" : "No\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/m0_37624640/article/details/80030467