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;
    }
  • 相关阅读:
    《转》2013年那些深入人心的小故事
    sklearn学习2-----LogisticsRegression
    sklearn学习汇总
    sklearn学习1----sklearn.SVM.SVC
    树(5)-----判断两颗树一样或者一棵树是否是另外一颗的子树
    树(4)-----树的高度
    面试题1-----SVM和LR的异同
    算法19-----(位运算)找出数组中出现只出现一次的数
    树(3)-----栈(迭代)
    python中的全局变量、局部变量、实例变量
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/5158528.html
Copyright © 2011-2022 走看看