POJ 1584 凸包算法的使用

B - A Round Peg in a Ground Hole
Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u
Submit  Status

Description

The DIY Furniture company specializes in assemble-it-yourself furniture kits. Typically, the pieces of wood are attached to one another using a wooden peg that fits into pre-cut holes in each piece to be attached. The pegs have a circular cross-section and so are intended to fit inside a round hole. 
A recent factory run of computer desks were flawed when an automatic grinding machine was mis-programmed. The result is an irregularly shaped hole in one piece that, instead of the expected circular shape, is actually an irregular polygon. You need to figure out whether the desks need to be scrapped or if they can be salvaged by filling a part of the hole with a mixture of wood shavings and glue. 
There are two concerns. First, if the hole contains any protrusions (i.e., if there exist any two interior points in the hole that, if connected by a line segment, that segment would cross one or more edges of the hole), then the filled-in-hole would not be structurally sound enough to support the peg under normal stress as the furniture is used. Second, assuming the hole is appropriately shaped, it must be big enough to allow insertion of the peg. Since the hole in this piece of wood must match up with a corresponding hole in other pieces, the precise location where the peg must fit is known. 
Write a program to accept descriptions of pegs and polygonal holes and determine if the hole is ill-formed and, if not, whether the peg will fit at the desired location. Each hole is described as a polygon with vertices (x1, y1), (x2, y2), . . . , (xn, yn). The edges of the polygon are (xi, yi) to (x  i+1, y  i+1) for i = 1 . . . n − 1 and (xn, yn) to (x1, y1).

Input

Input consists of a series of piece descriptions. Each piece description consists of the following data: 
Line 1 < nVertices > < pegRadius > < pegX > < pegY > 
number of vertices in polygon, n (integer) 
radius of peg (real) 
X and Y position of peg (real) 
n Lines < vertexX > < vertexY > 
On a line for each vertex, listed in order, the X and Y position of vertex The end of input is indicated by a number of polygon vertices less than 3.

Output

For each piece description, print a single line containing the string: 
HOLE IS ILL-FORMED if the hole contains protrusions 
PEG WILL FIT if the hole contains no protrusions and the peg fits in the hole at the indicated position 
PEG WILL NOT FIT if the hole contains no protrusions but the peg will not fit in the hole at the indicated position

Sample Input

5 1.5 1.5 2.0
1.0 1.0
2.0 2.0
1.75 2.0
1.0 3.0
0.0 2.0
5 1.5 1.5 2.0
1.0 1.0
2.0 2.0
1.75 2.5
1.0 3.0
0.0 2.0
1

Sample Output

HOLE IS ILL-FORMED
PEG WILL NOT FIT

题意:就是给顺时针或者逆时针连续的点,然后判断是不是凸包,如果是凸包的话,再判断圆心是否在凸包内,如果圆心在凸包内,再判断整个圆是否在凸包内。

思路:看了挺久这题了,又看了奎神给的PDF130页又研究了好久,才知道这凸包判断的基本算法,不过和奎神给的模板一样,都是差不多的,用哪个都一样……

#include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <algorithm>
#define MAX 111116
#define eps 1e-7
using namespace std;
int sgn(const double &x)
{
    return x < -eps? -1 : (x > eps);
}
inline double sqr(const double &x)
{
    return x * x;
}
struct Point
{
    double x, y;
    Point() {}
    Point(const double &x, const double &y):x(x), y(y) {}
    Point operator - (const Point &a)const
    {
        return Point(x - a.x, y - a.y);
    }
    Point operator + (const Point &a)const
    {
        return Point(x + a.x, y + a.y);
    }
    Point operator / (const double &a)const
    {
        return Point(x / a, y / a);
    }
    friend double det(const Point &a, const Point &b)
    {
        return a.x * b.y - a.y * b.x;
    }
    friend double dot(const Point &a, const Point &b)
    {
        return a.x * b.x + a.y * b.y;
    }
    friend double dist(const Point &a,const Point &b)
    {
        return sqrt(sqr(a.x - b.x) + sqr(a.y - b.y));
    }
    bool operator < (const Point &a)const
    {
        return sgn(x - a.x) < 0 || (sgn(x - a.x) == 0 && sgn(y - a.y) < 0);
    }
    void in()
    {
        scanf("%lf %lf",&x, &y);
    }
    void out()const
    {
        cout<<x<<" "<<y<<endl;
    }
};
struct Line
{
    Point s, t;
    Line() {}
    Line(const Point &s, const Point &t):s(s), t(t) {}
    void in() { s.in(),t.in(); }
    double pointDistLine(const Point &p)
    {
        if(sgn(dot(t - s, p - s)) < 0)return dist(p, s);
        if(sgn(dot( s - t, p - t)) < 0)return dist(p, t);
        return fabs(det(t - s, p - s)) / dist(s, t);
    }
    bool pointOnLine(const Point &p)
    {
        return sgn(det(t - s, p - s)) == 0 && sgn(dot(s - p, t - p)) <= 0;
    }
};
struct Poly
{
    vector<Point>p;
    vector<Point>tb;// 逆时针凸包
    void in(int r)
    {
        p.resize(r);
        for(int i = 0; i < r; i++)
            p[i].in();
    }

