結果

問題 No.622 点と三角柱の内外判定
ユーザー noriocnorioc
提出日時 2019-10-30 01:33:11
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 2 ms / 1,500 ms
コード長 2,591 bytes
コンパイル時間 2,072 ms
コンパイル使用メモリ 165,208 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-04 03:16:26
合計ジャッジ時間 3,108 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,384 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,384 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,384 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 1 ms
4,384 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 2 ms
4,384 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 1 ms
4,384 KB
testcase_29 AC 1 ms
4,380 KB
testcase_30 AC 1 ms
4,376 KB
testcase_31 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std;

void scan(T...)(ref T a) {
    string[] ss = readln.split;
    foreach (i, t; T) a[i] = ss[i].to!t;
}
T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;

Vec3 readVec3() {
    double x, y, z; scan(x, y, z);
    return Vec3(x, y, z);
}

// 点 H が三角形 ABC の内側にあるか
bool isPointInTriangle(Vec3 H, Vec3 A, Vec3 B, Vec3 C) {
    auto AB = B - A;
    auto BC = C - B;
    auto CA = A - C;

    auto AH = H - A;
    auto BH = H - B;
    auto CH = H - C;

    auto a = AB.cross(AH);
    auto b = BC.cross(BH);
    auto c = CA.cross(CH);
    if (sgn(a.dot(b)) == sgn(b.dot(c)) && sgn(b.dot(c)) == sgn(c.dot(a))) {
        return true;
    }
    return false;
}

void main() {
    auto A = readVec3();
    auto B = readVec3();
    auto C = readVec3();
    auto D = readVec3();

    auto AB = B - A;
    auto AC = C - A;
    auto n = AB.cross(AC).normalized();

    auto AD = D - A;
    // AD ベクトルの n ベクトルへの射影ベクトル
    auto m = AD.dot(n) * n;
    // 点 D から三角形 ABC へ下ろした垂線の足を H とする
    auto AH = AD - m;
    // 点 H の座標
    auto H = A + AH;

    if (isPointInTriangle(H, A, B, C)) {
        writeln("YES");
    }
    else writeln("NO");
}

struct Vec3 {
    immutable double x;
    immutable double y;
    immutable double z;

    this(double x, double y, double z) {
        this.x = x;
        this.y = y;
        this.z = z;
    }

    double dot(Vec3 o) {
        return this.x * o.x + this.y * o.y + this.z * o.z;
    }

    Vec3 cross(Vec3 o) {
        auto x = this.y * o.z - o.y * this.z;
        auto y = this.z * o.x - o.z * this.x;
        auto z = this.x * o.y - o.x * this.y;
        return Vec3(x, y, z);
    }

    double mag() { return sqrt(magSq()); }
    double magSq() { return this.x * this.x + this.y * this.y + this.z * this.z; }

    Vec3 normalized() {
        auto m = mag();
        return Vec3(this.x / m, this.y / m, this.z / m);
    }

    Vec3 opUnary(string op)() if (op == "-") { return Vec3(-this.x, -this.y, -this.z); }
    Vec3 opBinary(string op)(Vec3 o) if (op == "+") { return Vec3(this.x + o.x, this.y + o.y, this.z + o.z); }
    Vec3 opBinary(string op)(Vec3 o) if (op == "-") { return Vec3(this.x - o.x, this.y - o.y, this.z - o.z); }
    Vec3 opBinary(string op)(double d) if (op == "*") { return Vec3(this.x * d, this.y * d, this.z * d); }
    Vec3 opBinaryRight(string op)(double d) if (op == "*") { return Vec3(this.x * d, this.y * d, this.z * d); }
}
0