您的位置:首页 > 其它

LightOJ - 1118 (计算几何模板)

2017-08-31 18:58 369 查看
问题描述:

In the biological lab, you were examining some of the molecules. You got some interesting behavior about some of the molecules. There are some circular molecules, when two of them collide, they overlap with each other, and it's hard to find that which one
is over the other one.

Given two molecules as circles, you have to find the common area of the given molecules that is shaded in the picture.



Input

Input starts with an integer T (≤ 12), denoting the number of test cases.

Each case contains six integers x1, y1, r1 and x2, y2, r2. Where (x1, y1) is the center of the first molecule and r1 is
the radius and (x2, y2) is the center of the second molecule and r2 is the radius. Both the radiuses are positive. No integer will contain more than 3 digits.

Output
For each test case, print the case number and the common area of the given molecules. Errors less than 10-6 will be ignored.

Sample Input

3
0 0 10 15 0 10
-10 -10 5 0 -10 10
100 100 20 100 110 20
Sample Output

Case 1: 45.3311753978
Case 2: 35.07666099
Case 3: 860.84369

题目题意:题目给我们俩给圆的圆心坐标和半径,让我们求出相交的面积。

题目分析:直接根据几何知识来算出面积的表达式(挺简单的),自己可以存下来,以后可以用.

代码如下:

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

typedef struct node
{
int x;
int 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()
{
point a, b;
int r1,r2,icase=1,t;
scanf("%d",&t);
while (t--) {
scanf("%d%d%d%d%d%d",&a.x,&a.y,&r1,&b.x,&b.y,&r2);
double re = AREA(a, r1, b, r2);
printf("Case %d: %.8lf\n", icase++,re);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: