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
Sample Output
题意:给你一些平行于坐标轴的线段,这些线段连成一个多边形,让你判断一个点是在这个多边形上,多边形内还是多边形外。
因为给的边是乱序的,所以不能用叉积判断。
所以用射线法,以要求的点往一个方向作射线(当然不能过多边形的端点),判断与多边形的几条边相交,如果是奇数条,那么就是在多边形内,否则就是在多边形外。
判断在不在多边形上就不说了。
当然这题还有个坑就是这个多边形的边有可能是有多条线段组成的,你作的射线如果穿过两条平行的且相连的两条线段,就有可能会算成两条,实际上只能算一条,所以我们判断的时候把这些线段当成左闭右开的就好了。
(这题里面的线段都是平行于坐标轴的,所以就不用叉积判断相交了)
#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;
}
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;
}
相关文章推荐
- 判断一个点是否在多边形内部 - 射线法Scala实现
- 判断一个点是否在多边形内部 [1] 射线法思路
- [射线法]判断一个点是否在多边形内部
- 判断一个点是否在多边形内部 [2] 射线法实现
- 判断一个点是否在多边形内部 - 射线法思路
- 判断一个点是否在多边形内部 [2] 射线法实现
- 判断一个点是否在多边形内部,射线法思路,C#实现
- javascript 判断一个点是否在多边形内(向量的使用)
- 射线法判断点是否在多边形内的关键代码
- 如何判断一个点是否在一个多边形内部(有向面积)
- POJ 1584: A Round Peg in a Ground Hole 射线法判断点是否在多边形内 点与直线距离 判断多边形是否为凸多边形
- 判断一个点是否在多边形内C#
- Android 百度地图 动态画多边形,并判断一个点是否在多边形内部
- 判断一个点是否在多边形内
- 判断一个点是否在一个多边形里
- 判断一个坐标点是否在一个无规则的多边形内 (iOS定位服务与地图应用开发:高德地图开发)
- HDU 1756 判断一个点是否在多边形内
- 判断点是否在多边形内(包括在多边形上)的一个算法
- 判断一个点是否在一个复杂的多边形中
- 点在多边形内算法——判断一个点是否在一个复杂多边形的内部