【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 n n 个点构成一个多边形,又给出 m m m 个点,询问这些点是否在多边形内

题解:射线法

从这个点做一个平行于 x 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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值