1. 程式人生 > >【ZOJ 1081】Points Within

【ZOJ 1081】Points Within

【題目】

傳送門

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


【分析】

大致題意:(多組資料)按順序給出 n n 個點,這 n n 個點構成一個多邊形,又給出 m m 個點,詢問這些點是否在多邊形內

題解:射線法

從這個點做一個平行於 x x 軸的平行線,記錄一下與多邊形的交點個數,若為偶數則是在外面,奇數則在裡面

但是對於射線法,有很多很多的細節,比如說下圖:
在這裡插入圖片描述
交點只有一個,但是計算的時候肯定是列舉每一條邊來算有沒有交點,這樣的話這個點會被算兩次,就錯了

所以說我們就要強制規定,經過一條邊的左端點(或右端點)才能被計算

還有比較多的細節這裡就不列舉了,不過一般都不會卡吧,除非那種毒瘤題

【程式碼】

#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 105
#define eps 1e-8
using namespace std;
int n,m;
struct point
{
	int x,y;
	point(){}
	point(int x,int y):x(x),y(y){}
	point operator+(const point &a)  {return point(x+a.x,y+a.y);}
	point operator-(const point &a)  {return point(x-a.x,y-a.y);}
	point operator*(const int &a)  {return point(x*a,y*a);}
	point operator/(const int &a)  {return point(x/a,y/a);}
	friend int dot(const point &a,const point &b)  {return a.x*b.x+a.y*b.y;}
	friend int cross(const point &a,const point &b)  {return a.x*b.y-a.y*b.x;}
}a[N],p;
bool check(point a,point b,point c)
{
	int x=cross(a-c,b-c);
	if(x!=0)  return false;
	x=dot(a-c,b-c);
	return x<=0;
}
bool inside(const point &p)
{
	int i,num=0;
	for(i=1;i<=n;++i)
	{
		if(check(a[i],a[i+1],p))  return true;
		int d1=a[i].y-p.y,d2=a[i+1].y-p.y;
		int x=cross(a[i]-p,a[i+1]-p);
		if((x>=0&&d1<0&&d2>=0)||(x<=0&&d1>=0&&d2<0))  num++;
	}
	return num&1;
}
int main()
{
	int i,cas=0;
	while(~scanf("%d",&n))
	{
		if(!n)  break;
		scanf("%d",&m);
		for(i=1;i<=n;++i)
		  scanf("%d%d",&a[i].x,&a[i].y);
		a[n+1]=a[1];
		if(cas!=0)  printf("\n");
		printf("Problem %d:\n",++cas);
		for(i=1;i<=m;++i)
		{
			scanf("%d%d",&p.x,&p.y);
			if(inside(p))  printf("Within\n");
			else  printf("Outside\n");
		}
	}
	return 0;
}