計算幾何学 / C++とPythonで同じことをやっても挙動が変わってしまう

C++

#include <bits/stdc++.h>#include <math.h>using namespace std;#define ll long long /* judge by yes or no */void judge(bool x) { if (x) { cout << "Yes" << endl; } else { cout << "No" << endl; } } ll cross_product(vector<ll> x, vector<ll> y) { ll ans = x.at(0) * y.at(1) - y.at(0) * x.at(1); return ans;} pair<ll, ll> larger(pair<ll, ll> x, pair<ll, ll> y) { if (x < y) { return y; } else { return x; }}pair<ll, ll> smaller(pair<ll, ll> x, pair<ll, ll> y) { if (x > y) { return y; } else { return x; }} void solve() { ll x1, y1, x2, y2, x3, y3, x4, y4; cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3 >> x4 >> y4; pair<ll, ll> A = {x1, y1}; pair<ll, ll> B = {x2, y2}; pair<ll, ll> C = {x3, y3}; pair<ll, ll> D = {x4, y4}; if (A > B) { swap(A, B); } if (C > D) { swap(C, D); } x1 = A.first; y1 = A.second; x2 = B.first; y2 = B.second; x3 = C.first; y3 = C.second; x4 = D.first; y4 = D.second; vector<ll> AB = {x2 - x1, y2 - y1}; vector<ll> BA = {x1 - x2, y1 - y2}; vector<ll> AC = {x3 - x1, y3 - y1}; vector<ll> CA = {x1 - x3, y1 - y3}; vector<ll> AD = {x4 - x1, y4 - y1}; vector<ll> DA = {x1 - x4, y1 - y4}; vector<ll> BC = {x3 - x2, y3 - y2}; vector<ll> CB = {x2 - x3, y2 - y3}; vector<ll> BD = {x4 - x2, y4 - y2}; vector<ll> DB = {x2 - x4, y2 - y4}; vector<ll> CD = {x4 - x3, y4 - y3}; vector<ll> DC = {x3 - x4, y3 - y4}; ll cp1 = cross_product(AB, AC); ll cp2 = cross_product(AB, AD); ll cp3 = cross_product(CD, CA); ll cp4 = cross_product(CD, CB); bool flag = true; if (cp1 == 0 && cp2 == 0 && cp3 == 0 && cp4 == 0) { if (larger(A, C) > smaller(B, D)) { flag = false; } } else { if (cp1 * cp2 > 0 || cp3 * cp4 > 0) { flag = false; } } judge(flag);} int main() { solve();}

コメントを投稿

0 コメント