## SaySomething

Talk is cheap, Show me the code.

# HDU-1798 Tell me the area (C++和Java【水】两圆相交求公共面积)

## HDU-1798 Tell me the area(两圆相交求公共面积)

Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Problem Description
There are two circles in the plane (shown in the below picture), there is a common area between the two circles. The problem is easy that you just tell me the common area.

Input
There are many cases. In each case, there are two lines. Each line has three numbers: the coordinates (X and Y) of the centre of a circle, and the radius of the circle.

Output
For each case, you just print the common area which is rounded to three digits after the decimal point. For more details, just look at the sample.

Sample Input
0 0 2
2 2 1

Sample Output
0.108

WA的情况：
1、PI不能写3.1415926，最好写acos（-1.0）
2、考虑情况（相切、相离、考虑等于0、包含 ）

#include "cstdio"
#include "cmath"
#include "iostream"
using namespace std;
#define PI acos(-1.0)
double dis(double x1,double y1,double x2,double y2)
{
return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
double cos_angle(double a,double b,double c){
return 0.5*(a*a+c*c-b*b)/(a*c);
}
int main(){
double d,x1,y1,r1,x2,y2,r2,s,s1_,s2_,s1_tri,s2_tri,an1,an2;
while(~scanf("%lf %lf %lf",&x1,&y1,&r1)){
scanf("%lf %lf %lf",&x2,&y2,&r2);
d = dis(x1,y1,x2,y2);
if(d>=r1+r2||!r1||!r2){//相切、相离、考虑等于0
printf("0.000\n");
continue;
}
if(d+r1<=r2){//包含
printf("%.3lf\n",PI*r1*r1);
continue;
}
if(d+r2<=r1){//包含
printf("%.3lf\n",PI*r2*r2);
continue;
}
an1 = acos(cos_angle(r1,r2,d));
an2 = acos(cos_angle(r2,r1,d));

s1_tri = 0.5*r1*r1*sin(an1*2);
s2_tri = 0.5*r2*r2*sin(an2*2);
s1_ = r1*r1*an1;
s2_ = r2*r2*an2;
s = (s1_-s1_tri)+(s2_-s2_tri);
printf("%.3lf\n",s);
}
return 0;
}

Java（S三角形 用海伦公式）

import java.io.*;
import java.math.*;
import java.util.*;
import java.text.*;

public class Main {
public static void main(String []args){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
double PI = Math.acos(-1.0);
double x1 = sc.nextDouble(); // 圆心x
double y1 = sc.nextDouble(); // 圆心y
double r1 = sc.nextDouble();

double x2 = sc.nextDouble();
double y2 = sc.nextDouble();
double r2 = sc.nextDouble();
double alpha,area;
double d = Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));//两圆心距离

if(d>=r1+r2||r1==0||r2==0){//相切、相离、考虑等于0
area=0;

}
else if(d+r1<=r2){//包含
area = PI*r1*r1;

}
else if(d+r2<=r1){//包含
area = PI*r2*r2;
}
else{
alpha=Math.acos((d*d+r1*r1-r2*r2)/(2*d*r1));//余弦定理取得相交弧所对本圆的圆心角
area=alpha*r1*r1;//本圆扇形面积
alpha=Math.acos((d*d+r2*r2-r1*r1)/(2*d*r2));//余弦定理取得相交弧所对另一圆的圆心角
area+=alpha*r2*r2;//另一圆的扇形面积
double s=(d+r1+r2)/2;//海伦公式之s
area-=Math.sqrt(s*(s-d)*(s-r1)*(s-r2))*2;//两扇形面积减去两三角形面积即为交集
}
DecimalFormat df = new DecimalFormat( "0.000");
System.out.println(df.format(area));
}
}
}

#### ACM--两圆交集的面积--HDOJ 1798--Tell me the area

2016-05-24 16:38:58

#### 求两圆相交部分面积 c++

2016-07-10 12:10:22

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

2016-11-23 20:39:50

#### 计算几何专题（计算两圆相交面积）

2017-07-31 16:53:40

#### 两圆相交求面积

2014-12-01 21:56:01

#### [模板] 求两圆相交面积（模板）

2014-08-06 12:00:40

#### 计算两个圆相交部分的面积

2014-11-17 21:42:40

#### POJ 2546 Circular Area（两个圆相交面积）

2014-08-15 10:42:52

#### 求两圆环相交部分的面积

2016-07-25 10:36:21

#### 【数学】求两个圆的相交部分的面积

2016-11-20 16:58:27