POJ 2546 & ZOJ 1597 Circular Area(求两圆相交的面积 模板)

题目链接:

POJ:http://poj.org/problem?id=2546

ZOJ:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=597

Description

Your task is to write a program, which, given two circles, calculates the area of their intersection with the accuracy of three digits after decimal point.

Input

In the single line of input file there are space-separated real numbers x1 y1 r1 x2 y2 r2. They represent center coordinates and radii of two circles.

Output

The output file must contain single real number - the area.

Sample Input

20.0 30.0 15.0 40.0 30.0 30.0

Sample Output

608.366

Source

Northeastern Europe 2000, Far-Eastern Subregion

题意:

求两圆相交的面积!

直接上模板

代码如下:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
//#include <complex>
//#include <iomanip>
using namespace std;
const double eps = 1e-8;
const double PI = acos(-1.0);

int sgn(double x)
{
    if(fabs(x) < eps) return 0;
    if(x < 0) return - 1;
    else return 1;
}
struct Point
{
    double x, y;
    Point(){}
    Point(double _x, double _y)
    {
        x = _x; y = _y;
    }
    Point operator -( const Point &b) const
    {
        return Point(x - b. x, y - b. y);
    }
    //叉积
    double operator ^ (const Point &b) const
    {
        return x*b. y - y*b. x;
    }
    //点积
    double operator * (const Point &b) const
    {
        return x*b. x + y*b. y;
    }
    //绕原点旋转角度B(弧度值),后x,y的变化
    void transXY(double B)
    {
        double tx = x,ty = y;
        x = tx* cos(B) - ty*sin(B);
        y = tx* sin(B) + ty*cos(B);
    }
};
//*两点间距离
double dist( Point a, Point b)
{
    return sqrt((a-b)*(a- b));
}
//两个圆的公共部分面积
double Area_of_overlap(Point c1, double r1, Point c2, double r2)
{
    double d = dist(c1,c2);
    if(r1 + r2 < d + eps) return 0;
    if(d < fabs(r1 - r2) + eps)
    {
        double r = min(r1,r2);
    return PI*r*r;
    }
    double x = (d*d + r1*r1 - r2*r2)/(2*d);
    double t1 = acos(x / r1);
    double t2 = acos((d - x)/r2);
    return r1*r1*t1 + r2*r2*t2 - d*r1*sin(t1);
}

int main()
{
    double x1, y1, r1, x2, y2, r2;
    while(~scanf("%lf%lf%lf%lf%lf%lf",&x1,&y1,&r1,&x2,&y2,&r2))
    {
        Point c1, c2;
        c1.x = x1;
        c1.y = y1;
        c2.x = x2;
        c2.y = y2;
        double ans = Area_of_overlap(c1,r1,c2,r2);
        printf("%.3lf\n",ans);
        //cout<<setiosflags(ios::fixed)<<setprecision(3)<<ans<<endl;
    }
    return 0;
}
时间: 2024-12-06 08:06:41

POJ 2546 & ZOJ 1597 Circular Area(求两圆相交的面积 模板)的相关文章

POJ 2546 &amp; ZOJ 1597 Circular Area 两圆的面积交

Circular Area Time Limit: 2 Seconds      Memory Limit: 65536 KB Your task is to write a program, which, given two circles, calculates the area of their intersection with the accuracy of three digits after decimal point. Input In the single line of in

poj 2546 Circular Area (两圆相交面积)

链接:poj 2546 题意:已知两圆的圆心和半径,求两圆相交部分的面积 分析:两圆的位置关系有三种:相离,相交,内含 相离时:相交面积为0 相交时,大扇形面积+小扇形面积-四边形面积 内含时,相交面积为小圆面积 #include<stdio.h> #include<math.h> #define PI acos(-1.0) typedef struct stu { double x,y; }point; double Distance(point a,point b) { ret

求两圆相交部分面积(C++)

已知两圆圆心坐标和半径,求相交部分面积: 1 #include <iostream> 2 using namespace std; 3 #include<cmath> 4 #include<stdio.h> 5 #define PI 3.141593 6 struct point//点 7 { 8 double x,y; 9 }; 10 struct circle//圆 11 { 12 point center; 13 double r; 14 }; 15 float

hdu 5120 (求两圆相交的面积

题意:告诉你两个圆环,求圆环相交的面积. /* gyt Live up to every day */ #include<cstdio> #include<cmath> #include<iostream> #include<algorithm> #include<vector> #include<stack> #include<cstring> #include<queue> #include<set&

求两圆相交的面积

走自己的路,你会发现,在走的过程中你会收获很多 #include <stdio.h> #include <stdlib.h> #include <malloc.h> #include <limits.h> #include <ctype.h> #include <string.h> #include <string> #include <algorithm> #include <iostream>

hdu 5120 (求两圆相交的面积的公式)

S = A大B大 - A大B小 - A小B大 + A小B小.(A表示A环,大表示大圆,B同).然后直接套模板,,,, 1 #include <stdio.h> 2 #include <algorithm> 3 #include <string.h> 4 #include <cmath> 5 using namespace std; 6 7 const double eps = 1e-8; 8 const double PI = acos(-1.0); 9 1

hdu 3264 Open-air shopping malls 求两圆相交

对每个圆二分半径寻找可行的最小半径,然后取最小的一个半径. 对于两圆相交就只要求到两个扇形,然后减去两个全等三角形就行了. #include<cstdio> #include<iostream> #include<cmath> #include<algorithm> using namespace std; #define pi acos(-1.0) #define eps 1e-8 #define maxn 50 int n; struct point{

poj2546Circular Area(两圆相交面积)

链接 画图推公式 这两种情况 都可用一种公式算出来 就是两圆都求出圆心角 求出扇形的面积减掉三角形面积 #include <iostream> using namespace std; #include<cmath> #include<iomanip> #include<algorithm> int main() { double d,t,t1,s,x,y,xx,yy,r,rr; while(cin>>x>>y>>r) {

求两圆相交面积模板

#define PI 3.141592654 #define eps 1e-8 double getdis(int x1,int y1,int x2,int y2){ return sqrt((double)(x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)); } double getarea(int x1,int y1,double r1,int x2,int y2,double r2){ double d=getdis(x1,y1,x2,y2); if(r1+r2<d+eps)