hdu5858Hard problem+计算几何(圆的面积交)

本文介绍了一道小学奥林匹克数学题目,通过给定正方形边长来计算特定图形中阴影部分的面积。利用三个圆之间的交集计算阴影面积,提供了一个C++实现代码示例。

Problem Description
cjj is fun with math problem. One day he found a Olympic Mathematics problem for primary school students. It is too difficult for cjj. Can you solve it?

这里写图片描述
Give you the side length of the square L, you need to calculate the shaded area in the picture.

The full circle is the inscribed circle of the square, and the center of two quarter circle is the vertex of square, and its radius is the length of the square.

Input
The first line contains a integer T(1<=T<=10000), means the number of the test case. Each case contains one line with integer l(1<=l<=10000).

Output
For each test case, print one line, the shade area in the picture. The answer is round to two digit.

Sample Input

1
1

Sample Output

0.29

Author
BUPT

Source
2016 Multi-University Training Contest 10

题意:给你正方形的边长,求阴影部分的面积。。。
解法:中间那个圆的非阴影部分就是。3个圆的的面积交。。所以阴影部分的面积就是圆的面积-3个圆的交面积。。。

#include<bits/stdc++.h>
using namespace std;

const double eps=1e-8;
int cmp(double x){
    if(fabs(x)<eps) return 0;
    if(x>0) return 1;
    return -1;
}
const double pi=acos(-1.0);
inline double sqr(double x){
    return x*x;
}
struct point{
    double x,y;
    point(){};
    point(double a,double b):x(a),y(b){};
    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));
    }
};
double det(const point &a,const point &b){
    return a.x*b.y-a.x*b.y;
}
double dot(const point &a,const point &b){
    return a.x*b.x+a.y*b.y;
}
double dis(const point &a,const point &b){
    return (a-b).norm();
}
point rotate_point(const point &p,double A){
    double tx=p.x,ty=p.y;
    return point(tx*cos(A)-ty*sin(A),tx*sin(A)+ty*cos(A));
}
int dcmp(double k){
    return k<-eps?-1:k>eps?1:0;
}
double cross(const point &a,const point &b){
    return a.x*b.y-a.y*b.x;
}
double abs(const point &o){
    return sqrt(dot(o,o));
}
struct Circle{
    point p;
    double r;
    bool operator < (const Circle &o) const{
        if(dcmp(r-o.r)!=0) return dcmp(r-o.r)==-1;
        if(dcmp(p.x-o.p.x)!=0){
            return dcmp(p.x-o.p.x)==-1;
        }
        return dcmp(p.y-o.p.y)==-1;
    }
    bool operator == (const Circle &o) const{
        return dcmp(r-o.r)==0&&dcmp(p.x-o.p.x)==0&&dcmp(p.y-o.p.y)==0;
    }
};

//圆的交面积
double area(Circle &x,Circle &y){
    double a=dis(x.p,y.p),b=x.r,c=y.r;
    double cta1=acos((a*a+b*b-c*c)/2/(a*b)),
           cta2=acos((a*a+c*c-b*b)/2/(a*c));
    double s1=b*b*cta1-b*b*sin(cta1)*(a*a+b*b-c*c)/2/(a*b);
    double s2=c*c*cta2-c*c*sin(cta2)*(a*a+c*c-b*b)/2/(a*c);
    return s1+s2;
}

//圆的面积并。
point rotate(const point &p,double cost,double sint){
    double x=p.x,y=p.y;
    return point(x*cost-y*sint,x*sint+y*cost);
}
pair<point,point> crosspoint(point ap,double ar,point bp,double br){
    double d=(ap-bp).norm();
    double cost=(ar*ar+d*d-br*br)/(2*ar*d);
    double sint=sqrt(1.0-cost*cost);
    point v=(bp-ap)/(bp-ap).norm()*ar;
    return make_pair(ap+rotate(v,cost,-sint),ap+rotate(v,cost,sint));
}
inline pair<point ,point> crosspoint(const Circle &a,const Circle &b){
    return crosspoint(a.p,a.r,b.p,b.r);
}
Circle tc[10]; //待求圆
int m;         //待求圆的个数
struct Node{
    point p;
    double a;
    int d;
    Node(const point &p,double a,int d):p(p),a(a),d(d){};
    bool operator <(const Node &o)const{
        return a<o.a;
    }
};
double arg(point p){
    return arg(complex<double>(p.x,p.y));
}
double solve(){   //返回并面积
    int n=0;
    Circle c[10];
    sort(tc,tc+m);
    m=unique(tc,tc+m)-tc;
    for(int i=m-1;i>=0;--i){
        bool ok=true;
        for(int j=i+1;j<m;j++){
            double d=(tc[i].p-tc[j].p).norm();
            if(dcmp(d-abs(tc[i].r-tc[j].r))<=0){
                ok=false;
                break;
            }
        }
        if(ok) c[n++]=tc[i];
    }
    double ans=0;

    for(int i=0;i<n;i++){
        vector<Node>event;
        point boundary=c[i].p+point(-c[i].r,0);
        event.push_back(Node(boundary,-pi,0));
        event.push_back(Node(boundary,pi,0));
        for(int j=0;j<n;++j){
            if(i==j) continue;
            double d=(c[i].p-c[j].p).norm();
            if(dcmp(d-(c[i].r+c[j].r))<0){
                pair<point,point> ret=crosspoint(c[i],c[j]);
                double x=arg(ret.first-c[i].p);
                double y=arg(ret.second-c[i].p);
                if(dcmp(x-y)>0){
                    event.push_back(Node(ret.first,x,1));
                    event.push_back(Node(boundary,pi,-1));
                    event.push_back(Node(boundary,-pi,1));
                    event.push_back(Node(ret.second,y,-1));
                }
                else{
                    event.push_back(Node(ret.first,x,1));
                    event.push_back(Node(ret.second,y,-1));
                }
            }
        }
        sort(event.begin(),event.end());
        int sum=event[0].d;
        for(int j=1;j<(int)event.size();++j){
            if(sum==0){
                ans+=cross(event[j-1].p,event[j].p)/2;
                double x=event[j-1].a;
                double y=event[j].a;
                double area=c[i].r*c[i].r*(y-x)/2;
                point v1=event[j-1].p-c[i].p;
                point v2=event[j].p-c[i].p;
                area-=cross(v1,v2)/2;
                ans+=area;
            }
            sum+=event[j].d;
        }
    }
    return ans;
}
int main(){

    int t;
    scanf("%d",&t);
    while(t--){
        double n;
        Circle a,b,c;
        scanf("%lf",&n);
        a.p.x=(n/2);
        a.p.y=n/2;
        a.r=n/2;

        b.p.x=n;
        b.p.y=0;
        b.r=n;

        c.p.x=0;
        c.p.y=n;
        c.r=n;
        double temp=2*pi*(n/2)*(n/2)-area(a,b)-area(a,c);
        printf("%.2f\n",temp);
    }
    return 0;
}
ACM 很全的计算几何模板 基础部分 1.几何公式 5 1.1三角形 5 1.2四边形 5 1.3正n边形 5 1.4 5 1.5棱柱 6 1.6棱锥 6 1.7棱台 6 1.8柱 6 1.9锥 6 1.10台 7 1.11球 7 1.12球台 7 1.13球扇形 7 2.直线与线段 7 2.0预备函数 7 2.1判三点是否共线 8 2.2判点是否在线段上 9 2.3判断两点在线段的同一侧 9 2.4判断两点是否在线段的异侧 9 2.5点关于直线的对称点 10 2.7判断两线段是否相 10 2.7.1常用版 10 2.7.2不常用版 11 2.8 两条直线的点 11 2.9点到直线的最近距离 12 2.10点到线段的最近距离 12 3.多边形 12 3.0 预备浮点函数 12 3.1判定是否是凸多边形 13 3.2判定点是否在多边形内 14 3.3 判定一条线段是否在一个任意多边形内 15 4. 三角形 16 4.0预备函数 16 4.1三角形的外心 17 4.2三角形内心 17 4.3三角形垂心 17 5. 18 5.0预备函数 18 5.1判定直线是否与 19 5.2判定线段与 19 5.3判 19 5.4计算上到点p最近点 19 5.5计算直线与点 20 5.6计算两个点 20 6. 球面 21 6.0给出地球经度纬度,计算心角 21 6.1已知经纬度,计算地球上两点直线距离 21 6.2已知经纬度,计算地球上两点球面距离 21 7. 三维几何的若干模板 22 7.0预备函数 22 7.1判定三点是否共线 23 7.2判定四点是否共面 23 7.1判定点是否在线段上 23 7.2判断点是否在空间三角形上 24 7.3判断两点是否在线段同侧 24 7.4判断两点是否在线段异侧 25 7.5判断两点是否在平面同侧 25 7.6判断两点是否在平面异侧 25 7.7判断两空间直线是否平行 25 7.8判断两平面是否平行 26 7.9判断直线是否与平面平行 26 7.10判断两直线是否垂直 26 7.11判断两平面是否垂直 26 7.12判断两条空间线段是否相 27 7.13判断线段是否与空间三角形相 27 7.14计算两条直线的点 28 7.15计算直线与平面的点 28 7.16计算两平面的线 29 7.17点到直线的距离 29 7.18 计算点到平面的距离 29 7.19计算直线到直线的距离 30 7.20空间两直线夹角的cos值 30 7.21两平面夹角的cos值 30 7.22直线与平面夹角sin值 31 1.最远曼哈顿距离 31 2. 最近点对 32 3. 最近点对 34 4. 最小包围 36 5. 两个点 39 6. 三角形外接心 40 7. 凸包 42 8.凸包卡壳旋转出所有对踵点、最远点对 44 9. 凸包+旋转卡壳平面面积最大三角 47 10. Pick定理 50 11. 多边形面积和重心 51 12. 判断一个简单多边形是否有核 52 13. 模拟退火 54 14. 六边形坐标系 56 15. 用一个给定半径的覆盖最多的点 60 16. 不等大的弧表示 62 17. 矩形面积并 62 18. 矩形的周长并 66 19. 最近对 70 20. 两个面积 74
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值