poj 3130 How I Mathematician Wonder What You Are!

How I Mathematician Wonder What You Are!
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 1988   Accepted: 1077

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

1
0
题意:
给出若干多边形,判断这些多边形是否存在核
分析:
使用半平面交判断
 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
const int N = 105;
//顺时针方向给出点
struct point
{
    double x, y;
}ps[N], des[N], kernel[N];
int pn;
double MaxDist;//点和点之间的最大距离,作为延伸线段用
//计算多边形的有向面积,按这种方法,顺时针为负
double getArea(point* ps, int pn)
{
    double ans = 0;
    int i;
    ps[pn] = ps[0];
    for (i = 0; i < pn; i++) ans += (ps[i].x * ps[i + 1].y - ps[i].y * ps[i + 1].x);
    return abs(ans) / 2.0;
}
void revPs(point* ps, int pn)         //把ps中的点反序
{
    int l, r;
    l = 0, r = pn - 1;
    point tmp;
    while (l < r)
    {
        tmp = ps[l];
        ps[l] = ps[r];
        ps[r] = tmp;
        l++, r--;
    }
}
inline double dist(point& p1, point& p2)//求的是点p1和点p2的距离
{
    return sqrt(pow(p1.x - p2.x, 2.0) + pow(p1.y - p2.y, 2.0));
}
void extLine(point& st, point& ed, double l)//延伸线段,l为延伸的长度
{
    point tst = st, ted = ed;
    double d = dist(tst, ted);
    double dx, dy;
    dx = (ted.x - tst.x) * l / d;
    dy = (ted.y - tst.y) * l / d;
    ed.x = dx + tst.x;
    ed.y = dy + tst.y;
    dx = -dx;
    dy = -dy;
    st.x = dx + ted.x;
    st.y = dy + ted.y;
}
inline double xmul(point st1, point ed1, point st2, point ed2)//差积定理
{
    return (ed1.x - st1.x) * (ed2.y - st2.y) - (ed1.y - st1.y) * (ed2.x - st2.x);
}
point interPoint(point st1, point ed1, point st2, point ed2)  //得到内核
{
    double eds = fabs(xmul(st1, ed1, st1, ed2));
    double sts = fabs(xmul(st1, ed1, st1, st2));
    point dd;
    dd.x = (eds * st2.x + sts * ed2.x) / (eds + sts);
    dd.y = (eds * st2.y + sts * ed2.y) / (eds + sts);
    return dd;
}
//计算org在由st指向ed的线段的左侧的部分,一般来说st和ed要延伸
void semiPlane(point* org, int on, point* des, int& dn, point st, point ed)
{
    dn = 0;
    extLine(st, ed, MaxDist);
    org[on] = org[0];
    int i, j;
    double ps, pe;
    for (j = 0; j < on; j++)
    {
        ps = xmul(st, ed, st, org[j]);
        pe = xmul(st, ed, st, org[j + 1]);
        if (ps >= 0) des[dn++] = org[j];
        if (ps * pe < 0)
        {
            des[dn++] = interPoint(st, ed, org[j], org[j + 1]);
        }
    }
}
//获得org的内核,放在kernel里
void getKernel(point* org, int on, point* kernel, int& kn)
{
    if (getArea(org, on) < 0)
    {
        revPs(org, on);
    }
    org[on] = org[0];
    int i, dn, j;
    MaxDist = 0;
    for (i = 0; i < on; i++)
    {
        for (j = i + 1; j < on; j++) MaxDist = max(MaxDist, dist(org[i], org[j]));
    }
    for (i = 0; i < on; i++) kernel[i] = org[i];
    kn = on;
    for (i = 0; i < on; i++)
    {
        semiPlane(kernel, kn, des, dn, org[i], org[i + 1]);
        for (j = 0; j < dn; j++) kernel[j] = des[j];
        kn = dn;
    }
}
int main()
{
    int i, t, kn;

    while (scanf("%d", &pn)!=EOF)
    {
        if(!pn)break;
        for (i = 0; i < pn; i++) scanf("%lf%lf", &ps[i].x, &ps[i].y);
        getKernel(ps, pn, kernel, kn);
        if (kn) printf("1\n");
        else printf("0\n");
    }
    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值