• POJ 1584 A Round Peg in a Ground Hole[判断凸包 点在多边形内]


    A Round Peg in a Ground Hole
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 6682   Accepted: 2141

    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 (xi+1, yi+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

    Source


    题意:顺时针或逆时针给出n个点,问这n个点围成的图形是不是个凸多边形。如果是凸多边形,给一个圆,以圆心的坐标和半径表示,问这个圆是不是完全在凸多边形内部。

    用相邻两条边的叉积相同判断凸包,注意共线(一个叉积为0)
    然后直接用射线法判断点在多边形内行了
    最后判断每条边到圆心距离与半径比较
     
    我的多边形是从1开始
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <vector>
    using namespace std;
    typedef long long ll;
    const int N=1005;
    const double eps=1e-8;
    inline int read(){
        char c=getchar();int x=0,f=1;
        while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();}
        while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();}
        return x*f;
    }
    inline int sgn(double x){
        if(abs(x)<eps) return 0;
        else return x<0?-1:1;
    }
    struct Vector{
        double x,y;
        Vector(double a=0,double b=0):x(a),y(b){}
        bool operator <(const Vector &a)const{
            return x<a.x||(x==a.x&&y<a.y);
        }
        void print(){
            printf("%lf %lf
    ",x,y);
        }
    };
    typedef Vector Point;
    Vector operator +(Vector a,Vector b){return Vector(a.x+b.x,a.y+b.y);}
    Vector operator -(Vector a,Vector b){return Vector(a.x-b.x,a.y-b.y);}
    Vector operator *(Vector a,double b){return Vector(a.x*b,a.y*b);}
    Vector operator /(Vector a,double b){return Vector(a.x/b,a.y/b);}
    bool operator ==(Vector a,Vector b){return sgn(a.x-b.x)==0&&sgn(a.y-b.y)==0;}
    
    double Cross(Vector a,Vector b){
        return a.x*b.y-a.y*b.x;
    }
    double Dot(Vector a,Vector b){
        return a.x*b.x+a.y*b.y;
    }
    
    double Len(Vector a){return sqrt(Dot(a,a));}
    double DisTS(Point p,Point a,Point b){
        if(a==b) return Len(p-a);
        Vector v1=b-a,v2=p-a,v3=p-b;
        if(sgn(Dot(v1,v2))<0) return Len(v2);
        else if(sgn(Dot(v1,v3))>0) return Len(v3);
        else return abs(Cross(v1,v2)/Len(v1));
    }
    int PointInPolygon(Point p,Point poly[],int n){
        int wn=0;
        for(int i=1;i<=n;i++){
            if(sgn(DisTS(p,poly[i],poly[i%n+1]))==0) return -1;
            int k=sgn(Cross(poly[i%n+1]-poly[i],p-poly[i])),
            d1=sgn(poly[i].y-p.y),d2=sgn(poly[i%n+1].y-p.y);
            if(k>0&&d1<=0&&d2>0) wn++;
            if(k<0&&d2<=0&&d1>0) wn--;
        }
        return (bool)wn;
    }
    bool isConvex(Point poly[],int n){
        int last=0,now=0;
        for(int i=1;i<=n;i++){
            now=sgn(Cross(poly[i%n+1]-poly[i],poly[(i+1)%n+1]-poly[i%n+1]));
            if(last==0||now==0||now*last>0) last=now;
            else return false;
        }
        return true;
    }
    int n;
    double r,x,y,x2,y2;
    Point poly[N];
    void solve(){
        if(isConvex(poly,n)){
            Point c(x,y);
            if(PointInPolygon(c,poly,n)){
                int flag=1;
                for(int i=1;i<=n;i++)
                    if(sgn(DisTS(c,poly[i],poly[i%n+1])-r)<0){flag=0;break;}
                if(flag) puts("PEG WILL FIT");
                else puts("PEG WILL NOT FIT");
            }else puts("PEG WILL NOT FIT");
        }else puts("HOLE IS ILL-FORMED");
    }
    int main(int argc, const char * argv[]) {
        while(true){
            n=read();if(n<3) break;
            scanf("%lf%lf%lf",&r,&x,&y);
            for(int i=1;i<=n;i++)
                scanf("%lf%lf",&poly[i].x,&poly[i].y);
            solve();
        }
        return 0;
    }
  • 相关阅读:
    ATS缓存数据结构
    Akamai CDN
    spring中的设计模式
    深入解析spring中用到的九种设计模式
    24种设计模式的通俗理解
    JDK中所包含的设计模式
    JDK源码中使用的设计模式
    算法-索引
    JAVA REENTRANTLOCK、SEMAPHORE 的实现与 AQS 框架
    扒一扒ReentrantLock以及AQS实现原理
  • 原文地址:https://www.cnblogs.com/candy99/p/6354171.html
Copyright © 2020-2023  润新知