POJ 1584 A Round Peg in a Ground Hole 判断一个多边形是否为凸包

本文介绍了一种用于检测DIY家具组装套件中不规则孔洞的算法,旨在判断孔洞是否适合插入木制销钉。通过分析孔洞形状是否为凸多边形,并检查销钉是否能在指定位置正确安装,确保家具结构稳定。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://poj.org/problem?id=1584
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
Mid-Atlantic 2003
题目大意:给出一个多边形,一个圆的圆心和半径,如果该多边形不是凸多边形,输出 H O L E    I S    I L L _ F O R M E D HOLE\ \ IS\ \ ILL\_FORMED HOLE  IS  ILL_FORMED,否则若圆包含在该多边形内(与边界相切也算)输出 P E G    W I L L    F I T PEG\ \ WILL\ \ FIT PEG  WILL  FIT,否则输出 P E G    W I L L    N O T    F I T PEG\ \ WILL\ \ NOT\ \ FIT PEG  WILL  NOT  FIT
思路:利用叉积来判断一个多边形是不是凸包,即看相邻两条边的叉积的方向是否与前两条边的叉积的方向是一致的。如果是凸包,再判断圆心与多边形的位置关系,若圆心在多边形边界上且 r r r=0,也是符合题意的一种情况;若圆心在多边形内,就判断圆心到每一条边的距离,若距离 d i s dis dis &gt; = r &gt;=r >=r,则说明该圆在多边形内部。
代码前面一部分都是模板,所以比较多。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<vector>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
const double pi=acos(-1.0);//弧度pi
const double eps=1e-10;//精度

struct point
{
    double x,y;
    point(double a=0,double b=0)
    {
        x=a,y=b;
    }
    friend point operator * (point a,double b)
    {
        return point(a.x*b,a.y*b);
    }
    friend point operator * (double a,point b)
    {
        return point(b.x*a,b.y*a);
    }
    point operator - (const point &b)const
    {
        return point(x-b.x,y-b.y);
    }
    point operator + (const point &b)const
    {
        return point(x+b.x,y+b.y);
    }
    point operator / (const double b)const
    {
        return point(x/b,y/b);
    }
    bool operator < (const point &b)const//按坐标排序
    {
        if(fabs(x-b.x)<eps)
            return y<b.y-eps;
        return x<b.x-eps;
    }
    void transxy(double sinb,double cosb)//逆时针旋转b弧度
    {                                      //若顺时针 在传入的sinb前加个-即可
        double tx=x,ty=y;
        x=tx*cosb-ty*sinb;
        y=tx*sinb+ty*cosb;
    }
    void transxy(double b)//逆时针旋转b弧度
    {                     //若顺时针传入-b即可
        double tx=x,ty=y;
        x=tx*cos(b)-ty*sin(b);
        y=tx*sin(b)+ty*cos(b);
    }
    double norm()
    {
        return sqrt(x*x+y*y);
    }
};

inline double dot(point a,point b)//点积
{
    return a.x*b.x+a.y*b.y;
}
inline double cross(point a,point b)//叉积
{
    return a.x*b.y-a.y*b.x;
}

inline double dist(point a,point b)//两点间距离
{
    return (a-b).norm();
}

inline int sgn(double x)
{
    if(fabs(x)<eps)
        return 0;
    if(x>0)
        return 1;
    return -1;
}

int gcd(int a,int b)
{
    return b==0?a:gcd(b,a%b);
}

bool point_on_seg(point p,point s,point t)//判断点p 是否在线段st上 包括端点
{
    return sgn(cross(p-s,t-s))==0&&sgn(dot(p-s,p-t))<=0;
}

struct line
{
    point s,e;
    line(point _s,point _e)
    {
        s=_s,e=_e;
    }
};

