zoukankan      html  css  js  c++  java
  • codeforces D. Area of Two Circles' Intersection 计算几何

    D. Area of Two Circles' Intersection
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    You are given two circles. Find the area of their intersection.

    Input

    The first line contains three integers x1, y1, r1 ( - 109 ≤ x1, y1 ≤ 109, 1 ≤ r1 ≤ 109) — the position of the center and the radius of the first circle.

    The second line contains three integers x2, y2, r2 ( - 109 ≤ x2, y2 ≤ 109, 1 ≤ r2 ≤ 109) — the position of the center and the radius of the second circle.

    Output

    Print the area of the intersection of the circles. The answer will be considered correct if the absolute or relative error doesn't exceed10 - 6.

    Sample test(s)
    input
    0 0 4
    6 0 4
    output
    7.25298806364175601379
    input
    0 0 5
    11 0 5
    output
    0.00000000000000000000

     题意:求两个圆相交面积,主要问题就是精度的问题;

    解决思路:在两个圆心相距很远的时候用两个圆心和一个交点算角度的精度可能不够,可以用两个交点和一个圆心来算比较好;

    AC代码:

    #include <bits/stdc++.h>
    #define pi acos(-1.0)
    using namespace std;
     long double x1,x2,y2,r1,r2,y;
     long double area(){
     long double d=sqrt((x1-x2)*(x1-x2)+(y-y2)*(y-y2));
     if(r1>r2){
     long double temp=r1;
     r1=r2;
     r2=temp;
     }
     if(r1+r2<=d)  return 0;
     else if(r2-r1>=d)  return pi*r1*r1;
     else   {
        long double a1=acos((r1*r1+d*d-r2*r2)/(2.0*r1*d));
        long double a2=acos((r2*r2+d*d-r1*r1)/(2.0*r2*d));
        a1*=2;
        a2*=2;
        return (a1*r1*r1+a2*r2*r2-r1*r1*sin(a1)-r2*r2*sin(a2))/2.0;
            }
    }
    int main()
    {
        cin>>x1>>y>>r1;
        cin>>x2>>y2>>r2;
        cout<<fixed<<area()<<endl;
    
        return 0;
    }
  • 相关阅读:
    第三课 Spinner的使用
    Deployment failed due to an error in FastDev assembly synchronization.
    第二课 两个视图+数据传输
    第一课 Hello
    Tab的键的妙用
    RelativeLayout相对布局中拖放控件的办法
    更改layout的布局
    关于MultiDataTrigger和MultiTrigger的一些注意事项
    WPF中XAML的触发器的属性,事件 都有那些?以及如何寻找
    EventTrigger
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/5158528.html
Copyright © 2011-2022 走看看