您的位置:首页 > 其它

poj 2546 Circular Area(两个圆相交的面积)

2016-07-27 15:08 567 查看
Circular Area

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 5716Accepted: 2239
Description

Your task is to write a program, which, given two circles, calculates the area of their intersection with the accuracy of three digits after decimal point.
Input

In the single line of input file there are space-separated real numbers x1 y1 r1 x2 y2 r2. They represent center coordinates and radii of two circles.
Output

The output file must contain single real number - the area.
Sample Input

20.0 30.0 15.0 40.0 30.0 30.0

Sample Output

608.366

Source

Northeastern Europe 2000, Far-Eastern Subregion

题意:给第一个圆的圆心坐标和半径,再给第二个圆的圆心坐标和半径,求两个圆的相交面积。

几何题,刚开始接触,照着别人代码理解完成自己的代码。


两种情况一次分析就好,情况相同~

附上代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#define ll long long
#define M 100005
using namespace std;

const double  pi = acos(-1.0);

struct Round
{
double x,y;
double r;
} rr[2];

double dis(Round a, Round b)
{
return sqrt((a.x - b.x)*(a.x - b.x)+(a.y - b.y)*(a.y - b.y)); ///求两点之间的距离
}

double solve(Round a, Round b)
{
double d = dis(a, b);
if(d >= a.r + b.r)  ///相离的情况
return 0;
else if(d <= fabs(a.r - b.r)) ///内含的情况
{
double r = a.r < b.r?a.r : b.r;
return pi*r*r;
}
double ang1 = acos((a.r * a.r + d * d - b.r * b.r) / 2.0 / a.r / d);
///公式: cos(A)=(b^2+c^2-a^2)/2bc acos(cos(A))求出角A的弧度
double ang2 = acos((b.r * b.r + d * d - a.r * a.r) / 2.0 / b.r / d);
double ret = ang1 * a.r * a.r + ang2 * b.r * b.r - d * a.r * sin(ang1);
///扇形面积s=弧度*r^2/2 三角形面积=a*b*sin(A)/2
return ret;
}

int main()
{
while(~scanf("%lf%lf%lf%lf%lf%lf",&rr[0].x,&rr[0].y,&rr[0].r,&rr[1].x,&rr[1].y,&rr[1].r))
{
printf("%.3lf\n",solve(rr[0], rr[1]));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: