E - Sleepwalking
Description
All of you know about "Alauddin vai". But what you don't know is, he sometimes walks while sleeping. Yes, some kind of sleep walking.
Now one day he was sleeping in a strange room. But after waking up he doubts that whether he is inside the room or not.
You may assume that the room can be modeled as a polygon whose coordinates are 2D integer points. Alauddin vai can be thought as a 2D integer point. Now you are given the configuration of the room and the position of Alauddin vai (after waking up). You have to decide whether he is still in the room or not. Consider him inside if he is on the boundary of the polygon.
Input
Input starts with an integer T (≤ 10), denoting the number of test cases.
Each case starts with an integer n (3 ≤ n ≤ 100) denoting the number of vertices of the room. The next line contains 2n integers, where the ith pairxi yi denote the co-ordinate of the ith vertex of the room. The vertices will be given in anticlockwise order. The next line contains an integer q (1 ≤ q ≤ 300) denoting the number of queries. Each of the next q lines contains two integers x y denoting the co-ordinate of Alauddin vai. The absolute value of the given co-ordinates will be less than 10001.
Output
For each case, print the case number in a single line. Then print q lines, containing the answer for the queries as given in input. Print 'Yes' if he is inside the room, print 'No' otherwise.
Sample Input
2
3
0 0 10 0 0 20
2
5 5
10 10
3
0 0 3 3 0 3
1
5 5
Sample Output
Case 1:
Yes
No
Case 2:
No
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#define eps 1e-8
#define inf 1e20
using namespace std;
struct point{
double x,y;
}A[105],p;
struct line{
point a,b;
}B[2];
int N;
double MIN(double a,double b){
return a<b?a:b;
}
double MAX(double a,double b){
return a>b?a:b;
}
double cp(point p1,point p2,point p3){
return (p3.y-p1.y)*(p2.x-p1.x)-(p3.x-p1.x)*(p2.y-p1.y);
}
bool judge1(point p,line l){//判断点在直线上
if(fabs(cp(p,l.a,l.b))<=eps&&MIN(l.a.x,l.b.x)<=p.x&&p.x<=MAX(l.a.x,l.b.x)&&MIN(l.a.y,l.b.y)<=p.y&&p.y<=MAX(l.a.y,l.b.y))
return true;
return false;
}
bool judgeinter(int a,int b){//判断线段相交
if(MIN(B[a].a.x,B[a].b.x)>MAX(B[b].a.x,B[b].b.x)||MIN(B[a].a.y,B[a].b.y)>MAX(B[b].a.y,B[b].b.y)||MIN(B[b].a.x,B[b].b.x)>MAX(B[a].a.x,B[a].b.x)||MIN(B[b].a.y,B[b].b.y)>MAX(B[a].a.y,B[a].b.y))
return false;
double h,i,j,k;
h=(B[a].b.x-B[a].a.x)*(B[b].a.y-B[a].a.y)-(B[a].b.y-B[a].a.y)*(B[b].a.x-B[a].a.x);
i=(B[a].b.x-B[a].a.x)*(B[b].b.y-B[a].a.y)-(B[a].b.y-B[a].a.y)*(B[b].b.x-B[a].a.x);
j=(B[b].b.x-B[b].a.x)*(B[a].a.y-B[b].a.y)-(B[b].b.y-B[b].a.y)*(B[a].a.x-B[b].a.x);
k=(B[b].b.x-B[b].a.x)*(B[a].b.y-B[b].a.y)-(B[b].b.y-B[b].a.y)*(B[a].b.x-B[b].a.x);
return h*i<=eps&&j*k<=eps;
}
bool judge(){
B[0].a=p;B[0].b.x=inf;B[0].b.y=p.y;
int ans=0;
for(int i=0;i<N;++i){
B[1].a=A[i];
B[1].b=A[(i+1)%N];
if(judge1(p,B[1]))
return true;
if(fabs(B[1].a.y-B[1].b.y)<eps)
continue;
if(judge1(B[1].a,B[0])){
if(B[1].a.y>B[1].b.y)
ans++;
}
else if(judge1(B[1].b,B[0])){
if(B[1].b.y>B[1].a.y)
ans++;
}
else if(judgeinter(0,1))
ans++;
}
if(ans&1)
return true;
return false;
}
int main()
{
int t,i,j,k=1,m;
scanf("%d",&t);
while(t--){
scanf("%d",&N);
for(i=0;i<N;++i){
scanf("%lf%lf",&A[i].x,&A[i].y);
}
printf("Case %d:\n",k++);
scanf("%d",&m);
while(m--){
scanf("%lf%lf",&p.x,&p.y);
if(judge())
printf("Yes\n");
else
printf("No\n");
}
}
return 0;
}