您的位置:首页 > 其它

SGU 124. Broken line(射线法判断一个点是否在一个多边形内)

2017-10-09 21:03 465 查看
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 integer xi1,yi1,xi2,yi2; all
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 integers X0 and Y0 - the coordinates
of the given point delimited by a space. (Numbers X0, Y0 in 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<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define LL long long

using namespace std;

int x[10010],y[10010],xx[10010],yy[10010];

int main(void)
{
int n,i,j;
while(scanf("%d",&n)==1)
{
for(i=1;i<=n;i++)
{
scanf("%d%d%d%d",&x[i],&y[i],&xx[i],&yy[i]);
if(x[i] > xx[i])
swap(x[i],xx[i]);
if(y[i] > yy[i])
swap(y[i],yy[i]);
}
int tx,ty;
scanf("%d%d",&tx,&ty);
int flag = 0;
for(i=1;i<=n;i++)
{
if(x[i] == xx[i] && x[i] == tx && y[i] <= ty && ty <= yy[i])
flag = 1;
if(y[i] == yy[i] && y[i] == ty && x[i] <= tx && tx <= xx[i])
flag = 1;
}
if(flag == 1)
printf("BORDER\n");
else
{
int cnt = 0;
for(i=1;i<=n;i++)
{
if(x[i] == xx[i])
continue;
if(x[i] <= tx && tx < xx[i] && y[i] > ty)
cnt++;
}
if(cnt % 2 == 0)
printf("OUTSIDE\n");
else
printf("INSIDE\n");
}
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: