天天看點

poj 3130 How I Mathematician Wonder What You Are!(半平面交)

How I Mathematician Wonder What You Are!

Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 2962 Accepted: 1581

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 ofF. To get accustomed to the definition let’s see some examples below.

poj 3130 How I Mathematician Wonder What You Are!(半平面交)

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
x1 y1
x2 y2
xn yn

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 (xi,yi)–(xi + 1, yi + 1) (i = 1, …, n − 1) and the line segment (xn, yn)–(x1, y1) 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 “

” 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

1
0      

Source

Japan 2006

題意:等價于問多邊形是否有核 題解:n*n的半平面交即可解之,是否存在多邊形核問題

#include<stdio.h>
#define eps 1e-8
struct point{
    double x,y;
}p[58],tp[88],temp[188];
int n,m;
double a,b,c;
double cross(struct point p1,struct point p2,struct point p3)
{
    return (p1.x-p3.x)*(p2.y-p3.y)-(p1.y-p3.y)*(p2.x-p3.x);
}
void getline(struct point p1,struct point p2)
{
    a=p1.y-p2.y;
    b=p2.x-p1.x;
    c=p1.x*p2.y-p1.y*p2.x;
}
void getinter(struct point p1,struct point p2)
{
    double a2=p1.y-p2.y;
    double b2=p2.x-p1.x;
    double c2=p1.x*p2.y-p1.y*p2.x;
    double tmd=a*b2-a2*b;
    temp[183].x=(b*c2-b2*c)/tmd;
    temp[183].y=(a2*c-a*c2)/tmd;
}
void make_clockwise()
{
    int i;
    double area=0;

    for(i=2;i<=n;i++) area+=cross(p[i],p[i+1],p[1]);
    if(area>0)
    {
        for(i=0;i<=n+1-i;i++)
        {
            p[55]=p[i];
            p[i]=p[n+1-i];
            p[n+1-i]=p[55];
        }
    }
}
void input()
{
    int i;

    for(i=1;i<=n;i++) scanf("%lf%lf",&p[i].x,&p[i].y);
    p[0]=p[n],p[n+1]=p[1];
    make_clockwise();
    for(i=0;i<=n+1;i++) temp[i]=p[i];
    m=n;
}
void cut()
{
    int i,cou=0;

    for(i=1;i<=m;i++)
    {
        if(a*temp[i].x+b*temp[i].y+c<=0) tp[++cou]=temp[i];
        else
        {
            if(a*temp[i-1].x+b*temp[i-1].y+c<0)
            {
                getinter(temp[i-1],temp[i]);
                tp[++cou]=temp[183];
            }
            if(a*temp[i+1].x+b*temp[i+1].y+c<0)
            {
                getinter(temp[i+1],temp[i]);
                tp[++cou]=temp[183];
            }
        }
    }
    tp[0]=tp[cou],tp[cou+1]=tp[1];
    for(i=0;i<=cou+1;i++) temp[i]=tp[i];
    m=cou;
}
void solve()
{
    int i;

    for(i=1;i<=n;i++)
    {
        getline(p[i],p[i+1]);
        cut();
    }
    if(m>0) printf("1\n");
    else printf("0\n");
}
int main()
{
    //freopen("t.txt","r",stdin);
    while(scanf("%d",&n),n)
    {
        input();
        solve();
    }
}