判断点在多边形内部 - 射线法

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

 

转载于:https://www.cnblogs.com/ccut-ry/p/8987891.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值