poj 1584A Round Peg in a Ground Hole(计算几何 判断凸包)

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

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

大致题意:

按照顺时针或逆时针方向输入一个n边形的顶点坐标集,先判断这个n边形是否为凸包。

再给定一个圆形(圆心坐标和半径),判断这个圆是否完全在n变形内部。

1、先判断是不是凸多边形。我们可以通过两个向量的叉积来判断第三个点在前两个点的什么方向。 2、再判断点是否在凸多边形内。如果在凸多边形内的话,对于枚举凸多边形上的每一个点和它的下一个点所组成的向量,与这个点与圆心组成的向量的叉积符号一定是相同的,因为对于凸多边形来说,如果某一个点在其内部的话,一定在其所有边的相同一侧。 3,最后判断圆心到每个边距离的最小值是否大于圆的半径

3、当圆心在圆内时,判断圆与多边形的关系

   设圆心为P,逐条枚举n边形的边AB,利用得到△PAB的面积,

再根据公式S=0.5*|AB|*h,可以得到 注意  是先输入半径  在输入圆心   因为这个wa一早晨 ……
#include<stdio.h>
#include<string>
#include<math.h>
struct point
{
    double x,y;
}pt[11000],pointa;
double cross(point a,point b,point c)//叉乘 a起点  b,c为终点
{
    return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y);
}
int judge_tubao(int n)//判断是否为凸包
{
    int i;
    double a,b;
    a=0;
    for(i=0;i<n-1;i++)
    {
        b = cross(pt[i],pt[i+1],pt[i+2]);
        if(!a)//避免最初的点出现共线的情况  
            a=b;
        else if(a*b<0) return 0;//如果不同号说明 不是凸包
    }
    return 1;
}
int judge_yuanxin(int n)//判断圆心是否在凸包当中
{
    double a,b;
    int i;
    a=0;
    for(i=0;i<n;i++)
    {
        b = cross(pointa,pt[i],pt[i+1]);
        if(!a)
            a=b;
        else if(a*b<=0) return 0;//如果异号说明在外面 等于说明在边上
    }
    return 1;
}

double dis(point a,point b)//两点之间的距离
{
    return sqrt(pow(a.x-b.x,2)+pow(a.y-b.y,2));
}

double get_high(point a,point b)//求圆心到边的距离,利用三角形面积
{
    double h;
    double s = fabs(cross(pointa,a,b));
    double l = dis(a,b);
    h=s/l;
    return h;
}
int judge_zhijing(int n,double r)//判断半径超了没
{
    int i;
    for(i=0;i<n;i++)
    {
        if(get_high(pt[i],pt[i+1])<r)
            return 0;
    }
    return 1;
}
int main()
{
    double rx,ry,r;
    int n,i,j;

    while(~scanf("%d",&n),n>=3)
    {
        scanf("%lf%lf%lf",&r,&rx,&ry);

        for(i=0;i<n;i++)
            scanf("%lf%lf",&pt[i].x,&pt[i].y);

        pt[n] = pt[0];//这步很关键 别忘了


        pointa.x=rx;//pointa记录圆心
        pointa.y=ry;

        if(judge_tubao(n))
        {
            if(judge_zhijing(n,r)&&judge_yuanxin(n))
            {
                printf("PEG WILL FIT\n");
            }
            else
            {
                printf("PEG WILL NOT FIT\n");
            }
        }

        else
            printf("HOLE IS ILL-FORMED\n");
    }
    return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值