多边形面积以及圆和多边形相交的区域的面积

//来自陈鑫大佬
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
 
const double eps = 1e-8;
const double inf = 1e20;
 
int sgn(double x){
    if(fabs(x) < eps) return 0;
    if(x < 0) return -1;
    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);
    }
    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;
    }
    Point operator * (const double &k) const{
        return Point(x * k, y * k);
    }
    Point operator / (const double &k) const{
        return Point(x / k, y / k);
    }
    double len(){
        return hypot(x, y);
    }
    double len2(){
        return x * x + y * y;
    }
    double distance(Point p){
        return hypot(x - p.x, y - p.y);
    }
    double rad(Point a, Point b){
        Point p = *this;
        return fabs(atan2( fabs((a - p) ^ (b - p)), (a - p) * (b - p) ));
    }
    Point trunc(double r){
        double l = len();
        if(!sgn(l)) return *this;
        r /= l;
        return Point(x * r, y * r);
    }
};
 
struct Line{
    Point s, e;
    Line(){}
    Line(Point _s, Point _e){
        s = _s;
        e = _e;
    }
    double length(){
        return s.distance(e);
    }
    double dispointtoline(Point p){
        return fabs((p - s) ^ (e - s)) / length();
    }
    Point lineprog(Point p){
        return s + ( ((e - s) * ((e - s) * (p - s))) / ((e - s).len2()) );
    }
};
 
struct circle{
    Point p;
    double r;
    circle(){}
    circle(Point _p, double _r){
        p = _p;
        r = _r;
    }
    circle(double x, double y, double _r){
        p = Point(x, y);
        r = _r;
    }
    int relation(Point b){
        double dst = b.distance(p);
        if(sgn(dst - r) < 0) return 2;
        if(sgn(dst - r) == 0) return 1;
        return 0;
    }
    int relationline(Line v){
        double dst = v.dispointtoline(p);
        if(sgn(dst - r) < 0) return 2;
        if(sgn(dst - r) == 0) return 1;
        return 0;
    }
    int pointcrossline(Line v, Point &p1, Point &p2){
        if(!(*this).relationline(v)) return 0;
        Point a =v.lineprog(p);
        double d = v.dispointtoline(p);
        d = sqrt(r * r - d * d);
        if(sgn(d) == 0){
            p1 = a;
            p2 = a;
            return 1;
        }
        p1 = a + (v.e - v.s).trunc(d);
        p2 = a - (v.e - v.s).trunc(d);
        return 2;
    }
    double areatriangle(Point a, Point b){
        if(sgn((p - a) ^ (p - b)) == 0) return 0.0;
        Point q[5];
        int len = 0;
        q[len ++] = a;
        Line l(a, b);
        if(pointcrossline(l, q[1], q[2]) == 2){
            if(sgn((a - q[1]) * (b - q[1])) < 0) q[len ++] = q[1];
            if(sgn((a - q[2]) * (b - q[2])) < 0) q[len ++] = q[2];
        }
        q[len ++] = b;
        if(len == 4 && sgn((q[0] - q[1]) * (q[2] - q[1])) > 0)
            swap(q[1], q[2]);
        double res = 0;
        for(int i = 0; i < len - 1; i ++){
            if(relation(q[i]) == 0 || relation(q[i + 1]) == 0){
                double arg = p.rad(q[i], q[i + 1]);
                res += r * r * arg / 2.0;
            }
            else{
                res += fabs((q[i] - p) ^ (q[i + 1] - p)) / 2.0;
            }
        }
        return res;
    }
};
 
double getarea(int n, Point *p){
    double sum = 0;
    for(int i = 0; i < n; i ++){
        sum += (p[i] ^ p[(i + 1) % n]);
    }
    return fabs(sum) * 0.5;
}
 
double areacircle(circle c, int n, Point *p){
    double ans = 0;
    for(int i = 0; i < n; i ++){
        int j = (i + 1) % n;
        if(sgn((p[j] - c.p) ^ (p[i] - c.p)) >= 0)
            ans += c.areatriangle(p[i], p[j]);
        else
            ans -= c.areatriangle(p[i], p[j]);
    }
    return fabs(ans);
}
 
int n, m;
double x, y, P, Q;
Point a[205];
 
int main()
{
    scanf("%d", &n);
    for(int i = 0; i < n; i ++){
        scanf("%lf%lf", &x, &y);
        a[i] = Point(x, y);
    }
    double tot = getarea(n, a);
    scanf("%d", &m);
    while(m --){
        scanf("%lf%lf%lf%lf", &x, &y, &P, &Q);
        double l = 0, r = 1e4;
        while(l + eps < r){
            double mid = (l + r) * 0.5;
            circle cr = circle(x, y, mid);
            if(areacircle(cr, n, a) < tot * (1.0 - P / Q)) l = mid;
            else r = mid;
        }
        printf("%.12f\n", l);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值