터렛

이 문제는 기하학적으로 두 원의 교점을 구하는 문제입니다. 두 원이 서로 어떻게 위치하는지에 따라 교점의 개수가 달라집니다.

두 원의 위치 관계

  1. 두 원이 일치하는 경우: * 중심이 같고 반경도 같은 경우 무수히 많은 교점을 가집니다 (출력: -1).
  2. 두 원이 외접하는 경우: * 두 원이 한 점에서 외접하는 경우 교점은 1개입니다 (출력: 1).
  3. 두 원이 내접하는 경우: * 한 원이 다른 원의 내부에 접하는 경우 교점은 1개입니다 (출력: 1).
  4. 두 원이 두 점에서 만나는 경우: * 두 원이 서로 교차하는 경우 교점은 2개입니다 (출력: 2).
  5. 두 원이 만나지 않는 경우: * 두 원이 서로 만나지 않는 경우 교점은 0개입니다 (출력: 0).
import java.util.Scanner;

/**
 * 제목 : 터렛
 * 링크 : https://www.acmicpc.net/problem/1002
 * 분류 : 기하 알고리즘
 */
public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int t = sc.nextInt();

		for (int i = 0; i < t; i++) {
			int x1 = sc.nextInt();
			int y1 = sc.nextInt();
			int r1 = sc.nextInt();
			int x2 = sc.nextInt();
			int y2 = sc.nextInt();
			int r2 = sc.nextInt();

			System.out.println(turret(x1, y1, r1, x2, y2, r2));
		}
	}

	private static int turret(int x1, int y1, int r1, int x2, int y2, int r2) {
		int result = 0;
		if (x1 == x2 && y1 == y2) {
			if (r1 == r2) {
				result = -1;
			} else {
				result = 0;
			}
		} else {
			if (r1 + r2 == dist(x1, x2, y1, y2) || Math.abs(r1 - r2) == dist(x1, x2, y1, y2)) {
				result = 1;
			}
			if (r1 + r2 > dist(x1, x2, y1, y2) && Math.abs(r1 - r2) < dist(x1, x2, y1, y2)) {
				result = 2;
			}
		}
		return result;
	}

	private static double dist(int x1, int x2, int y1, int y2) {
		return Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2));
	}
}