判断点在多边形内部 - 射线法
Statement of the Problem
Several drawing applications allow us to draw polygons and almost all of them allow us to fill them with some color. The task of filling a polygon reduces to knowing which points are inside it, so programmers have to colour only those points.
You're expected to write a program which tells us if a given point lies inside a given polygon described by the coordinates of its vertices. You can assume that if a point is in the border of the polygon, then it is in fact inside the polygon.
Input Format
The input file may contain several instances of the problem. Each instance consists of: (i) one line containing integers N, 0 < N < 100 and M, respectively the number of vertices of the polygon and the number of points to be tested. (ii) N lines, each containing a pair of integers describing the coordinates of the polygon's vertices; (iii) M lines, each containing a pair of integer coordinates of the points which will be tested for "withinness" in the polygon.
You may assume that: the vertices are all distinct;
consecutive vertices in the input are adjacent in the polygon; the last
vertex is adjacent to the first one; and the resulting polygon is
simple, that is, every vertex is incident with exactly two edges and two
edges only intersect at their common endpoint. The last instance is
followed by a line with a 0 (zero).
Output Format
For the ith instance in the input, you have to write one line
in the output with the phrase "Problem i:", followed by several lines,
one for each point tested, in the order they appear in the input. Each
of these lines should read "Within" or "Outside", depending on the
outcome of the test. The output of two consecutive instances should be
separated by a blank line.
Sample Input
3 1
0 0
0 5
5 0
10 2
3 2
4 4
3 1
1 2
1 3
2 2
0
Sample Output
Problem 1:
Outside
Problem 2:
Outside
Within
题意 : 给你一个 n 边型的点,在给你 m 个要判断的点,每次判断所输入的点是否在多边形的内部,边界上的点也属于内部
思路 : 射线法,从所要判断的点引一条射线,射线与边的会有一个入和出的关系,若点在多边形外,则 入 = 出
代码示例:
int n, m; struct point { double x, y; point(double _x=0, double _y=0):x(_x), y(_y){} point operator - (const point &v){ return point(x-v.x, y-v.y); } }; typedef point Vector; 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 Cross(Vector a, Vector b) {return a.x*b.y-a.y*b.x;} double Dot(Vector a, Vector b){return a.x*b.x+a.y*b.y;} bool OneInter(point p, point a1, point a2){ return dcmp(Cross(a1-p, a2-p))==0 && dcmp(Dot(a1-p, a2-p))<0; } point p[105]; bool check(point po){ int wn = 0; p[n] = p[0]; for(int i = 0; i < n; i++){ if (OneInter(po, p[i], p[i+1]) || po==p[i]) return 1; int k = dcmp(Cross(p[i+1]-p[i], po-p[i])); int d1 = dcmp(p[i].y-po.y); int d2 = dcmp(p[i+1].y-po.y); if (k>0 && d1 <= 0 && d2>0) wn++; if (k<0 && d2 <= 0 && d1>0) wn--; } return wn != 0; } int main() { //freopen("in.txt", "r", stdin); //freopen("out.txt", "w", stdout); int kase = 1; while(~scanf("%d", &n) && n){ scanf("%d", &m); if (kase > 1) printf("\n"); printf("Problem %d:\n", kase++); for(int i = 0; i < n; i++){ scanf("%lf%lf", &p[i].x, &p[i].y); } double x, y; for(int i = 1; i <= m; i++){ scanf("%lf%lf", &x, &y); if (check(point(x, y))) printf("Within\n"); else printf("Outside\n"); } } return 0; }