AOJ - Problem 0023 : Circles Intersection
円と円が交わっていないか、交わっているか含まれているかを判定して出力する問題です。
2点間の距離と2つの円の半径の大小関係から判定できます。円のどちらかが含まれている場合は
半径を比較して小さい円が大きい円に含まれています。
#include <iostream> #include <string> using namespace std; int collision_detection(double x1, double y1, double x2, double y2, double r1, double r2){ double d = ((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2)); double sr = ((r1+r2)*(r1+r2)); double dr = ((r1-r2)*(r1-r2)); if( d > sr ){ return 0; }else if( d < sr && dr < d){ return 1; }else if(dr > d){ return (r1>r2)? 2 : -2; }else{ return 1; } } int main(){ int n, ans; double xa, ya, ra, xb, yb, rb; cin >> n; for(int i=0 ; i<n ; i++){ cin >> xa >> ya >> ra >> xb >> yb >> rb; ans = collision_detection( xa, ya, xb, yb, ra, rb); cout << ans << endl; } }