    //判断点集是否为凸包
    bool isCanHull()
    {
        int n = p.size();
        int m = 0;
        sort(p.begin(), p.end());
        tb.resize(n + 1);
        for(int i = 0; i < n; i++)
        {
            while(m > 1 && sgn(det(tb[m - 1]- tb[m - 2],  p[i] - tb[m - 2])) < 0)m--;
            tb[m++] = p[i];
        }
        int k = m;
        for(int i = n - 2; i >= 0; i --)
        {
            while(m > k && sgn(det(tb[m - 1] - tb[m - 2],  p[i] - tb[m -2])) < 0)m--;
            tb[m++] = p[i];
        }
        if(m) tb.resize(m - 1);
        return m - 1 == n;
    }

    //判断点t(圆心)是否在凸包内部,这个是O(logn)的算法
    bool isContainOlogn(const Point &t)
    {
        int n = tb.size();
        if(n < 3)return 0;
        Point g = (tb[0] + tb[n / 3] + tb[n * 2 / 3] )/ 3.0;
        int l = 0, r = n;
        while(l + 1 < r)
        {
            int mid = (l + r) >> 1;
            int k = sgn(det(tb[l] - g, tb[mid] - g) );
            int dl = sgn(det(tb[l] - g, t - g) );
            int dr = sgn(det(tb[mid] - g, t - g) );
            if(k > 0)
            {
                if(dl >= 0 && dr < 0) r = mid;
                else l = mid;
            }
            else
            {
                if(dl < 0 && dr >= 0) l = mid;
                else r = mid;
            }
        }
        r %= n;
        int res = sgn(det(tb[l] - t, tb[r] - t));
        if(res >= 0) return true;
        return false;
    }

    //判断点t是否在凸包内部,这个是O(n)的算法
    bool isContainOn(const Point &t)
    {
        int sign = 0;
        int n = tb.size();
        for(int i = 0; i < n; i++)
        {
            Line line(tb[i], tb[(i + 1) % n]);
            if(line.pointOnLine(t))return 2;
        }
        for(int i = 0; i < n; i++)
        {
            int signt = sgn(det(tb[i] - t, tb[(i + 1) % n] - t));
            if(signt)
            {
                if(!sign) sign = signt;
                else if(signt != sign) return false;
            }
        }
        return true;
    }
} poly;
struct Cir
{
    Point o;
    double r;
    void in() { scanf("%lf", &r),o.in(); }

    //判断圆半径是否小于到多边形的最小边
    bool isInsectPoly(const Poly &poly)
    {
        int len = poly.tb.size();
        for(int i  = 0; i < len; i++)
        {
            Line line = Line(poly.tb[i], poly.tb[(i + 1)%len]); //把凸包的点集扫一遍即可
            double dist = line.pointDistLine(o);
            if(sgn(dist - r) < 0) return true; //然后判断距离即可
        }
        return false;
    }
} cir;
int n;
void solve()
{
    if(!poly.isCanHull()) puts("HOLE IS ILL-FORMED");
    else if(!poly.isContainOlogn(cir.o) || cir.isInsectPoly(poly))
        puts("PEG WILL NOT FIT");
    else puts("PEG WILL FIT");
}
int main()
{
    while(cin>>n, n > 2)
    {
        cir.in();
        poly.in(n);
        solve();
    }
    return 0;
}


你可能感兴趣的:(POJ 1584 凸包算法的使用)