inline double dis_point_seg(point p,line l)//计算点到线段距离
{
    if(sgn(dot(p-l.s,l.e-l.s))<0)//不存在 返回点到线段端点的距离
        return (p-l.s).norm();
    if(sgn(dot(p-l.e,l.s-l.e))<0)//不存在 返回点到线段端点的距离
        return (p-l.e).norm();
    return fabs(cross(l.s-p,l.e-p))/dist(l.s,l.e);//|叉积|=2*S△
}

typedef point Vector;

const int maxn=1005;

struct polygon
{
    int n;//多边形顶点数
    point a[maxn];//0 到 n-1 顺时针顺序
    polygon() {}
    double perimeter()//计算多边形周长
    {
        double sum=0;
        a[n]=a[0];
        for(int i=0;i<n;i++)
            sum+=(a[i+1]-a[i]).norm();
        return sum;
    }
    double area()//计算多边形有向面积
    {
        double sum=0;
        a[n]=a[0];
        for(int i=0;i<n;i++)
            sum+=cross(a[i+1],a[i]);
        return sum/2;
    }
    double final_area()//多边形面积 即一定为正数
    {
        return fabs(area());
    }
    int point_in(point t)//t在多边形外返回0 t在多边形内返回1 t在多边形边界上返回2
    {
        int num=0,d1,d2,k;
        a[n]=a[0];
        for(int i=0;i<n;i++)
        {
            if(point_on_seg(t,a[i],a[i+1]))
                return 2;
            k=sgn(cross(a[i+1]-a[i],t-a[i]));
            d1=sgn(a[i].y-t.y);
            d2=sgn(a[i+1].y-t.y);
            if(k>0&&d1<=0&&d2>0)
                ++num;
            if(k<0&&d2<=0&&d1>0)
                --num;
        }
        return num!=0;
    }
    point mass_center()//求多边形的重心坐标
    {
        point ans=point(0,0);
        if(sgn(area())==0)
            return ans;//多边形面积为0时重心没有定义 特判
        a[n]=a[0];
        for(int i=0;i<n;i++)
            ans=ans+(a[i]+a[i+1])*cross(a[i+1],a[i]);
        return ans/area()/6;
    }
    int border_int_point_num()
    {
        int num=0;
        a[n]=a[0];
        for(int i=0;i<n;i++)
            num+=gcd(abs(int(a[i+1].x-a[i].x)),abs(int(a[i+1].y-a[i].y)));
        return num;
    }
    int inside_int_point_num()
    {
        return int(final_area())+1-border_int_point_num()/2;
    }
    bool is_convex()
    {
        a[n]=a[0],a[n+1]=a[1];
        double tag=0,tmp;
        for(int i=0;i<n;i++)
        {
            tmp=cross(a[i+1]-a[i],a[i+2]-a[i+1]);
            if(tag==0)
                tag=tmp;
            else if(tag*tmp<0)//方向不同
                return 0;
        }
        return 1;
    }
};

int n;
point b[maxn];

int main()
{
    while(~scanf("%d",&n)&&n>=3)
    {
        point circle;
        double r;
        scanf("%lf%lf%lf",&r,&circle.x,&circle.y);
        polygon p;
        p.n=n;
        for(int i=0;i<n;i++)
            scanf("%lf%lf",&p.a[i].x,&p.a[i].y);
        if(!p.is_convex())
            printf("HOLE IS ILL-FORMED\n");
        else
        {
            int tmp=p.point_in(circle);
            if(tmp==2&&sgn(r)==0)//在多边形边上且半径为0
                printf("PEG WILL FIT\n");
            else if(tmp==1)//在多边形内部
            {
                bool flag=0;
                p.a[n]=p.a[0];
                for(int i=0;i<n;i++)
                {
                    if(sgn(dis_point_seg(circle,line(p.a[i],p.a[i+1]))-r)<0)
                        flag=1;
                    if(flag)
                        break;
                }
                if(flag)
                    printf("PEG WILL NOT FIT\n");
                else
                    printf("PEG WILL FIT\n");
            }
            else//在多边形外部
                printf("PEG WILL NOT FIT\n");
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值