A Pilot in Danger! FZU - 1120(判断点在多边形内)

判断点是否在多边形内

#include 
#include 
#include 

using namespace std;

const int maxn=100;
const double eps=1e-8;

int sgn(double x)
{
    if(fabs(x)=min(l2.s.x,l2.e.x)&&
    max(l2.s.x,l2.e.x)>=min(l1.s.x,l1.e.x)&&
    max(l1.s.y,l1.e.y)>=min(l2.s.y,l2.e.y)&&
    max(l2.s.y,l2.e.y)>=min(l1.s.y,l2.s.y)&&
    sgn((l2.s-l1.s)^(l2.e-l1.s))*sgn((l2.s-l1.e)^(l2.e-l1.e))<=0&&
    sgn((l1.s-l2.s)^(l1.e-l2.s))*sgn((l1.s-l2.e)^(l1.e-l2.e))<=0;
}
int inPoly(Point p,Point poly[],int n)
{
    int cnt;
    Line ray,side;
    cnt=0;
    ray.s=p;
    ray.e.y=p.y;
    ray.e.x=-100000000.0;
    for(int i=0;i0)
                cnt++;
        }
        else if(OnSeg(side.e,ray))
        {
            if(sgn(side.e.y-side.s.y)>0)
                cnt++;
        }
        else if(inter(ray,side))
            cnt++;
    }
    if(cnt%2==1)
        return 1;
    else
        return -1;
}

int main()
{
    int n;
    int sum=1;
    while(1)
    {
        cin>>n;
        if(n==0)
            break;
        Point sp=Point(0,0);
        for(int i=0;i>x1>>y1;
            p[i]=Point(x1,y1);
        }
        int a1,a2;
        cin>>a1>>a2;
        printf("Pilot %d\n",sum);
        sum++;
        if(inPoly(sp,p,n)==1)
        {
            printf("The pilot is in danger!\n");
            int ans;
            ans=(a1-1)*(a2-1)/2;
            printf("The secret number is %d.\n",ans);
        }
        else
        {
            printf("The pilot is safe.\n");
        }
        cout<

 

你可能感兴趣的:(计算几何)