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;
}