poj 1584 A Round Peg in a Ground Hole(计算几何)

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

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

Source


题意:给定一个n边形,若不是凸多边形则输出HOLE IS ILL-FORMED,若是而且圆在凸多边形里面则输出PEG WILL FIT,若是而且圆不在凸多边形里面则

输出PEG WILL NOT FIT

题解:先判断多边形是否凸多边形,根据第一条偏转的线段判断后面的偏转方向是否相同。然后判断圆心是否在多边形内,判断一下圆心对多边形的三角形面积和是否等于原多边形面积就可以了。然后就是判圆心到凸多边形的距离,若圆心与点的连线和该点的边组成钝角,则最短距离为到顶点的距离,否则是到直线的距离,到直线的距离可以用三角形面积巧妙计算,具体如代码

#include<stdio.h>
#include<math.h>
#include<algorithm>
#define eps 1e-8
struct point{
    double x,y;
}p[1000008],c;
using namespace std;
double cross(point p1,point p2,point p3)
{
    return (p2.x-p1.x)*(p3.y-p1.y)-(p2.y-p1.y)*(p3.x-p1.x);
}
double dot(point p1,point p2,point p3)
{
    return (p2.x-p1.x)*(p3.x-p1.x)+(p2.y-p1.y)*(p3.y-p1.y);
}
double dis(point p1,point p2)
{
    return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
}
int judge(int n,int i=0)
{
    double temp=cross(p[i],p[i+1],p[i+2]);
    while(fabs(temp)<eps&&i<n-2) i++,temp=cross(p[i],p[i+1],p[i+2]);
    for(i++;i<=n-2;i++)
    {
        if(cross(p[i],p[i+1],p[i+2])*temp<-eps)
            return 0;
    }
    return 1;
}
int judge2(int n,double temp=0,double temp2=0)
{
    for(int i=1;i<n;i++) temp+=fabs(cross(p[0],p[i],p[i+1]));
    for(int i=0;i<n;i++) temp2+=fabs(cross(c,p[i],p[i+1]));
    if(fabs(temp-temp2)<eps) return 1;
    return 0;
}
int main()
{
    int n,i,flag;
    double r,d;

    while(scanf("%d",&n),n>=3)
    {
        scanf("%lf%lf%lf",&r,&c.x,&c.y);
        for(i=0;i<n;i++) scanf("%lf%lf",&p[i].x,&p[i].y);
        p[n]=p[0];
        if(!judge(n)) printf("HOLE IS ILL-FORMED\n");
        else
        {
            if(judge2(n)) flag=1;
            else flag=0;
            for(int i=0;i<n&&flag;i++)
            {
                if(dot(p[i],c,p[i+1])<-eps||dot(p[i+1],c,p[i])<-eps)
                    d=min(dis(c,p[i]),dis(c,p[i+1]));
                else d=fabs(cross(c,p[i],p[i+1]))/dis(p[i],p[i+1]);
                if(d<r) flag=0;
            }
            if(flag) printf("PEG WILL FIT\n");
            else printf("PEG WILL NOT FIT\n");
        }
    }

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值