zoj1081 Points Within

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

题目大意:给你一堆点组成的多边形,再给另外一些点,问每个点是否在多边形内.

分析:判断点是否在多边形内常用的方法是射线法.即从要判断的点以x轴正方向做一条射线,如果它与多边形边的交点数量为奇数,则在多边形内,否则在多边形外.需要特判的是点是否在一条线段上,同时还要规定如果射线恰好交到了多边形的一个点上,这个点是边的上端点才统计还是下端点才统计.

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

int tt,n,m;

struct node
{
    int x,y;
}e[110],q;

int dot(node a,node b)
{
    return a.x * b.x + a.y * b.y;
}

int det(node a,node b)
{
    return a.x * b.y - a.y * b.x;
}

node sub(node a,node b)
{
    node temp;
    temp.x = a.x - b.x;
    temp.y = a.y - b.y;
    return temp;
}

bool check2()
{
    int res = 0;
    for (int i = 1; i <= n; i++)
        res += det(e[i],e[i + 1]);
    return res < 0;
}

void init()
{
    for (int i = 1; i <= n; i++)
        scanf("%d%d",&e[i].x,&e[i].y);
    e[n + 1] = e[1];
    if (check2())   //如果不是按照逆时针,就翻转
        reverse(e + 1,e + 1 + n);
    e[n + 1] = e[1];
}

bool inner(node a,node b,node c)
{
    node u = sub(a,c),v = sub(b,c);
    if (det(u,v) != 0)   //如果叉乘不为0,则不在ab直线上
        return false;
    if (dot(u,v) <= 0) //如果点乘≤0,则在ab线段上
        return true;
    return false;
}

bool check(node q)
{
    int cnt = 0;
    for (int i = 1; i <= n; i++)
    {
        if (inner(e[i],e[i + 1],q))  //判断点是否在线段上
            return true;
        int d1 = e[i].y - q.y,d2 = e[i + 1].y - q.y;
        int temp = det(sub(e[i],q),sub(e[i + 1],q));   //叉乘判断方向
        if ((temp >= 0 && d2 >= 0 && d1 < 0) || (temp <= 0 && d1 >= 0 && d2 < 0))  // =是规定只有上端点被统计进入答案.
            cnt++;
    }
    if (cnt & 1)
        return true;
    return false;
}

int main()
{
    for (tt = 1;;tt++)
    {
        scanf("%d",&n);
        if (n == 0)
            break;
        scanf("%d",&m);
        init();
        if (tt != 1)
            printf("
");
        printf("Problem %d:
",tt);
        while (m--)
        {
            scanf("%d%d",&q.x,&q.y);
            if (check(q))
                puts("Within");
            else
                puts("Outside");
        }
    }

    return 0;
}
原文地址:https://www.cnblogs.com/zbtrs/p/8097464.html