Hgc Dot In Closed Area

Description

Tigers are enclosed in wire-net fencing at zoo.If they get out of the fencing, they may attack people.We use some lines to present the fencing as the picture following:

Your task is to judge if a tiger in the fencing or not.
In picture 1 the tiger is in the fencing.
Note that enclosed fencing may look like picture 2 where the tiger is not in the fencing.

Input

The first line of input is a integer t(1<=t<=100),the number of test cases.The first line of each test case contains a integer n(1<=n<=100), the number of vertexes of the fencing.The second line contains the position of the tiger followed by n vertexes of the fencing in counter-closewise order.All numbers are integers in interval[0,10000].

Output

Print "yes" if the tiger is in the fencing or at the border,print "no" if not.

Sample Input

1
5
100 76
55 118
119 114
147 66
123 33
68 56

Sample Output

yes

code:

View Code
#include<stdio.h>
#include<string.h>
const double eps=1e-10;
double abs(double x)
{
if(x<0)return -1.0*x;
return x;
}
double max(double a,double b)
{
return a>b?a:b;
}
double min(double a,double b)
{
return a<b?a:b;
}
struct point
{
double x,y;
}q[10000];
double mult(point p1,point p2,point p0)
{
return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
int inter(point a,point b,point c,point d)
{
return ((max(a.x,b.x)>=min(c.x,d.x)&&
max(c.x,d.x)>=min(a.x,b.x)&&
max(a.y,b.y)>=min(c.y,d.y)&&
max(c.y,d.y)>=min(a.y,b.y)&&
mult(c,a,b)*mult(a,d,a)>=0&&
mult(a,d,c)*mult(d,b,c)>=0));
}
int online(point a,point b,point c)
{
if(abs(mult(b,a,c))<=eps&&min(b.x,c.x)<=a.x&&a.x<=max(b.x,c.x)
&&min(b.y,c.y)<=a.y&&a.y<=max(b.y,c.y))
return 1;
return 0;
}
int main()
{
int n,i,count,flag;
point a,b;
while(scanf("%d",&n),n)
{
flag=0;
scanf("%lf%lf",&a.x,&a.y);
b.x=999999; b.y=a.y;
for(i=0;i<n;i++)
scanf("%lf%lf",&q[i].x,&q[i].y);
for(count=i=0;i<n;i++)
{
if(online(a,q[i],q[(i+1)/n]))
{ flag=1;break;}
if(abs(q[i].y-q[(i+1)%n].y<eps))
continue;
else if(online(q[(i+1)%n],a,b)&&q[(i+1)%n].y>q[i].y)
count++;
else if(online(q[i],a,b)&&q[i].y>q[(i+1)%n].y)
count++;
else if(inter(a,b,q[i],q[(i+1)%n]))
count++;
}
if(count&1) flag=1;
if(flag)printf("Yes\n");
else printf("No\n");
}
return 0;
}

 

posted @ 2012-03-15 00:29  'wind  阅读(223)  评论(0编辑  收藏  举报