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

原创 2016年05月31日 10:08:37

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三角形 用海伦公式)
第一次用JAVA写ACM,解决输入输出问题。

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));
        }
    }
}
版权声明:本文为博主原创文章,未经博主允许不得转载。

两圆相交求面积

两圆相交求面积,考虑两圆的位置关系再计算其面积: (因为是计算面积,不考虑两圆相切的情况) 1.两圆相离: 判定条件:两圆半径之和大于等于圆心距 计算方法:显然相交面积为0 2.两圆相含:两...

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

【问题描述】   给定平面上两个圆,求它们的公共部分的面积。 【输入格式】 一行有6个实数:x1,y1,r1,x2,y2,r2。其中r1,r2>0。【输出格式】 输出公共部分面积。【输入样例】...

hdu 5120 Intersection(求相交圆的面积)

画画图发现,两个相同的环重合部分的面积就是A大圆和B大圆相交的面积,减去A大圆和B小圆,减去A小圆和B大圆的面积,再加上两个小圆相交的面积,直接套,两圆相交的面积模板 S = A大B大 - ...

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

【问题描述】   给定平面上两个圆,求它们的公共部分的面积。 【输入格式】 一行有6个实数:x1,y1,r1,x2,y2,r2。其中r1,r2>0。【输出格式】 输出公共部分面积。【输入样例】...

hdu 1798 - Tell me the area(求两圆相交面积) 解题报告

Tell me the area Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others...
  • Luwhere
  • Luwhere
  • 2015年08月10日 15:04
  • 526

HDU 1798 Tell me the area (两圆相交面积)

Tell me the area Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)...

HDU 1798 Tell me the area(计算几何,求相交圆面积)

题目地址 http://acm.hdu.edu.cn/showproblem.php?pid=1798 Tell me the area Time Limit: 3000/1000 MS...

Tell me the area(计算几何--求两圆相交面积)

Problem Link:http://acm.hdu.edu.cn/showproblem.php?pid=1798 Tell me the area Time Limit: 3...

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

HDOJ题目地址:传送门 Tell me the area Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768...

HDU 1798 Tell me the area

Tell me the area Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:HDU-1798 Tell me the area (C++和Java【水】两圆相交求公共面积)
举报原因:
原因补充:

(最多只允许输入30个字)