1. 程式人生 > >5120 Intersection(簡單幾何)——2014ACM/ICPC亞洲區北京站

5120 Intersection(簡單幾何)——2014ACM/ICPC亞洲區北京站

傳送門
Matt is a big fan of logo design. Recently he falls in love with logo made up by rings. The following figures are some famous examples you may know這裡寫圖片描述.


A ring is a 2-D figure bounded by two circles sharing the common center. The radius for these circles are denoted by r and R (r < R). For more details, refer to the gray part in the illustration below.

這裡寫圖片描述



Matt just designed a new logo consisting of two rings with the same size in the 2-D plane. For his interests, Matt would like to know the area of the intersection of these two rings.


Input The first line contains only one integer T (T ≤ 105), which indicates the number of test cases. For each test case, the first line contains two integers r, R (0 ≤ r < R ≤ 10).

Each of the following two lines contains two integers xi
, yi (0 ≤ xi, yi ≤ 20) indicating the coordinates of the center of each ring.
Output For each test case, output a single line “Case #x: y”, where x is the case number (starting from 1) and y is the area of intersection rounded to 6 decimal places.

Sample Input
2 
2 3
0 0
0 0
2 3
0 0

題目大意:

給兩個一樣大的圓環,圓心座標不同,求兩個圓環相交的面積

解題思路:

在紙上畫畫圖發現,就是:
兩個大圓相交面積-大圓1與小圓2相交面積-大圓2與小圓1相交面積+兩個小圓相交面積
求圓相交面積有模板,一套就好了。

程式碼:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
const double eps = 1e-8;
const double PI = acos(-1);
// 幾何誤差修正
inline int cmp(double x) {
    return x < -eps ? -1 : (x > eps);
}
// 計算x的平方
inline double sqr(double x) {
    return x * x;
}
// 開方誤差修正
inline double mySqrt(double n) {
    return sqrt(max((double)0, n));
}

// 二維點(向量)類
struct Point {
    double x, y;
    Point() {}
    Point(double x, double y): x(x), y(y) {}
    void input() {
        scanf("%lf%lf", &x, &y);
    }
    friend Point operator + (const Point& a, const Point& b) {
        return Point(a.x + b.x, a.y + b.y);
    }
    friend Point operator - (const Point& a, const Point& b) {
        return Point(a.x - b.x, a.y - b.y);
    }
    friend bool operator == (const Point& a, const Point& b) {
        return cmp(a.x - b.x) == 0 && cmp(a.y - b.y) == 0;
    }
    friend Point operator * (const Point& a, const double& b) {
        return Point(a.x * b, a.y * b);
    }
    friend Point operator * (const double& a, const Point& b) {
        return Point(a * b.x, a * b.y);
    }
    friend Point operator / (const Point& a, const double& b) {
        return Point(a.x / b, a.y / b);
    }
    // 返回本向量的長度
    double norm() {
        return sqrt(sqr(x) + sqr(y));
    }
    // 返回本向量對應的單位向量
    Point unit() {
        return Point(x, y) / norm();
    }
};
//求兩圓相交面積
double intersect(double x1,double y1,double r1,double x2,double y2,double r2){
    double s,temp,p,l,ans;
    l=sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
    if(l>=r1+r2) ans=0;
    else if(l<=fabs(r1-r2)){
        if(r1<=r2) ans=PI*r1*r1;
        else ans=PI*r2*r2;
    }
    else{
        p=(l+r1+r2)/2;
        s=2*sqrt(p*(p-l)*(p-r1)*(p-r2));
        if(r1>r2){
            temp=x1;x1=x2;x2=temp;
            temp=y1;y1=y2;y2=temp;
            temp=r1;r1=r2;r2=temp;
        }
        ans=acos((r1*r1+l*l-r2*r2)/(2*r1*l))*r1*r1+acos((r2*r2+l*l-r1*r1)/(2*r2*l))*r2*r2-s;
    }
    return ans;
}

int main()
{
    int T; scanf("%d", &T);
    for(int cas=1; cas<=T; cas++){
        double r, R; scanf("%lf%lf", &r, &R);
        double x1, y1, x2, y2; scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
        double ans1 = intersect(x1, y1, R, x2, y2, R);
        double ans2 = intersect(x1, y1, R, x2, y2, r);
        double ans3 = intersect(x1, y1, r, x2, y2, R);
        double ans4 = intersect(x1, y1, r, x2, y2, r);
        double ans = ans1-ans2-ans3+ans4;
        printf("Case #%d: %.6f\n",cas,ans);
    }
    return 0;
}