UVA634 Polygon点与多边形

Posted 海岛Blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA634 Polygon点与多边形相关的知识,希望对你有一定的参考价值。

Modern graphic computer programs can, among other, even more stunning capabilities, fill a closed region. Though not all of them can protect the user from accidentally choosing to fill the background rather than the inner part. Besides being a channel hopper at home your boss’ favourite hobby is colouring the pictures, you cannot protest long about adding this magnificent protection feature to his graphic program.
    This means that your job is to write a program, which determines whether a point belong to a polygon, given the array of its vertices.
    To make life a bit simpler you may assume that:
• all edges of the polygon are vertical or horizontal segments
• lengths of all the edges of the polygon are even integer numbers
• co-ordinates of at least one vertex are odd integer numbers
• both co-ordinates of any vortex cannot be divisible by 7 at the same time
• the investigated point P has both co-ordinates being even integer numbers
• the polygon has at most 1000 vertices
• co-ordinates of the vertices lay in the range: -10000…10000.
Input
Input data may consist of several data sets, each beginning with a number of polygon’s vertices (n). Consecutive n lines contain co-ordinates of the vertices (x followed by y). Then go the co-ordinates of investigated point P. Input data end when you find 0 the number of polygon’s vertices.
Output
For each polygon and each point P you should print one character (in separate lines): ‘T’ when P belongs to the polygon or ‘F’ otherwise.
Sample Input
4
1 1
1 3
3 3
3 1
2 2
12
1 1
1 9
3 9
3 5
5 5
5 9
7 9
7 1
5 1
5 3
3 3
3 1
4 2
0
Sample Output
T
F

问题链接UVA634 Polygon
问题简述:给定n个顶点的多边形,再给定一个点,判定点是否在多边形内?
问题分析:判定一个点是否在多边形内,模板题。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA634 Polygon */

#include <bits/stdc++.h>

using namespace std;

const int N = 1000;
struct Point {
    double x, y;
} p[N], q;

int main()
{
    int n;
    while (~scanf("%d", &n) && n) {
        for (int i = 0; i < n; i++)
            scanf("%lf%lf", &p[i].x, &p[i].y);
        scanf("%lf%lf", &q.x, &q.y);

        int cnt = 0;
        for (int i = 0, j = n - 1; i < n; j = i++) {
            if((p[i].y > q.y) != (p[j].y > q.y) &&
               q.x < (p[j].x - p[i].x) * (q.y - p[i].y) / (p[j].y - p[i].y) + p[i].x)
            cnt++;
        }

        puts(cnt & 1 ? "T" : "F");
    }

    return 0;
}

以上是关于UVA634 Polygon点与多边形的主要内容,如果未能解决你的问题,请参考以下文章

uva 11971 Polygon

如何将 boost::polygon 中的多边形集数据转换为多边形数据?

确定给定点是不是在多边形内

polygon mndeling汉语啥意思

R语言polygon函数绘制多边形实战

hdu_6055 : Regular polygon (2017 多校第二场 1011) 计算几何