結果

問題 No.622 点と三角柱の内外判定
ユーザー kk
提出日時 2021-02-15 17:38:34
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 1,500 ms
コード長 1,566 bytes
コンパイル時間 2,057 ms
コンパイル使用メモリ 203,232 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-30 14:42:41
合計ジャッジ時間 3,358 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

struct point {
  double x, y, z;
  point(double x, double y, double z): x(x), y(y), z(z) {}
  point(): x(0), y(0), z(0) {}
};

point operator-(const point& a, const point& b) {
  return point(a.x - b.x, a.y - b.y, a.z - b.z);
}

point operator*(double k, const point& p) {
  return point(k * p.x, k * p.y, k * p.z);
}

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

point cross(point a, 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);
}

double norm(point p) {
  return sqrt(dot(p, p));
}

// pから三角形abcに延ばした垂線の足の座標
point calc(point a, point b, point c, point p) {
  point n = cross(b - a, c - a);
  double len = dot(p - a, n) / norm(n);
  return p - len / norm(n) * n;
}

// pが三角形abcの内部にあるか?
bool check(point a, point b, point c, point p) {
  vector<point> tri{a, b, c};

  vector<point> products;
  for (int i = 0; i < tri.size(); i++) {
    products.push_back(cross(tri[(i+1) % tri.size()] - tri[i], p - tri[i]));
  }

  for (int i = 0; i < products.size(); i++) {
    if (dot(products[0], products[i]) < 0)
      return false;
  }
  
  return true;
}


int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  point a, b, c, p;
  cin >> a.x >> a.y >> a.z;
  cin >> b.x >> b.y >> b.z;
  cin >> c.x >> c.y >> c.z;
  cin >> p.x >> p.y >> p.z;

  point h = calc(a, b, c, p);
  if (check(a, b, c, h))
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
  
  return 0;
}
0