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 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, 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 <stdio.h> #include <string.h> #include <algorithm> #include <math.h> #include <sstream> #include <vector> #include <stack> #include <map> #include <queue> #include <set> #include <iostream> #include <stdlib.h> #include <cctype> #define rep(i,s,e) for(int i = (s);i<=(e);++i) #define maxn 4100 #define INF 1000000000 #define LL long long using namespace std; struct Point { double x,y; Point(double _x = 0,double _y = 0):x(_x),y(_y){} }; typedef Point Vector; Point read_point() { double x,y; scanf("%lf%lf",&x,&y); return Point(x,y); } Vector operator +(Vector A, Vector B) { return Vector(A.x+B.x, A.y+B.y); } Vector operator -(Vector a, Vector b) { return Vector(a.x-b.x,a.y-b.y); } Vector operator *(Vector a, double b) { return Vector(a.x*b,a.y*b); } Vector operator /(Vector a, double b) { return Vector(a.x/b,a.y/b); } bool operator < (const Point &a,const Point &b) { return a.x<b.x||(a.x==b.x&&a.y<b.y); } const double eps= 1e-20; int dcmp(double x) { if(fabs(x)<eps) { return 0; } else { return x>0?1:-1; } } bool operator == (const Point &a,const Point &b) { return dcmp(a.x-b.x)==0&&dcmp(a.y-b.y)==0; } double Dot(Vector a,Vector b)//向量点乘 { return a.x*b.x+a.y*b.y; } double Cross(Vector a, Vector b)//向量差乘,判断是在左边还是右边 { return a.x*b.y-a.y*b.x; } bool OnSegment(Point p, Point a, Point b)//判断是否点在线上 { return dcmp(Cross(a-p,b-p))==0&&dcmp(Dot(a-p,b-p))<0; } int IsPointInPolygon(const Point& p, const vector<Point>& poly)//判定主函数 { int wn = 0; int n = poly.size(); for(int i = 0; i < n; i+=2){ const Point& p1 = poly[i]; const Point& p2 = poly[(i+1)%n]; if(p1 == p || p2 == p || OnSegment(p, p1, p2)) return -1; // 在边界上 int k = dcmp(Cross(p2-p1, p-p1)); int d1 = dcmp(p1.y - p.y); int d2 = dcmp(p2.y - p.y); if(k > 0 && d1 <= 0 && d2 > 0) wn++; if(k < 0 && d2 <= 0 && d1 > 0) wn++; } if (wn%2!= 0) return 1; // 内部 else return 0; // 外部 } vector<Point> p; int main() { int n; while(scanf("%d",&n)==1) { p.clear(); for(int i=0;i<2*n;i++) { p.push_back(read_point()); } Point po=read_point(); int ans=IsPointInPolygon(po, p); if(ans==1) cout<<"INSIDE"<<endl; else if(ans==-1) cout<<"BORDER"<<endl; else cout<<"OUTSIDE"<<endl; } return 0; }