Aizu Online Judge(AOJ)の過去問から、その提出コードの解答例です。
・問題 "Intersection"
https://onlinejudge.u-aizu.ac.jp/problems/CGL_2_B
・交差判定
僕が作成、提出したコードは、以下のとおりです。
Aizu Online Judge in C++ #CGL_2_B : Intersection
/* Aizu Online Judge in C++ #CGL_2_B : Intersection https://onlinejudge.u-aizu.ac.jp/problems/CGL_2_B 提出コードの解答例 https://neguse-atama.hatenablog.com */ #include<bits/stdc++.h> using namespace std; typedef complex<double> P; using ld=long double; P inputP(void){ ld x,y; cin>>x>>y; return P(x,y); } ld f(P a,P b){ return (a*conj(b)).imag(); } bool tf(P a,P b){ P p=a*conj(b); return (p.imag()==0 && p.real()>=0 && abs(a)>abs(b)); } int main(void){ int q; cin>>q; while(q--){ P a=inputP(),b=inputP(),c=inputP(),d=inputP(); cout<<(f(b-a,c-a)*f(b-a,d-a)<0 && f(d-c,a-c)*f(d-c,b-c)<0 || tf(b-a,c-a) || tf(b-a,d-a) || tf(d-c,a-c) || tf(d-c,b-c))<<endl; } return 0; }
設問の出典は、プログラミング問題のオンライン採点システム「Aizu Online Judge(AOJ)」です。
http://judge.u-aizu.ac.jp/onlinejudge/