51NOD1265(四點共面)

題目鏈接:點擊打開鏈接


解題思路:

  判斷四點共面,先求出三點構成的平面的法向量(叉積),如果第四個點和前三點任意一點構成的向量與平面法向量垂直(點積爲0),則四點共面.

   回憶下叉積和點積.對於三位空間向量,叉積公式爲

=(
),
=(
),a×b=(
-
)i+(
-
)j+(
-
)k,

寫成行列式形式 .點積公式爲

 , x1 * x2 + y1 * y2,自行擴展至三維.


完整代碼:

#include <algorithm>
#include <iostream>
#include <cstring>
#include <climits>
#include <cstdio>
#include <string>
#include <cmath>
#include <set>
#include <queue>
#include <map>
#include <vector>
#include <cstdlib>
#include <stack>
#include <time.h>
using namespace std;
typedef long long LL;
const int MOD = int(1e9)+7;
const int INF = 0x3f3f3f3f;
const double EPS = 1e-9;
const double PI = acos(-1.0); //M_PI;
const int maxn = 100001;


class Point_3
{
public:
        double x , y , z;
        Point_3() {}
        Point_3(double xx , double yy , double zz) : x(xx)  , y(yy) , z(zz) {}
        void input()
        {
                scanf("%lf%lf%lf",&x,&y,&z);
        }
        friend Point_3 operator  - (const Point_3 &a , const Point_3 &b)
        {
                return Point_3(a.x - b.x , a.y - b.y , a.z - b.z);
        }
};

Point_3 det(const Point_3 &a , const Point_3 &b)
{
        return Point_3(a.y * b.z - a.z * b.y , a.z *b.x - a.x * b.z , a.x * b.y - a.y * b.x);
}

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

Point_3 pvec(Point_3 &s1 , Point_3 &s2 , Point_3 s3)
{
        return det((s1 - s2) , (s2 - s3));
}

bool zreo(double x)
{
        return fabs(x) < EPS;
}

int dots_onplane(Point_3  a , Point_3 b  , Point_3 c , Point_3 d )
{
        return zreo(dot(pvec(a , b , c ) , d - a));
}

int main()
{
#ifdef DoubleQ
    freopen("in.txt","r",stdin);
#endif
    int T;
    scanf("%d",&T);
    while(T--)
    {
            Point_3 a , b , c , d;
            a.input();
            b.input();
            c.input();
            d.input();
            if(dots_onplane(a , b , c , d))
                printf("Yes\n");
            else
                printf("No\n");
    }
    return 0;
}


/*************************************************
*
*   Copyright By DoubleQ
*   Written in 2015
*   Blog Address : zhanghe.ac.cn
*                  http://blog.csdn.net/u013447865
*   Email Address: [email protected]
*
*************************************************/



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章