結果

問題 No.132 点と平面との距離
ユーザー HachimoriHachimori
提出日時 2015-01-21 01:23:03
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 57 ms / 5,000 ms
コード長 1,659 bytes
コンパイル時間 438 ms
コンパイル使用メモリ 59,956 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-05 02:41:58
合計ジャッジ時間 971 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
4,380 KB
testcase_01 AC 18 ms
4,380 KB
testcase_02 AC 57 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<cmath>
#include<cstdio>
using namespace std;
const int BUF = 305;


class Point{
public:
    double x, y, z;
    Point(){}
    Point(double x, double y, double z):
        x(x), y(y), z(z){}

    Point operator- (const Point &opp) {
        return Point(x - opp.x, y - opp.y, z - opp.z);
    }

    void normalize() {
        double d = sqrt(x * x + y * y + z * z);
        x /= d;
        y /= d;
        z /= d;
    }
};

// 二つのベクトルの内積を取る
double dot(const Point &a, const Point &b) {
    return a.x * b.x + a.y * b.y + a.z * b.z;
}

// 二つのベクトルの外積を取る
Point cross(const Point &a, const Point &b) {
    return Point ((a.y * b.z) - (a.z * b.y), 
                  (a.z * b.x) - (a.x * b.z), 
                  (a.x * b.y) - (a.y * b.x));
}


int nPt;
Point P;
Point pt[BUF];

void read() {
    cin >> nPt;
    cin >> P.x >> P.y >> P.z;
    for (int i = 0; i < nPt; ++i)
        cin >> pt[i].x >> pt[i].y >> pt[i].z;
}


double calc(Point p, Point a, Point b, Point c) {
    // abc の法線ベクトル n を求める
    Point ab = b - a;
    Point bc = c - b;
    Point n = cross(ab, bc);
    n.normalize();
    
    // 平面上の任意の点として a をとり、 ベクトル pa と 法線ベクトル n の内積を取る
    Point pa = a - p;
    return fabs(dot(pa, n));
}


void work() {
    double sum = 0;
    for (int i = 0; i < nPt; ++i)
        for (int j = i + 1; j < nPt; ++j)
            for (int k = j + 1; k < nPt; ++k)
                sum += calc(P, pt[i], pt[j], pt[k]);
    printf("%.12lf\n", sum);
}


int main() {
    read();
    work();
    return 0;
}
0