ド素人のメモ帳

解いた問題のプログラムとか載せてくと思います。

AOJ0023

Circles Intersection

■問題
2つの円の中点の座標と半径が与えられる。
1つの円がもう一つの円の中にあるか、円周が交わっているかいないかという2つの円の関係を求める。


■解法
2つの中点との距離hを求めて、
Br+h>Ar だったら円Aの中にBがある。
Ar+h>Br だったら円Bの中にAがある。
h>=Ar+Br だったら円周が交わっている。
と言うことになる。
何言ってるんだと思ったら絵を書いてみるとすぐわかる。


■ソース

#include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
int main(void){
  int n;
  scanf("%d",&n);
  for(int i=0;i<n;i++){
    double x[2],y[2],r[2],h;
    for(int j=0;j<2;j++) scanf("%lf %lf %lf",&x[j],&y[j],&r[j]);
    h=hypot(abs(x[0]-x[1]),abs(y[0]-y[1]));
    if(h+r[1]<r[0]) puts("2");
    else if(h+r[0]<r[1]) puts("-2");
    else if(h<=r[0]+r[1]) puts("1");
    else puts("0");
  }
}