FZU 2213——Common Tangents——————【两个圆的切线个数】

Problem 2213 Common Tangents

Accept: 7    Submit: 8
Time Limit: 1000 mSec    Memory Limit : 32768 KB

 Problem 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.

 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.

 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.

 Sample Input

3
10 10 5 20 20 5
10 10 10 20 20 10
10 10 5 20 10 5

 Sample Output

4
2
3

 Source

第六届福建省大学生程序设计竞赛-重现赛(感谢承办方华侨大学)
 
 
题目大意:给你两个圆的圆心和半径,问你一共有多少条切线。
 
解题思路:讨论圆心距跟半径和与差的关系。
 
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<string.h>
using namespace std;
struct Circle{
    double x,y,r;
};
int getTangents(Circle A,Circle B){
    int cnt = 0;
    if(A.r < B.r){ swap(A,B); } //固定A为大圆
    int d2 = (A.x-B.x)*(A.x-B.x) + (A.y-B.y)*(A.y-B.y); //圆心距的平方
    int rdiff = A.r - B.r;  //半径差
    int rsum = A.r + B.r;   //半径和
    if(d2 < rdiff*rdiff) return 0;  //内含
    double base = atan2(B.y-A.y,B.x-A.x);   //求出两个圆心所在直线与x轴正方向的夹角
    if(d2 == 0 && A.r == B.r) return -1;    //两个圆相等,无数条切线
    if(d2 == rdiff*rdiff){  //内切,一条切线
        cnt++;
        return 1;
    }
    double ang = acos((A.r-B.r)/sqrt(d2));
    //两条外公切线
    cnt++; cnt++;
    if(d2 == rsum*rsum){    //外切,一条内公切线
        cnt++;  
    }else if(d2 > rsum*rsum){   //相离,两条内公切线
        cnt++; cnt++;
    }
    return cnt;
}
int main(){
    int T;
    Circle r1, r2;
    scanf("%d",&T);
    while(T--){
        scanf("%lf%lf%lf%lf%lf%lf",&r1.x,&r1.y,&r1.r,&r2.x,&r2.y,&r2.r);
        int ans = getTangents(r1,r2);
        printf("%d
",ans);
    }
    return 0;
}

  

原文地址:https://www.cnblogs.com/chengsheng/p/5080823.html