POJ 3335||Rotating Scoreboard(半平面交求核

新抄一个模板,再学习。

#include <math.h>
#include <stdio.h>
#include <iostream>
#include <algorithm>
#define eps 1e-8
using namespace std;

const int maxn = 55;
int dq[maxn], lst, fir, pn, order[maxn], ln;
struct Point
{
    double x, y;
    Point(double xx=0.0,double yy=0.0):x(xx),y(yy){}
    double operator ^ (const Point &b)const{
        return x*b.y - y*b.x;
    }
    Point operator - (const Point &b)const{
        return Point(x-b.x,y-b.y);
    }

} p[maxn];
struct Line
{
    Point a, b;
    double angle;
    Line(){}
    Line( Point a,Point b)
    {
        this->a = a;
        this->b = b;
        angle = atan2( b.y-a.y,b.x-a.x);
    }
} l[maxn];
int dcmp(double x)
{
    if(fabs(x) < eps)
        return 0;
    return x > 0 ? 1 : -1;
}
double cross(Point p0, Point p1, Point p2)
{
    return (p1-p0)^(p2-p0);
}
bool cmp(int u, int v)
{
    double d = dcmp(l[u].angle-l[v].angle);
    if(dcmp(d)==0)
        return dcmp(cross(l[u].a, l[v].a, l[v].b)) < 0;
    //大于0取向量左半部分为半平面,小于0,取右半部分
    return d < 0;
}
void get_line_intersect(Line l1, Line l2, Point& p)
{
    double cp1, cp2;
    cp1 = cross(l2.a, l1.b, l1.a);
    cp2 = cross(l1.b, l2.b, l1.a);
    p.x = (l2.a.x * cp2 + l2.b.x * cp1) / (cp2 + cp1);
    p.y = (l2.a.y * cp2 + l2.b.y * cp1) / (cp2 + cp1);
}
bool judge(Line l0, Line l1, Line l2)
{
    Point p;
    get_line_intersect(l1, l2, p);
    return dcmp(cross(p, l0.a, l0.b)) > 0;
    //大于小于符号与上面cmp()中注释处相反
}
void addLine(Point a,Point b)
{
    l[ln] = Line(a,b);
    order[ln] = ln;
    ln++;
}
void half_plane_inrer_sect()
{
    int i, j;
    sort(order, order+ln, cmp);
    for(i = 1, j = 0; i < ln; i++)
        if(dcmp(l[order[i]].angle-l[order[j]].angle) > 0)
            order[++j] = order[i];
    ln = j + 1;
    dq[0] = order[0];
    dq[1] = order[1];
    fir = 0;
    lst = 1;
    for(i = 2; i < ln; i++)
    {
        while(fir < lst && judge(l[order[i]], l[dq[lst-1]], l[dq[lst]]))
            lst--;
        while(fir < lst && judge(l[order[i]], l[dq[fir+1]], l[dq[fir]]))
            fir++;
        dq[++lst] = order[i];
    }
    while(fir < lst && judge(l[dq[fir]], l[dq[lst-1]], l[dq[lst]]))
        lst--;
    while(fir < lst && judge(l[dq[lst]], l[dq[fir+1]], l[dq[fir]]))
        fir++;
}
bool is_view()
{
    if(lst-fir > 1)
        return true;
    return false;
}
int main()
{
    int i,T;
    scanf("%d",&T);
    while( T--)
    {
        scanf ("%d", &pn);

        scanf ("%lf%lf", &p[0].x, &p[0].y);
        ln = 0;
        for(i = 1; i < pn; i++)
        {
            scanf ("%lf%lf", &p[i].x, &p[i].y);
            addLine(p[i-1], p[i]);
        }
        addLine(p[pn-1],p[0]);
        half_plane_inrer_sect();
        if(is_view()) printf ("YES\n");
        else printf ("NO\n");
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值