Rotating Scoreboard POJ - 3335 (半平面交)

Rotating Scoreboard POJ - 3335
半平面交裸题:
亮模板:

#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
const int MAXN=1e3+5;
const double EPS=1e-8;
const double PI=acos(-1);
inline int sgn(double a){ return a < -EPS ? -1 : a > EPS; }
inline int cmp(double a, double b){ return sgn(a-b); }
long long mod=1e9+7;
int n,m,T;
struct Point;
struct Line;
typedef Point Vector;

struct Point{
    double x,y;
    Point(){}
    Point(double a, double b):x(a),y(b){}
    double len(){return sqrt(x*x+y*y);}
    void read(){scanf("%lf%lf",&x,&y);}
    Point operator+(Vector v){return {x+v.x,y+v.y};}
    Vector operator-(Point p){return {x-p.x,y-p.y};}
    double operator^(Vector v){return x*v.y-y*v.x;}//叉乘
    double operator*(Vector v){return x*v.x+y*v.y;}//点乘
    Vector operator*(double d){return {x*d,y*d};}
    Vector operator/(double d){return {x/d,y/d};}
    bool operator==(Point p){return cmp(x,p.x)==0&&cmp(y,p.y)==0;}
    bool operator<(Point p){if(cmp(x,p.x)==0) return y<p.y;return x<p.x;}
};
struct Line{
    Line(){}
    Line(Point a,Point b){s=a;e=b;ang=atan2(e.y-s.y,e.x-s.x);}
    Point operator &(Line &b){
        double t=((s-b.s)^(b.s-b.e))/((s-e)^(b.s-b.e));
        return s+(e-s)*t;
    }
    Point s,e;
    double ang;
};

bool HPIcmp(Line a,Line b) {
    if (fabs(a.ang - b.ang) > EPS)return a.ang < b.ang;
    else return ((a.s - b.s) ^ (b.e - b.s)) < 0;
}
struct halfplanes{
    int n;
    Line line[MAXN];
    Point p[MAXN];
    Line Q[MAXN];
    int head,tail;
    void read(){
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            p[i].read();
    }
    void connect(){
        for(int i=1;i<=n;i++)
            line[i]={p[i],p[i%n+1]};
    }
    bool HPI() {
        sort(line+1, line+n+1, HPIcmp);
        int tot = 1;
        for (int i = 2; i <= n; i++)
            if (fabs(line[i].ang - line[i - 1].ang) > EPS) //去掉斜率重复的
                line[++tot] = line[i];
        head = 1, tail = 2;
        Q[1] = line[1];Q[2] = line[2];
        for (int i = 3; i <= tot; i++) {
            if (fabs((Q[tail].e - Q[tail].s) ^ (Q[tail - 1].e - Q[tail - 1].s)) < EPS ||
                fabs((Q[head].e - Q[head].s) ^ (Q[head + 1].e - Q[head + 1].s)) < EPS)
                return false;
            while (head < tail && (((Q[tail] & Q[tail - 1]) -
                                    line[i].s) ^ (line[i].e - line[i].s)) > EPS)
                tail--;
            while (head < tail && (((Q[head] & Q[head + 1]) -
                                    line[i].s) ^ (line[i].e - line[i].s)) > EPS)
                head++;
            Q[++tail] = line[i];
        }
        while (head < tail && (((Q[tail] & Q[tail - 1]) -
                                Q[head].s) ^ (Q[head].e - Q[head].s)) > EPS)
            tail--;
        while (head < tail && (((Q[head] & Q[head + 1]) -
                                Q[tail].s) ^ (Q[tail].e - Q[tail].s)) > EPS)
            head++;
        if (tail <= head + 1) return false;
        return true;
    }
    void getConvex(Point res[], int &resn){//得到核上的点
        resn=0;
        for(int i = head; i < tail; i++)
            res[++resn] = Q[i]&Q[i+1];
        if(head < tail - 1)
            res[++resn] = Q[head]&Q[tail];
    }
}hpi;
bool judge(Point p[],int n) {
    double ans = 0;
    for (int i = 1; i <= n; i++) {
        ans += (p[i] ^ p[i % n + 1]);
    }
    return ans<0;
}
void solve(){
    if(hpi.HPI()) printf("YES\n");
    else printf("NO\n");
}
void init(){
    hpi.read();
    if(judge(hpi.p,hpi.n))//判断点的输入顺序,顺时针要反转
        reverse(hpi.p+1,hpi.p+hpi.n+1);
    hpi.connect();
}
int main(){
    scanf("%d",&T);
    while(T--){
        init();
        solve();
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值