天天看點

poj3130How I Mathematician Wonder What You Are!+半平面交

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   
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 “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

Sample Output

1

Source

輸入一個簡單多邊形,判斷是否存在核。

直接求半平面交的面積,不為0則是存在的。

#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<queue>
#include<deque>
#include<map>
#include<stack>
#include<set>
#include<complex>
#define e exp(1.0); //2.718281828
#define mod 1000000007
#define INF 0x7fffffff
#define inf 0x3f3f3f3f
typedef long long LL;
using namespace std;

#define zero(x) (((x)>0?(x):(-x))<eps)
const double eps=;
const double pi=acos(-);

//判斷數k的符号 -1負數 1正數 0零
int dcmp(double k) {
    return k<-eps?-:k>eps?:;
}

inline double sqr(double x) {
    return x*x;
}
struct point {
    double x,y;
    point() {};
    point(double a,double b):x(a),y(b) {};
    void input() {
        scanf("%lf %lf",&x,&y);
    }
    friend point operator + (const point &a,const point &b) {
        return point(a.x+b.x,a.y+b.y);
    }
    friend point operator - (const point &a,const point &b) {
        return point(a.x-b.x,a.y-b.y);
    }
    friend bool operator == (const point &a,const point &b) {
        return dcmp(a.x-b.x)==&&dcmp(a.y-b.y)==;
    }
    friend bool operator < (const point &a, const point &b) {
        return a.x < b.x || (a.x == b.x && a.y < b.y);
    }
    friend point operator * (const point &a,const double &b) {
        return point(a.x*b,a.y*b);
    }
    friend point operator * (const double &a,const point &b) {
        return point(a*b.x,a*b.y);
    }
    double norm() {
        return sqrt(sqr(x)+sqr(y));
    }
};
//計算兩個向量的叉積
double cross(const point &a,const point &b) {
    return a.x*b.y-a.y*b.x;
}
double cross3(point A,point B,point C) { //叉乘
    return (B.x-A.x)*(C.y-A.y)-(B.y-A.y)*(C.x-A.x);
}
//計算兩個點的點積
double dot(const point &a,const point &b) {
    return a.x*b.x+a.y*b.y;
}
double dot3(point A,point B,point C) { //點乘
    return (C.x-A.x)*(B.x-A.x)+(C.y-A.y)*(B.y-A.y);
}

//求n邊形的面積,多邊形上的點要按逆時針的順序存儲在p中
double ConvexPolygonArea(point *p, int n) {
    double area = ;
    for(int i=; i<n-; ++i)
        area += cross(p[i] - p[], p[i+] - p[]);
    return area / ;
}

//typedef complex<double> point;
//typedef pair<point,point> halfplane;

struct halfplane{
    point a,b;
    halfplane(){};
    halfplane(point a,point b):a(a),b(b){};
};

inline double satisfy(point a,halfplane p){
    return dcmp(cross(a-p.a,p.b-p.a))<=;
}
point crosspoint(const halfplane &a,const halfplane &b){
    double k=cross(b.a-b.b,a.a-b.b);
    k=k/(k-cross(b.a-b.b,a.b-b.b));
    return a.a+(a.b-a.a)*(k);
}
double arg(point p){
    return arg(complex<double>(p.x,p.y));
}
bool cmp(const halfplane &a,const halfplane &b){
    int res=dcmp(arg(a.b-a.a)-arg(b.b-b.a));
    return res==? satisfy(a.a,b):res<;
}
int halfplaneIntersection(halfplane *v,int n,point *out){
    //sort(v.begin(),v.end(),cmp);
    sort(v,v+n,cmp);
    deque<halfplane> q;
    deque<point> ans;
    q.push_back(v[]);
    for(int i=;i<n;++i){
        if(dcmp(arg(v[i].b-v[i].a)-arg(v[i-].b-v[i-].a))==){
            continue;
        }
        while(ans.size()>&&!satisfy(ans.back(),v[i])){
            ans.pop_back();
            q.pop_back();
        }
        while(ans.size()>&&!satisfy(ans.front(),v[i])){
            ans.pop_front();
            q.pop_front();
        }
        ans.push_back(crosspoint(q.back(),v[i]));
        q.push_back(v[i]);
    }
    while(ans.size()>&&!satisfy(ans.back(),q.front())){
        ans.pop_back();
        q.pop_back();
    }
    while(ans.size()>&&!satisfy(ans.front(),q.back())){
        ans.pop_front();
        q.pop_front();
    }
    ans.push_back(crosspoint(q.back(),q.front()));
    int m=;
    while(ans.empty()==false){
        out[m++]=ans.front();
        ans.pop_front();
    }
    return m;
}
halfplane v[];
point out[];
point a[],b[];

int main(){
    int n1,n2;
    int cas=;
    while(scanf("%d",&n1)!=EOF&&n1){
        for(int i=;i<n1;i++) scanf("%lf %lf",&a[i].x,&a[i].y);
        a[n1]=a[];
        int n=;
        for(int i=;i<n1;i++){
            v[n++]=halfplane(a[i],a[i+]);
        }

        int m=halfplaneIntersection(v,n,out);
        if(ConvexPolygonArea(out,m)>eps) printf("1\n");
        else printf("0\n");
    }
    return ;
}