Points Within
Time Limit: 2 Seconds Memory Limit: 65536 KB
Statement of the Problem
Several drawing applications allow us to draw polygons and almost all of them allow us to fill them with some color. The task of filling a polygon reduces to knowing which points are inside it, so programmers have to colour only those points.
You're expected to write a program which tells us if a given point lies inside a given polygon described by the coordinates of its vertices. You can assume that if a point is in the border of the polygon, then it is in fact inside the polygon.
Input Format
The input file may contain several instances of the problem. Each instance consists of: (i) one line containing integers N, 0 < N < 100 and M, respectively the number of vertices of the polygon and the number of points to be tested. (ii) N lines, each containing a pair of integers describing the coordinates of the polygon's vertices; (iii) M lines, each containing a pair of integer coordinates of the points which will be tested for "withinness" in the polygon.
You may assume that: the vertices are all distinct; consecutive vertices in the input are adjacent in the polygon; the last vertex is adjacent to the first one; and the resulting polygon is simple, that is, every vertex is incident with exactly two edges and two edges only intersect at their common endpoint. The last instance is followed by a line with a 0 (zero).
Output Format
For the ith instance in the input, you have to write one line in the output with the phrase "Problem i:", followed by several lines, one for each point tested, in the order they appear in the input. Each of these lines should read "Within" or "Outside", depending on the outcome of the test. The output of two consecutive instances should be separated by a blank line.
Sample Input
3 1
0 0
0 5
5 0
10 2
3 2
4 4
3 1
1 2
1 3
2 2
0
Sample Output
Problem 1:
Outside
Problem 2:
Outside
Within
解析:
计算几何:判断点是否在多边形内。
常用方法为射线法,但考虑得特殊情况较多,这里介绍一种角度判别法,即求出这个点与多边形所有顶点得连线的夹角和,若为360‘则在多边形内,只需要特判在边上的情况。
代码:
#include <bits/stdc++.h>
using namespace std;
const double pi=acos(-1);
const int Max=105;
int t,n,m;
struct point{
double x,y;
point(){}
point(const double &_x,const double &_y):x(_x),y(_y){}
friend point operator +(const point &a,const point &b){return point(a.x+b.x,a.y+b.y);}
friend point operator -(const point &a,const point &b){return point(a.x-b.x,a.y-b.y);}
friend double operator *(const point &a,const point &b){return a.x*b.y-a.y*b.x;}
friend double dot(const point &a,const point &b){return a.x*b.x+a.y*b.y;}
friend double ang(const point &a,const point &b){return acos(dot(a,b)/sqrt(dot(a,a))/sqrt(dot(b,b)));}
}p[Max];
inline int get_int()
{
int x=0,f=1;
char c;
for(c=getchar();(!isdigit(c))&&(c!='-');c=getchar());
if(c=='-') f=-1,c=getchar();
for(;isdigit(c);c=getchar()) x=(x<<3)+(x<<1)+c-'0';
return x*f;
}
inline void init()
{
for(int i=1;i<=n;i++) p[i].x=get_int(),p[i].y=get_int();
}
inline bool seg(const point &a,const point &b,const point &q)
{
if((a-q)*(b-q)!=0) return 0;
return dot(a-q,b-q)<=0;
}
inline bool check(const point &s)
{
double rad=0.0;
for(int i=1;i<=n;i++)
{
if(seg(p[i],p[i%n+1],s)) return 1;
double du=fabs(ang(p[i]-s,p[i%n+1]-s));
if((p[i]-s)*(p[i%n+1]-s)>0) rad+=du;
else rad-=du;
}
return fabs(fabs(rad)-pi*2)<1e-6;
}
inline void solve()
{
printf("Problem %d:\n",t);
while(m--)
{
point s;
s.x=get_int(),s.y=get_int();
if(check(s)) puts("Within");
else puts("Outside");
}
}
int main()
{
while(1)
{
t++;
n=get_int();
if(!n) break;
m=get_int();
init();
if(t!=1) putchar('\n');
solve();
}
return 0;
}