POJ1279 && LA2512 Art Gallery(求多边形的核)

题意:给出一个多边形,求多边形的核。


思路:

多边形的核:在多边形中可见多边形的所有边界的一篇区域称为多边形的核。

显然多边形的核是由多边形的每条边切割成的半平面交。

由于N只有1500.直接上N^2的算法搞,水水的。


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <ctime>
#include <bitset>
#include <algorithm>
#include <set>
#include <string>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <deque>
using namespace std;

#define LL long long


const double eps = 1e-8;
const double pi = acos(-1.0);

int cmp(double x) {
    if(fabs(x) < eps) return 0;
    if(x > 0)return 1;
    return -1;
}

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 bool operator < (const point &a, const point& b) {
        return cmp(a.x - b.x) < 0 || 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.y * b.x;
}

double dot(const point &a, const point &b) {
    return a.x * b.x + a.y * b.y;
}

double dist(const point &a, const point &b) {
    return (a - b).norm();
}

double Angle(point a, point b) {
    if(cmp(dot(a, b) - a.norm() * b.norm()) == 0) return 0;
    if(cmp(dot(a, b) + a.norm() * b.norm()) == 0) return pi;
    return acos(dot(a,b) / a.norm() / b.norm()); 
}

double angle(point p) {
    return atan2(p.y, p.x);
}

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));
}

point rotate_point(const point &p, double sint, double cost) {
    double tx = p.x, ty = p.y;
    return point(tx * cost - ty * sint, tx * sint + ty * cost);
}

struct line {
    point a, b;
    line(){}
    line(point x, point y):a(x),b(y){}
    void input() {
        a.input();
        b.input();
    }
};



void point_pro_line(const point p, const point s, const point t, point &cp) {  
    double r = dot(t - s, p - s) / dot (t - s, t - s);
    cp = s + r * (t - s);
}

bool point_pro_segment(const point p, const point s, const point t, point &cp) {
    if(cmp(dot(p - s, t - s))<0) {
        cp = s;
        return 0;
    }
    if(cmp(dot(p - t, s - t))<0) {
        cp = t;
        return 0;
    }

    double r = dot(t - s, p - s) / dot (t - s, t - s);
    cp = s + r * (t - s);
    return 1;
}

bool point_on_segment(point p, point s, point t) {
    return cmp(det(p - s, t - s))== 0 && cmp(dot(p - s, p - t)) < 0;
}

bool parallel(line a, line b) {
    return !cmp(det(a.a - a.b, b.a - b.b));
}

bool line_cross_line(line a, line b, point &res){
    if(parallel(a, b)) return false;
    double s1 = det(a.a - b.a, b.b - b.a);
    double s2 = det(a.b - b.a, b.b - b.a);
    res = (s1 * a.b - s2 * a.a) / (s1 - s2);
    return true;
}

struct polygon {
    vector<point> P;

    polygon(int size = 0) {
        P.resize(size);
    }
    point& operator [](int index) {
        return P[index];
    }
    int size() {
        return P.size();
    }    

    double area() {
        int n = size();
        double sum = 0;
        for(int i = 0; i < n; i++) sum += det(P[i], P[(i + 1) % n]);
        return sum * 0.5;
    }

};


polygon cut_polygon(polygon& pol, point& a, point& b) {
    polygon ret;
    point c, d, p;
    int n = pol.size();
    if (n <= 2) return pol;
    for(int i = 0; i < n; i++) {
        c = pol[i];
        d = pol[(i + 1) % n];
        if(cmp(det(b - a, c - a)) >= 0) ret.P.push_back(c);
        if(line_cross_line(line(a, b), line(c, d), p)) {
            if(point_on_segment(p, c, d)) ret.P.push_back(p);
        }     
    }
    return ret;
}

point a[2222];
int n;
int main() {
    int T;
    scanf("%d", &T);
    while(T--) {
        scanf("%d", &n);
        polygon pol(n);
        for(int i = 0; i < n; i++) pol[i].input();
        if(pol.area() <= 0) {
            for(int i = 0; i < n; i++) a[i] = pol[i];
            for(int i = 0; i < n; i++) pol[i] = a[n - i - 1];
        }
        polygon hp = pol;
        for(int i = 0; i < n; i++) {
            hp = cut_polygon(hp, pol[i], pol[(i + 1) % n]);
        }
        printf("%.2f\n", fabs(hp.area()));
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值