Points Within

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=81
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

YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY
#include<stdio.h>
#include<math.h>
struct point 
{
	double x,y;
};
point p[120];
int m,n;
double MIN(double x,double y)
{
	return x<y?x:y;
}
double MAX(double x,double y)
{
	return x>y?x:y;
}
int xdy(double x,double y)
{
	return x>y;
}
int xxy(double x,double y)
{
	return x<y;
}int xddy(double x,double y)
{
	return x>=y;
}
int xxdy(double x,double y)
{
	return x<=y;
}
int xdddy(double x,double y)
{
	return fabs(x-y)<1e-6;
}
double cross(point a,point b,point c)
{
	return (c.x-a.x)*(b.y-a.y)-(b.x-a.x)*(c.y-a.y);
}
int onsegement(point a,point b,point c)
{
	double d1=MIN(a.x,b.x);
	double d2=MAX(a.x,b.x);
	double d3=MIN(a.y,b.y);
	double d4=MAX(a.y,b.y);
	if(d1<=c.x&&c.x<=d2&&d3<=c.y&&c.y<=d4)
	return 1;
	return 0;
}
int segment(point a,point b,point c,point d)
{
	double d1=cross(c,d,a);
	double d2=cross(c,d,b);
	double d3=cross(a,b,c);
	double d4=cross(a,b,d);
	if(d1*d2<0&&d3*d4<0)
	return 1;
	if(d1==0&&onsegement(c,d,a))
	return 1;
	if(d2==0&&onsegement(c,d,b))
	return 1;
	if(d3==0&&onsegement(a,b,c))
	return 1;
	if(d1==0&&onsegement(a,b,d))
	return 1;
	return 0; 
}
int inploy(point a)
{
	point b;
	b.x=1e10,b.y=a.y+50;
	int i,j,k,l,tmp,cout=0;
	for(i=0;i<m;i++)
	{
		if(cross(p[i],p[(i+1)%m],a)==0&&onsegement(p[i],p[(i+1)%m],a))
		return 1;
		//printf("cout==%d\n",cout);
		tmp=-1;
		if(cross(a,b,p[i])==0&&onsegement(a,b,p[i]))
		tmp=i;
		else if(cross(a,b,p[(i+1)%m])==0&&onsegement(a,b,p[(i+1)%m]))
		tmp=(i+1)%m;
		if(tmp!=-1&&xdddy(p[tmp].y,MAX(p[i].y,p[(i+1)%m].y)))
		cout++;
		else if(tmp==-1&&segment(p[i],p[(i+1)%m],a,b))
		cout++;
	}
	if(cout%2==0)
	return 0;
	else 
	return 1;
}
int main()
{
	int i,j,k,ind=1;
	while(scanf("%d",&m),m)
	{
		scanf("%d",&n);
		if(ind!=1)
		printf("\n");
		printf("Problem %d:\n",ind++);
		for(i=0;i<m;i++)
		scanf("%lf%lf",&p[i].x,&p[i].y);
		while(n--)
		{
			point a;
			scanf("%lf%lf",&a.x,&a.y);
			if(inploy(a))
			printf("Within\n");
			else
			printf("Outside\n");
		}
		
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值