1. 程式人生 > >HDU 5120 (計算幾何+圓相交)

HDU 5120 (計算幾何+圓相交)

問題描述:

Matt is a big fan of logo design. Recently he falls in love with logo made up by rings. The following figures are some famous examples you may know.


A ring is a 2-D figure bounded by two circles sharing the common center. The radius for these circles are denoted by r and R (r < R). For more details, refer to the gray part in the illustration below.


Matt just designed a new logo consisting of two rings with the same size in the 2-D plane. For his interests, Matt would like to know the area of the intersection of these two rings.

Input

The first line contains only one integer T (T ≤ 105), which indicates the number of test cases. For each test case, the first line contains two integers r, R (0 ≤ r < R ≤ 10).

Each of the following two lines contains two integers xi

, yi (0 ≤ xi, yi ≤ 20) indicating the coordinates of the center of each ring.

Output

For each test case, output a single line “Case #x: y”, where x is the case number (starting from 1) and y is the area of intersection rounded to 6 decimal places.

Sample Input 

2
2 3
0 0
0 0
2 3
0 0
5 0
Sample Output
Case #1: 15.707963
Case #2: 2.250778
題目題意:題目給我們倆個大小相同的圓環,圓心座標不同,問這個倆個圓環相交的面積是多少。

題目分析:這個題目主要的是要搞清楚圓環有那幾種情況(畫圖不方便害羞,我描述一下)

將圓環看出倆個大小圓。

1:倆個圓環的大圓相離

2:倆個圓環的大圓相交,大圓與小圓相離

3:倆個圓環的大圓相交,大圓與小圓相交,小圓與小圓相離

4:倆個圓環的大圓相交,大圓與小圓相交,小圓與小圓相交

5:倆個圓環重合

綜上可以得到面積=倆個圓環的大圓相交的面積-一個圓環的大圓與另一個小圓相交的面積*2(因為對稱)+倆個圓環的小圓與小圓相交。

畫圖可以很直觀!

程式碼如下:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#define PI acos(-1.0)
using namespace std;

const double eps=1e-9;
typedef struct node
{
	int x;
	int y;
	bool operator==(const struct node &a) const {
	    return x==a.x&&y==a.y;
	}
}point;
double AREA(point a, double r1, point b, double r2)//座標加半徑
{
	double d = sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));
	if (d >= r1+r2)
		return 0;
	if (r1>r2) swap (r1,r2);
	if(r2 - r1 >= d)
		return PI*r1*r1;
	double ang1=acos((r1*r1+d*d-r2*r2)/(2*r1*d));
	double ang2=acos((r2*r2+d*d-r1*r1)/(2*r2*d));
	return ang1*r1*r1 + ang2*r2*r2 - r1*d*sin(ang1);
}

int main()
{
    int t,icase=1;
    scanf("%d",&t);
    while (t--) {
        point a,b;
        double r,R;
        scanf("%lf%lf",&r,&R);
        scanf("%d%d%d%d",&a.x,&a.y,&b.x,&b.y);
        printf("Case #%d: %.6f\n",icase++,AREA(a,R,b,R)-2*AREA(a,R,b,r)+AREA(a,r,b,r));
    }
    return 0;
}