POJ 2546 Circular Area【计算几何,计算两圆相交面积】

来源:互联网 发布:red hat linux是什么 编辑:程序博客网 时间:2024/05/28 15:09

Circular Area
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 5798 Accepted: 2268

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

原题链接:http://poj.org/problem?id=2546

题意:给出两个圆的圆心个半径,求两个圆的相交面积。

要求相交面积,首先要判断两圆的位置关系。

参考博客:http://www.cnblogs.com/luyingfeng/p/4130689.html

思路 : 分三种情况讨论

  • 假设半径小的圆为c1,半径大的圆为c2。
  • c1的半径r1,圆心坐标(x1,y1)。c2的半径r2,圆心坐标(x2,y2)。
  • d为两圆圆心连线的长度。
  • 相交面积为S
  • d=sqrt((x1-x2)^2+(y1-y2)^2)
  • 1)如果r1+r2<=d
  • 那么两圆相离,相交面积S=0
  • 2)如果r2-r1>=d
  • 那么半径小的圆内含半径大的圆,那么相交面积为小圆的面积S=pi*r1*r1
  • 3)既非(1)也非(2)
  • 在图上画两个相交圆,结合图像看。
  • 那么两圆相交,连接小圆的圆心与两个圆的交点,连接大圆的圆心和两个圆的交点。
  • 可以发现形成的图形被两个圆心的连线平分成2个全等三角形。
  • 由小圆圆心和交点所连两条线(长度为半径)以及在大圆之内的弧所形成的扇形为S1
  • 由大圆圆心和交点所连两条线(长度为半径)以及在小圆之内的弧所形成的扇形为S2
  • 由小圆圆心和交点所连两条线以及由大圆圆心和交点所连两条线所形成的四边形的面积为S3
  • 可见相交面积S=S1+S2-S3
  • 要求出扇形的面积,要知道扇形的圆心角。
  • 小圆包含的扇形的圆心角为2*a1(考虑一个三角形)
  • a1=acos((r1^2+d^2-r2^2)/(2.0*r1*d)) 余弦定理
  • a2=acos((r2^2+d^2-r1^2)/(2.0*r2*d)) 
  • S1=pi*r1*r1*2*a1/(2*pi)=a1*r1*r1
  • 同理
  • S2=a2*r2*r2
  • S3为一个三角形面积的2倍
  • S3=2*r1*d*sin(a1)/2=r1*d*sin(a1)
  • S=a1*r1*r1+a2*r2*r2-r1*d*sin(a1)
AC代码:

/*** 行有余力,则来刷题!  * 博客链接:http://blog.csdn.net/hurmishine  **/#include <cstdio>#include <iostream>#include <cmath>using namespace std;typedef double DB;DB PI=acos(-1.0);DB Area(DB x1,DB y1,DB r1,DB x2,DB y2,DB r2){    double d=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));    if(d>=r1+r2)        return 0;    else if(fabs(r1-r2)>=d)    {        if(r1>r2)            return PI*r2*r2;        else            return PI*r1*r2;    }    else    {        //正弦定理求扇形圆心角        double a1=2*acos((r1*r1+d*d-r2*r2)/2/r1/d);        double a2=2*acos((r2*r2+d*d-r1*r1)/2/r2/d);        //两个扇形面积和减去四边形的面积即为相交区域面积        //四边形面积再转化为两个三角形的面积之和来计算        double ans=r1*r1*a1/2+r2*r2*a2/2-r1*r1*sin(a1)/2-r2*r2*sin(a2)/2;        return ans;    }}int main(){    double x1,y1,r1,x2,y2,r2;    while(cin>>x1>>y1>>r1>>x2>>y2>>r2)    {        printf("%.3lf\n",Area(x1,y1,r1,x2,y2,r2));    }    return 0;}



0 0