HDU 1756 Cupid‘s Arrow(转角法)

 

转角法判断点在多边形内部模板题 

const int N=1e3+5;

    int i,j,k;
    int n,m,t;
    struct Point
    {
        double x,y;
        Point(double x=0,double y=0):x(x),y(y){}
    }a[N],p;
typedef Point Vector ;
Vector operator-(Vector a,Vector b){ return Vector(a.x-b.x,a.y-b.y); }
double Cross(Vector a,Vector b){ return a.x*b.y-a.y*b.x; }
double Dot(Vector a,Vector b){ return a.x*b.x+a.y*b.y; }

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

bool point_on_segment(Point p,Point a,Point b)
{
    return sgn(Cross(a-p,b-p))==0 && sgn(Dot(p-a,p-b))<0;    
}

bool point_in_polygon(Point p)
{
    int wn=0;
    for(int i = 0; i<n ; i++){
        if(point_on_segment(p,a[i],a[(i+1)%n])) return true;
        int k=sgn(Cross(a[(i+1)%n]-a[i],p-a[i]));
        int d1=sgn(a[i].y-p.y);
        int d2=sgn(a[(i+1)%n].y-p.y);
        if(k>0 && d1<=0 && d2>0) wn++;
        if(k<0 && d2<=0 && d1>0) wn--;
    }
    if(wn) return true;
    return false;
}

int main()
{
    //IOS;
    while(~sd(n)){
        for(int i=0;i<n;i++) sff(a[i].x,a[i].y);
        sd(m);
        while(m--){
            sff(p.x,p.y);
            if( point_in_polygon(p) ) puts("Yes");
            else puts("No");
        }
    }
    //PAUSE;
    return 0;
}

 

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 酷酷鲨 设计师:CSDN官方博客 返回首页