Description |
题目描述 |
Two different circles can have at most four common tangents. The picture below is an illustration of two circles with four common tangents. Now given the center and radius of two circles, your job is to find how many common tangents between them. |
两个不同的圆最多可以有4种公切线。 下图表示两个圆的4种公切线。 现在分别给你两个圆的圆心和半径,求他们之间有几条公切线。 |
Input |
输入 |
The first line contains an integer T, meaning the number of the cases (1 <= T <= 50.). For each test case, there is one line contains six integers x1 (−100 ≤ x1 ≤ 100), y1 (−100 ≤ y1 ≤ 100), r1 (0 < r1 ≤ 200), x2 (−100 ≤ x2 ≤ 100), y2 (−100 ≤ y2 ≤ 100), r2 (0 < r2 ≤ 200). Here (x1, y1) and (x2, y2) are the coordinates of the center of the first circle and second circle respectively, r1 is the radius of the first circle and r2 is the radius of the second circle. |
输入的首行是一个整数T,表示测试样例的数量。(1 <= T <= 50)。 每个测试样例都是一行6个整数,x1 (−100 ≤ x1 ≤ 100), y1 (−100 ≤ y1 ≤ 100), r1 (0 < r1 ≤ 200), x2 (−100 ≤ x2 ≤ 100), y2 (−100 ≤ y2 ≤ 100), r2 (0 < r2 ≤ 200)。 其中(x1, y1)与(x2, y2)分别是第一个圆与第二个圆的圆心,r1、r2分别是第一个圆与第二个圆的半径。 |
Output |
输出 |
For each test case, output the corresponding answer in one line. If there is infinite number of tangents between the two circles then output -1. |
每个测试样例的结果输出一行。 如果存在无数条公切线则输出-1。 |
Sample Input - 输入样例 |
Sample Output - 输出样例 |
3 10 10 5 20 20 5 10 10 10 20 20 10 10 10 5 20 10 5 |
4 2 3 |
【题解】
根据圆的位置关系分情况即可。
1. 圆心距>半径和,相离,4条公切线。
2. 圆心距==半径和,外切,3条公切线。
3. 半径差<圆心距<半径和,相交,2条公切线。
4. 圆心距==半径差,圆心不等,内切,1条公切线;圆心相等,重合,无数条公切线。
5. 圆心距<半径差,内含,0条公切线。
接着用平方表示就能解决误差的问题,直接可用int进行比较。
【代码 C++】
1 #include<cstdio> 2 #include<cstring> 3 int main(){ 4 int t, x1, y1, r1, x2, y2, r2, c_distance, r_sum, r_difference; 5 scanf("%d", &t); 6 while (t--){ 7 scanf("%d%d%d%d%d%d", &x1, &y1, &r1, &x2, &y2, &r2); 8 c_distance = (x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2); 9 r_sum = (r1 + r2)*(r1 + r2); 10 r_difference = r_sum - (r1*r2 << 2); 11 if (c_distance > r_sum) puts("4");//相离 12 else if (c_distance == r_sum) puts("3");//外切 13 else if (r_difference < c_distance &&c_distance < r_sum) puts("2");//相交 14 else if (c_distance == r_difference){ 15 if (x1 ^ y1 ^ x2 ^ y2) puts("1");//内切 16 else puts("-1");//重合 17 } 18 else puts("0");//内含 19 } 20 return 0; 21 }