C - How I Mathematician Wonder What You Are!解题报告(来自网络)

来源:互联网 发布:阿里云优惠口令看不懂 编辑:程序博客网 时间:2024/04/27 19:05

C - How I Mathematician Wonder What You Are!
Time Limit:5000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit Status Practice POJ 3130

Description

After counting so many stars in the sky in his childhood, Isaac, now an astronomer and a mathematician uses a big astronomical telescope and lets his image processing program count stars. The hardest part of the program is to judge if shining object in the sky is really a star. As a mathematician, the only way he knows is to apply a mathematical definition of stars.

The mathematical definition of a star shape is as follows: A planar shape F is star-shaped if and only if there is a point C ∈ F such that, for any point P ∈F, the line segment CP is contained in F. Such a point C is called a center of F. To get accustomed to the definition let’s see some examples below.

The first two are what you would normally call stars. According to the above definition, however, all shapes in the first row are star-shaped. The two in the second row are not. For each star shape, a center is indicated with a dot. Note that a star shape in general has infinitely many centers. Fore Example, for the third quadrangular shape, all points in it are centers.

Your job is to write a program that tells whether a given polygonal shape is star-shaped or not.

Input

The input is a sequence of datasets followed by a line containing a single zero. Each dataset specifies a polygon, and is formatted as follows.

n x1y1x2y2

xnyn

The first line is the number of vertices, n, which satisfies 4 ≤ n ≤ 50. Subsequent n lines are the x- and y-coordinates of the n vertices. They are integers and satisfy 0 ≤ xi ≤ 10000 and 0 ≤ yi ≤ 10000 (i = 1, …, n). Line segments (xiyi)–(xi + 1yi + 1) (i = 1, …, n − 1) and the line segment (xnyn)–(x1y1) form the border of the polygon in the counterclockwise order. That is, these line segments see the inside of the polygon in the left of their directions.

You may assume that the polygon is simple, that is, its border never crosses or touches itself. You may assume assume that no three edges of the polygon meet at a single point even when they are infinitely extended.

Output

For each dataset, output “1” if the polygon is star-shaped and “0” otherwise. Each number must be in a separate line and the line should not contain any other characters.

Sample Input

6 66 13 96 61 76 98 13 94 4 0 45 68 8 27 21 55 14 93 12 56 95 15 48 38 46 51 65 64 31 0

Sample Output

10

 对于半平面交的一些简明扼要的介绍可以参考这篇博客:http://blog.csdn.net/accry/article/details/6070621。此外,这篇博客上介绍的还有我敲出的程序都只是比较好理解的O(n^2)的求半平面交的算法,对于O(nlogn)的算法可以参考朱泽园的论文。

    由于这个题目指明了多边形上的点是按逆时针序给出的,因而就不用再将每组数据都其统一成某个顺序了。

#include<stdio.h>#include<string.h>#define MAXD 110#define zero 1e-8#define INF 100000struct point{    double x, y;}p[MAXD], wa[MAXD], wb[MAXD], *a, *b;int N, an, bn;double fabs(double x){    return x < 0 ? -x : x;}int dcmp(double x){    return fabs(x) < zero ? 0 : (x < 0 ? -1 : 1);}double det(double x1, double y1, double x2, double y2){    return x1 * y2 - x2 * y1;}void init(){    int i, j, k;    for(i = 0; i < N; i ++)        scanf("%lf%lf", &p[i].x, &p[i].y);    p[N] = p[0];}void add(double x, double y){    b[bn].x = x, b[bn].y = y;    ++ bn;}void cut(int k){    int i, j, tn;    point *t;    double x, y, t1, t2;    bn = 0;    for(i = 0; i < an; i ++)    {        t1 = det(p[k + 1].x - p[k].x, p[k + 1].y - p[k].y, a[i].x - p[k].x, a[i].y - p[k].y);        t2 = det(p[k + 1].x - p[k].x, p[k + 1].y - p[k].y, a[i + 1].x - p[k].x, a[i + 1].y - p[k].y);        if(dcmp(t1) >= 0)            add(a[i].x, a[i].y);        if(dcmp(t1) * dcmp(t2) < 0)        {            x = (fabs(t2) * a[i].x + fabs(t1) * a[i + 1].x) / (fabs(t1) + fabs(t2));            y = (fabs(t2) * a[i].y + fabs(t1) * a[i + 1].y) / (fabs(t1) + fabs(t2));            add(x, y);        }    }    t = a, a = b, b = t;    tn = an, an = bn, bn = tn;    a[an] = a[0];}void solve(){    int i, j, k;    a = wa, b = wb;    an = 4;    a[0].x = -INF, a[0].y = -INF, a[1].x = INF, a[1].y = -INF, a[2].x = INF, a[2].y = INF, a[3].x = -INF, a[3].y = INF;    a[4] = a[0];    for(i = 0; i < N; i ++)        cut(i);    if(an == 0)        printf("0\n");    else        printf("1\n");}int main(){    for(;;)    {        scanf("%d", &N);        if(!N)            break;        init();        solve();    }    return 0;}





原创粉丝点击