随笔 - 346,  文章 - 0,  评论 - 39,  阅读 - 26万

题目链接

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

分析:

两圆的位置关系有相离、相切和相交三种,相切又有外切和内切。

如果两圆的位置关系是相离或者外切的话,它们是没有公共部分的,及面积为0;

如果两圆内切,公共部分就为整个的小圆,面积也就是小圆的面积;

如果两圆相交的话,面积就应该是每个圆所对应的扇形减去所对应的三角形的面积和。

代码:

#include<stdio.h>
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
    double q,w,m,n,a,b,c,x,y,z,PI;
    PI=2*asin(1.0);
    while(~scanf("%lf%lf%lf",&a,&b,&c))
    {
        scanf("%lf%lf%lf",&x,&y,&z);
        a=sqrt((a-x)*(a-x)+(b-y)*(b-y));///计算圆心距
        ///如果两圆相离、外切或至少一圆半径为0时,那么所求面积为0
        if(a>=c+z||!c||!z)x=0;
        ///如果两内切或内含,那么所求面积为小圆面积
        else if(a<=fabs(z-c))
        {
            if(z>c)z=c;
            x=z*z*PI;
        }
        ///如果两圆相交,面积求解如下
        else
        {
            ///由余弦定理求出公共弦在圆o1中对应的圆心角的一半
            b=acos((a*a+c*c-z*z)/(2*a*c));
            ///由余弦定理求出公共弦在圆o2中对应的圆心角的一半
            y=acos((a*a+z*z-c*c)/(2*a*z));
            ///计算圆o1中扇形面积
            m=b*c*c;
            ///计算圆o2中扇形面积
            n=y*z*z;
            ///计算圆o1中扇形所对应的三角形面积
            q=c*c*sin(b)*cos(b);
            ///计算圆o2中扇形所对应的三角形面积
            w=z*z*sin(y)*cos(y);
            ///q+w为图中四边形面积,两扇形面积之和与四边形面积之差即为
            ///所求面积。在图2中y为钝角,计算出的面积w为负值,这时q+w
            ///表示两三角面积之差,刚好还是四边形面积,因此对于图1和图
            ///2不必分情况讨论
            x=m+n-(q+w);
        }
        printf("%.3f\n",x);
    }
    return 0;
}
posted on   渡……  阅读(207)  评论(0编辑  收藏  举报
编辑推荐:
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· .NET 适配 HarmonyOS 进展
阅读排行:
· 用 DeepSeek 给对象做个网站,她一定感动坏了
· DeepSeek+PageAssist实现本地大模型联网
· 手把手教你更优雅的享受 DeepSeek
· Java轻量级代码工程
· 从 14 秒到 1 秒:MySQL DDL 性能优化实战

< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8
点击右上角即可分享
微信分享提示