暑假集训#1 B题

B - B

Crawling in process...Crawling failed  Time Limit:500MS    Memory Limit:4096KB     64bit IO Format:%I64d & %I64u

 

Description

There is a closed broken line on a plane with sides parallel to coordinate axes, without self-crossings and self-contacts. The broken line consists of K segments. You have to determine, whether a given point with coordinates (X0,Y0) is inside this closed broken line, outside or belongs to the broken line.

Input

The first line contains integer K (4ЈK Ј 10000) - the number of broken line segments.  Each of the following N lines contains coordinates of the beginning and end points of the segments (4 integerxi1,yi1,xi2,yi2all numbers in a range from -10000 up to 10000 inclusive). Number separate by a space.  The segments are given in random order. Last line contains 2 integersX0 and Y0- the coordinates of the given point delimited by a space. (NumbersX0, Y0in a range from  -10000 up to 10000 inclusive).

Output

The first line should contain:

INSIDE - if the point is inside closed broken line,

OUTSIDE - if the point is outside,

BORDER - if the point belongs to broken line.

Sample Input

4
0 0 0 3
3 3 3 0
0 3 3 3
3 0 0 0
2 2

Sample Output

INSIDE

这是个计算几何问题,给出了很多的折线形成了一个封闭的图形,然后给出个点判断点是在多边形内或者外或者在多边形上。

方法是从给出的点出发做射线,检查边与射线的交点个数,如果是奇数个点的话就说明在多边形内,如果是偶数则是在多边形外,若检查出该点在边上就说明在多边形的边上。

对题目给出的各个边进行遍历,检查线段始末点的坐标和给出的坐标之间的关系确定焦点个数。对了要注意的一个问题就是线段的端点一端是属于线段,一端是属于线段外的。

#include<iostream>
#include<stdio.h>
#define INF 100100
#define MAX 100001
using namespace std;
struct Segment{
	int x1,y1,x2,y2;
}sgm[MAX];
int main()
{
	int n;
	while(cin>>n)
	{
		for(int i=0;i<n;i++)
		{
		     int t1,t2,t3,t4;
		     cin>>t1>>t2>>t3>>t4;
		     if(t2<=t4)
		     {
		     	sgm[i].x1=t1;
				sgm[i].y1=t2;
				sgm[i].x2=t3;
				sgm[i].y2=t4;
		     }
		     else
		     {
		     	sgm[i].x1=t3;
				sgm[i].y1=t4;
				sgm[i].x2=t1;
				sgm[i].y2=t2;
		     }
		}
		int count=0;
		int x,y;
		cin>>x>>y;
		bool boarder=false;
		for(int i=0;i<n;i++)
		{
			if(sgm[i].x1==sgm[i].x2&&sgm[i].x1>x&&y>=sgm[i].y1&&y<sgm[i].y2)
			{
				count++;
			}
		}
		for(int i=0;i<n;i++)
		{
			if((sgm[i].x1==sgm[i].x2&&sgm[i].x1==x)&&(y>=sgm[i].y1&&y<=sgm[i].y2))
			{
				boarder=true;
			}
			if(sgm[i].y1==sgm[i].y2&&sgm[i].y1==y)
			{
				if(sgm[i].x1<sgm[i].x2&&x>=sgm[i].x1&&x<=sgm[i].x2)
				 boarder=true;
				if(sgm[i].x1>sgm[i].x2&&x>=sgm[i].x2&&x<=sgm[i].x1)
				 boarder=true;
			}
		} 
		if(boarder)
		{
			cout<<"BORDER"<<endl;
		}
		else if(count%2!=0)
		{
			cout<<"INSIDE"<<endl;
		}
		else cout<<"OUTSIDE"<<endl;
	}
}

posted @ 2013-07-28 00:38  湖心北斗  阅读(153)  评论(0编辑  收藏  举报