[백준(Baekjoon)][자바(java)] (1002) 터렛 / 수학 2

728x90

 

import java.util.Scanner;

public class Main {

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

		double d;
		int x1, x2, y1, y2, r1, r2, t = sc.nextInt();
		
		for( int i = 0; i < t; i++ ) {
			
			x1 = sc.nextInt();
			y1 = sc.nextInt();
			r1 = sc.nextInt();
            
			x2 = sc.nextInt();
			y2 = sc.nextInt();
			r2 = sc.nextInt();
			
			d = Math.sqrt( ((x1-x2)*(x1-x2)) + ((y1-y2)*(y1-y2)) );
			
			if( (d == 0) && (r1 == r2) ) { 
				System.out.println(-1);
				continue;
			}
			if( (Math.abs(r1-r2) < d) && (d < r1+r2) )
				System.out.println(2);
			if( (Math.abs(r1-r2) == d) || (d == r1+r2) )
				System.out.println(1);
			if( (Math.abs(r1-r2) > d) || (d > r1+r2) || (d == 0) )
				System.out.println(0);
		}
        
		sc.close();
	}
}

 

원1

중심 좌표 : ( x1, y1 )

반지름 : r1

 

원2

중심 좌표 : ( x2, y2 )

반지름 : r2

 

두 원의 중심 좌표 사이의 거리 : d

 

------------------------------------

 

두 원의 교점의 개수 구하기

 

2개  r1-r2 < d < r1+r2
1개  r1-r2 = d   or   r1+r2 = d
0개  r1-r2 > d   or   r1+r2 < d
무한대  d = 0   and   r1 = r2

 

출처 : https://mathbang.net/101

 

반응형